1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.yiboshi.science.config.security;
import com.yiboshi.arch.exception.BusinessException;
import com.yiboshi.science.enumeration.CommonEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
;
/**
* 获取当前登录的用户
*
* @author kylin
* @date 2021-11-17
*/
@Slf4j
@Component
public class SecurityUserHolder {
/**
* 获取系统用户名称
*
* @return 系统用户
*/
public static SecurityUser getCurrentUser() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new BusinessException("Login expired or not logged in!");
}
if (authentication.getPrincipal() instanceof SecurityUser) {
SecurityUser securityUser = (SecurityUser) authentication.getPrincipal();
return securityUser;
}
throw new BusinessException("Login expired or not logged in!");
}
/**
* 获取系统人员ID
*
* @return
*/
public static String getPersonId() {
return getCurrentUser().getPersonId();
}
/**
* 获取系统用户ID
*
* @return
*/
public static String getUserId() {
return getCurrentUser().getUserId();
}
/**
* 获取专家ID
*
* @return
*/
public static String getExpertId() {
return getCurrentUser().getExpertId();
}
/**
* 获取系统单位ID
*
* @return
*/
public static String getUnitId() {
return getCurrentUser().getUnitId();
}
/**
* 获取系统单位treeCode
*
* @return
*/
public static String getUnitCode() {
return getCurrentUser().getUnitCode();
}
/**
* 获取RoleId
*
* @return
*/
public static String getRoles() {
return getCurrentUser().getRoles();
}
/**
* 获取Token
*
* @return
*/
public static String getToken() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String bearerToken = request.getHeader("Authorization");
String tokenHead = "Bearer ";
if (StringUtils.hasText(bearerToken) && bearerToken.startsWith(tokenHead)) {
// 去掉令牌前缀
return bearerToken.replace(tokenHead, "");
} else {
bearerToken = request.getParameter("access_token");
if (StringUtils.hasText(bearerToken) && bearerToken.startsWith(tokenHead)) {
// 去掉令牌前缀
return bearerToken.replace(tokenHead, "");
}
}
return null;
}
public static String getAuthType() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String authType = request.getHeader("Auth-Type");
if (StringUtils.hasText(authType))
return authType;
else
return null;
}
public static boolean isKeyDiscipline() {
if (null != getAuthType() && getAuthType().equals("2"))
return true;
else
return false;
}
public static int projectType() {
if (null != getAuthType() && getAuthType().equals("2"))
return CommonEnum.projType.key.getCode();
else
return CommonEnum.projType.num.getCode();
}
}