2
2
3
3
import com .central .common .constant .CommonConstant ;
4
4
import com .central .common .constant .SecurityConstants ;
5
+ import com .central .common .context .LoginUserContextHolder ;
5
6
import com .central .common .model .SysUser ;
6
7
import com .central .common .utils .SpringUtil ;
7
8
import com .central .oauth2 .common .token .CustomWebAuthenticationDetails ;
8
9
import lombok .extern .slf4j .Slf4j ;
10
+ import org .springframework .security .authentication .AnonymousAuthenticationToken ;
9
11
import org .springframework .security .core .Authentication ;
12
+ import org .springframework .security .core .context .SecurityContextHolder ;
10
13
import org .springframework .security .oauth2 .common .OAuth2AccessToken ;
11
14
import org .springframework .security .oauth2 .common .exceptions .InvalidTokenException ;
12
15
import org .springframework .security .oauth2 .common .exceptions .UnapprovedClientAuthenticationException ;
15
18
16
19
import javax .servlet .http .HttpServletRequest ;
17
20
import java .nio .charset .StandardCharsets ;
18
- import java .util .Base64 ;
19
- import java .util .Enumeration ;
20
- import java .util .Map ;
21
+ import java .util .*;
21
22
22
23
/**
23
24
* 认证授权相关工具类
24
25
*
25
26
* @author zlt
26
27
* @date 2018/5/13
28
+ * <p>
29
+ * Blog: https://zlt2000.gitee.io
30
+ * Github: https://github.com/zlt2000
27
31
*/
28
32
@ Slf4j
29
33
public class AuthUtils {
@@ -73,12 +77,12 @@ private static String extractHeaderToken(HttpServletRequest request) {
73
77
/**
74
78
* 校验accessToken
75
79
*/
76
- public static void checkAccessToken (HttpServletRequest request ) {
80
+ public static SysUser checkAccessToken (HttpServletRequest request ) {
77
81
String accessToken = extractToken (request );
78
- checkAccessToken (accessToken );
82
+ return checkAccessToken (accessToken );
79
83
}
80
84
81
- public static void checkAccessToken (String accessTokenValue ) {
85
+ public static SysUser checkAccessToken (String accessTokenValue ) {
82
86
TokenStore tokenStore = SpringUtil .getBean (TokenStore .class );
83
87
OAuth2AccessToken accessToken = tokenStore .readAccessToken (accessTokenValue );
84
88
if (accessToken == null || accessToken .getValue () == null ) {
@@ -91,6 +95,17 @@ public static void checkAccessToken(String accessTokenValue) {
91
95
if (result == null ) {
92
96
throw new InvalidTokenException ("Invalid access token: " + accessTokenValue );
93
97
}
98
+ return setContext (result );
99
+ }
100
+
101
+ /**
102
+ * 用户信息赋值 context 对象
103
+ */
104
+ public static SysUser setContext (Authentication authentication ) {
105
+ SecurityContextHolder .getContext ().setAuthentication (authentication );
106
+ SysUser user = getUser (authentication );
107
+ LoginUserContextHolder .setUser (user );
108
+ return user ;
94
109
}
95
110
96
111
/**
@@ -134,6 +149,21 @@ public static String getUsername(Authentication authentication) {
134
149
return username ;
135
150
}
136
151
152
+ /**
153
+ * 获取登陆的用户对象
154
+ */
155
+ public static SysUser getUser (Authentication authentication ) {
156
+ SysUser user = null ;
157
+ if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken )) {
158
+ Object principal = authentication .getPrincipal ();
159
+ //客户端模式只返回一个clientId
160
+ if (principal instanceof SysUser ) {
161
+ user = (SysUser )principal ;
162
+ }
163
+ }
164
+ return user ;
165
+ }
166
+
137
167
/**
138
168
* 获取登陆的帐户类型
139
169
*/
0 commit comments