11package io .github .talelin .latticy .common .interceptor ;
22
3- import com .auth0 .jwt .exceptions .*;
3+ import com .auth0 .jwt .exceptions .AlgorithmMismatchException ;
4+ import com .auth0 .jwt .exceptions .InvalidClaimException ;
5+ import com .auth0 .jwt .exceptions .JWTDecodeException ;
6+ import com .auth0 .jwt .exceptions .SignatureVerificationException ;
47import com .auth0 .jwt .exceptions .TokenExpiredException ;
58import com .auth0 .jwt .interfaces .Claim ;
69import io .github .talelin .autoconfigure .bean .MetaInfo ;
2831
2932/**
3033 * @author pedro@TaleLin
34+ * @author Juzi@TaleLin
3135 */
32- @ SuppressWarnings ("Duplicates" )
3336@ Component
3437public class AuthorizeVerifyResolverImpl implements AuthorizeVerifyResolver {
3538
36- public final static String authorizationHeader = "Authorization" ;
39+ public final static String AUTHORIZATION_HEADER = "Authorization" ;
3740
38- public final static String bearerPattern = "^Bearer$" ;
41+ public final static String BEARER_PATTERN = "^Bearer$" ;
3942
4043 @ Autowired
4144 private DoubleJWT jwt ;
@@ -53,9 +56,10 @@ public class AuthorizeVerifyResolverImpl implements AuthorizeVerifyResolver {
5356 private String servePath ;
5457
5558
59+ @ Override
5660 public boolean handleLogin (HttpServletRequest request , HttpServletResponse response , MetaInfo meta ) {
5761 String tokenStr = verifyHeader (request , response );
58- Map <String , Claim > claims = null ;
62+ Map <String , Claim > claims ;
5963 try {
6064 claims = jwt .decodeAccessToken (tokenStr );
6165 } catch (TokenExpiredException e ) {
@@ -70,30 +74,35 @@ public boolean handleLogin(HttpServletRequest request, HttpServletResponse respo
7074 public boolean handleGroup (HttpServletRequest request , HttpServletResponse response , MetaInfo meta ) {
7175 handleLogin (request , response , meta );
7276 UserDO user = LocalUser .getLocalUser ();
73- if (verifyAdmin (user ))
77+ if (verifyAdmin (user )) {
7478 return true ;
79+ }
7580 long userId = user .getId ();
7681 String permission = meta .getPermission ();
7782 String module = meta .getModule ();
7883 List <PermissionDO > permissions = userService .getUserPermissions (userId );
7984 boolean matched = permissions .stream ().anyMatch (it -> it .getModule ().equals (module ) && it .getName ().equals (permission ));
80- if (!matched )
85+ if (!matched ) {
8186 throw new AuthenticationException ("you don't have the permission to access" , 10001 );
87+ }
8288 return true ;
8389 }
8490
91+ @ Override
8592 public boolean handleAdmin (HttpServletRequest request , HttpServletResponse response , MetaInfo meta ) {
8693 handleLogin (request , response , meta );
8794 UserDO user = LocalUser .getLocalUser ();
88- if (!verifyAdmin (user ))
95+ if (!verifyAdmin (user )) {
8996 throw new AuthenticationException ("you don't have the permission to access" , 10001 );
97+ }
9098 return true ;
9199 }
92100
93101
102+ @ Override
94103 public boolean handleRefresh (HttpServletRequest request , HttpServletResponse response , MetaInfo meta ) {
95104 String tokenStr = verifyHeader (request , response );
96- Map <String , Claim > claims = null ;
105+ Map <String , Claim > claims ;
97106 try {
98107 claims = jwt .decodeRefreshToken (tokenStr );
99108 } catch (TokenExpiredException e ) {
@@ -148,7 +157,7 @@ private boolean verifyAdmin(UserDO user) {
148157
149158 private String verifyHeader (HttpServletRequest request , HttpServletResponse response ) {
150159 // 处理头部header,带有access_token的可以访问
151- String authorization = request .getHeader (authorizationHeader );
160+ String authorization = request .getHeader (AUTHORIZATION_HEADER );
152161 if (authorization == null || Strings .isBlank (authorization )) {
153162 throw new AuthorizationException ("authorization field is required" , 10012 );
154163 }
@@ -160,7 +169,7 @@ private String verifyHeader(HttpServletRequest request, HttpServletResponse resp
160169 String scheme = splits [0 ];
161170 // token 字段
162171 String tokenStr = splits [1 ];
163- if (!Pattern .matches (bearerPattern , scheme )) {
172+ if (!Pattern .matches (BEARER_PATTERN , scheme )) {
164173 throw new AuthorizationException ("authorization field is invalid" , 10013 );
165174 }
166175 return tokenStr ;
0 commit comments