AuthenticationUser.java 7.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
package cn.ibizlab.util.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
10
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
11 12 13 14 15 16 17 18 19 20 21
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.ObjectUtils;
import java.sql.Timestamp;
import java.util.*;
import com.alibaba.fastjson.JSONObject;

@Data
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class AuthenticationUser implements UserDetails
{
22 23 24 25
	public AuthenticationUser(){}
	/**
	 * 用戶标识
	 */
26
	private String userid;
27 28 29
	/**
	 * 用户全局名
	 */
30
	private String username;
31 32 33
	/**
	 * 用户姓名
	 */
34
	private String personname;
35 36 37
	/**
	 * 登录名
	 */
38
	private String loginname;
39 40 41 42 43 44 45
	/**
	 * 用户工号
	 */
	private String usercode;
	/**
	 * 登录密码
	 */
46
	private String password;
47 48 49
	/**
	 * 区属
	 */
50
	private String domain;
51
	/**
52 53 54
     * 租户
     */
    private String srfdcid;
55 56 57 58 59
    /**
     * 系统标识
     */
    private String srfsystemid;
	/**
60 61 62 63 64
    /**
     * 动态实例标识
     */
    private String srfdynainstid;
	/**
65 66
	 * 部门标识
	 */
67
	private String mdeptid;
68 69 70
	/**
	 * 部门编码
	 */
71
	private String mdeptcode;
72 73 74
	/**
	 * 部门名称
	 */
75
	private String mdeptname;
76 77 78
	/**
	 * 业务编码
	 */
79
	private String bcode;
80 81 82
	/**
	 * 岗位标识
	 */
83
	private String postid;
84 85 86
	/**
	 * 岗位代码
	 */
87
	private String postcode;
88 89 90
	/**
	 * 岗位名称
	 */
91
	private String postname;
92 93 94
	/**
	 * 单位标识
	 */
95
	private String orgid;
96 97 98
	/**
	 * 单位编码
	 */
99
	private String orgcode;
100 101 102
	/**
	 * 单位名称
	 */
103
	private String orgname;
104 105 106
	/**
	 * 昵称别名
	 */
107
	private String nickname;
108 109 110
	/**
	 * 邮箱
	 */
111
	private String email;
112 113 114
	/**
	 * 社交账号
	 */
115
	private String avatar;
116 117 118
	/**
	 * 电话
	 */
119
	private String phone;
120 121 122
	/**
	 * 照片
	 */
123
	private String usericon;
124 125 126
	/**
	 * 性别
	 */
127
	private String sex;
128 129 130
	/**
	 * 出生日期
	 */
131
	private Timestamp birthday;
132 133 134
	/**
	 * 证件号码
	 */
135
	private String certcode;
136 137 138
	/**
	 * 地址
	 */
139
	private String addr;
140 141 142
	/**
	 * 主题
	 */
143
	private String theme;
144 145 146
	/**
	 * 字号
	 */
147
	private String fontsize;
148 149 150
	/**
	 * 语言
	 */
151
	private String lang;
152 153 154
	/**
	 * 备注
	 */
155
	private String memo;
156 157 158 159 160 161 162
	/**
	 * 保留字段
	 */
	private String reserver;
	/**
	 * 用户上下文参数
	 */
163
	private Map <String,Object> sessionParams;
164 165 166
	/**
	 * 用户权限资源
	 */
167 168
	@JsonIgnore
	private Collection<GrantedAuthority> authorities;
169 170 171
    /**
    * 是否为超级管理员
    */
172
    private int superuser;
173 174 175
    /**
    * 用户权限资源
    */
176
    private JSONObject permissionList;
177 178 179
    /**
    * 用户上下文参数
    */
180
    @JsonIgnore
181 182 183 184 185 186 187 188
    private Map<String,Object> userSessionParam;
    /**
    * 当前用户上下级组织信息
    */
    private Map<String, Set<String>> orgInfo;
	/**
	* 上级组织
	*/
189
	private String porg;
190 191 192
	/**
	* 下级组织
	*/
193
	private String sorg;
194 195 196
	/**
	* 上级部门
	*/
197
	private String pdept;
198 199 200
	/**
	* 下级部门
	*/
201
	private String sdept;
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

	@JsonIgnore
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@JsonIgnore
	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@JsonIgnore
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@JsonIgnore
	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	public static AuthenticationUser getAuthenticationUser()
	{
        if(SecurityContextHolder.getContext()==null||SecurityContextHolder.getContext().getAuthentication()==null||SecurityContextHolder.getContext().getAuthentication().getPrincipal()==null){
			return new AuthenticationUser();
		}
    	Object userDetails = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		AuthenticationUser authuserdetail;
		if(userDetails instanceof AuthenticationUser ) {
			authuserdetail= (AuthenticationUser)userDetails;
		}
		else {
			authuserdetail=new AuthenticationUser();
		}
	 	return authuserdetail;
	}

248 249 250 251 252 253 254 255 256
	public static AuthenticationUser setAuthenticationUser(String userId , String userName) {
		AuthenticationUser user = new AuthenticationUser();
		user.setUserid(userId);
		user.setPersonname(userName);
		UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
		SecurityContextHolder.getContext().setAuthentication(authentication);
		return user;
	}

257 258 259 260 261
    public Map <String,Object> getSessionParams()
    {
		if(this.sessionParams==null)
		{
			sessionParams = getUserSessionParam();
262
			sessionParams.put("srfsystemid",this.getSrfsystemid());
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
			sessionParams.put("srfpersonid", this.getUserid());
			sessionParams.put("srfpersonname", this.getPersonname());
			sessionParams.put("srforgsectorid", this.getMdeptid());
			sessionParams.put("srforgsectorcode", this.getMdeptcode());
			sessionParams.put("srforgsectorname", this.getMdeptname());
			sessionParams.put("srforgid", this.getOrgid());
			sessionParams.put("srforgcode", this.getOrgcode());
			sessionParams.put("srforgname", this.getOrgname());
			sessionParams.put("srfuserid", this.getUserid());
			sessionParams.put("srfusername", this.getPersonname());
			sessionParams.put("srfusermode", "");
			sessionParams.put("srforgsectorbc", this.getBcode());
			sessionParams.put("srfloginname", this.getLoginname());
			sessionParams.put("srflocale", this.getLang());
			sessionParams.put("srftimezone", "");
    		sessionParams.put("srfusercode", this.getUsercode());
279 280 281 282
			sessionParams.put("srfporg", this.getPorg());
			sessionParams.put("srfsorg", this.getSorg());
			sessionParams.put("srfpdept", this.getPdept());
			sessionParams.put("srfsdept", this.getSdept());
283 284 285 286
		}
		return this.sessionParams;
    }
    private Map<String, Object> getUserSessionParam() {
287
		if(userSessionParam!=null){
288
			return userSessionParam;
289 290
		}
		else{
291
			return new HashMap<>();
292
		}
293 294
    }

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	public void setOrgInfo(Map<String, Set<String>> orgInfo) {
		this.orgInfo = orgInfo;
		if(!ObjectUtils.isEmpty(orgInfo) && !ObjectUtils.isEmpty(orgInfo.get("parentorg"))){
			porg=(String.format("'%s'",String.join("','",orgInfo.get("parentorg"))));
            this.getSessionParams().put("srfporg",porg);
		}
		if(!ObjectUtils.isEmpty(orgInfo) && !ObjectUtils.isEmpty(orgInfo.get("suborg")) ){
			sorg=(String.format("'%s'",String.join("','",orgInfo.get("suborg"))));
            this.getSessionParams().put("srfsorg",sorg);
		}
		if(!ObjectUtils.isEmpty(orgInfo) && !ObjectUtils.isEmpty(orgInfo.get("parentdept"))){
			pdept=(String.format("'%s'",String.join("','",orgInfo.get("parentdept"))));
            this.getSessionParams().put("srfpdept",pdept);
		}
		if(!ObjectUtils.isEmpty(orgInfo) && !ObjectUtils.isEmpty(orgInfo.get("subdept"))){
			sdept=(String.format("'%s'",String.join("','",orgInfo.get("subdept"))));
            this.getSessionParams().put("srfsdept",sdept);
		}
	}

315 316
	public void setPermissionList(JSONObject permissionList) {
		this.permissionList = permissionList;
317
		if(authorities==null && permissionList !=null){
318 319 320
		if(permissionList.getJSONArray("authorities")!=null){
			authorities=new ArrayList<>();
			permissionList.getJSONArray("authorities").
321 322 323 324 325
				forEach(item->authorities.add(new SimpleGrantedAuthority(String.valueOf(item))));
			}
		}
	}
}