22
33import com .central .common .constant .CommonConstant ;
44import com .central .common .constant .SecurityConstants ;
5+ import com .central .common .context .LoginUserContextHolder ;
56import com .central .common .model .SysUser ;
67import com .central .common .utils .SpringUtil ;
78import com .central .oauth2 .common .token .CustomWebAuthenticationDetails ;
89import lombok .extern .slf4j .Slf4j ;
10+ import org .springframework .security .authentication .AnonymousAuthenticationToken ;
911import org .springframework .security .core .Authentication ;
12+ import org .springframework .security .core .context .SecurityContextHolder ;
1013import org .springframework .security .oauth2 .common .OAuth2AccessToken ;
1114import org .springframework .security .oauth2 .common .exceptions .InvalidTokenException ;
1215import org .springframework .security .oauth2 .common .exceptions .UnapprovedClientAuthenticationException ;
2225 *
2326 * @author zlt
2427 * @date 2018/5/13
28+ * <p>
29+ * Blog: https://zlt2000.gitee.io
30+ * Github: https://github.com/zlt2000
2531 */
2632@ Slf4j
2733public class AuthUtils {
@@ -71,12 +77,12 @@ private static String extractHeaderToken(HttpServletRequest request) {
7177 /**
7278 * 校验accessToken
7379 */
74- public static void checkAccessToken (HttpServletRequest request ) {
80+ public static SysUser checkAccessToken (HttpServletRequest request ) {
7581 String accessToken = extractToken (request );
76- checkAccessToken (accessToken );
82+ return checkAccessToken (accessToken );
7783 }
7884
79- public static void checkAccessToken (String accessTokenValue ) {
85+ public static SysUser checkAccessToken (String accessTokenValue ) {
8086 TokenStore tokenStore = SpringUtil .getBean (TokenStore .class );
8187 OAuth2AccessToken accessToken = tokenStore .readAccessToken (accessTokenValue );
8288 if (accessToken == null || accessToken .getValue () == null ) {
@@ -89,6 +95,17 @@ public static void checkAccessToken(String accessTokenValue) {
8995 if (result == null ) {
9096 throw new InvalidTokenException ("Invalid access token: " + accessTokenValue );
9197 }
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 ;
92109 }
93110
94111 /**
@@ -132,6 +149,21 @@ public static String getUsername(Authentication authentication) {
132149 return username ;
133150 }
134151
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+
135167 /**
136168 * 获取登陆的帐户类型
137169 */
0 commit comments