Răsfoiți Sursa

优化认证逻辑

woody 1 an în urmă
părinte
comite
14573261c4

+ 13 - 0
framework-base/src/main/java/com/chelvc/framework/base/context/SessionContextHolder.java

@@ -547,6 +547,19 @@ public class SessionContextHolder implements ServletRequestListener {
         response(response, result(status, message));
     }
 
+    /**
+     * 相应请求结果
+     *
+     * @param response Http响应对象
+     * @param code     结果码
+     * @param message  结果消息
+     * @throws IOException I/O异常
+     */
+    public static void response(@NonNull HttpServletResponse response, @NonNull String code, String message)
+            throws IOException {
+        response(response, Result.of(code, null, message));
+    }
+
     /**
      * 相应请求结果
      *

+ 6 - 3
framework-oauth/src/main/java/com/chelvc/framework/oauth/config/OAuthConfigurer.java

@@ -16,7 +16,6 @@ import com.chelvc.framework.base.context.LoggingContextHolder;
 import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.base.util.HttpUtils;
 import com.chelvc.framework.base.util.SpringUtils;
-import com.chelvc.framework.common.model.Result;
 import com.chelvc.framework.common.util.AESUtils;
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
@@ -43,6 +42,7 @@ import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
 import org.springframework.security.oauth2.core.OAuth2Error;
 import org.springframework.security.oauth2.jwt.JwtDecoder;
 import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
+import org.springframework.security.oauth2.server.resource.InvalidBearerTokenException;
 import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
 import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
 import org.springframework.security.web.AuthenticationEntryPoint;
@@ -89,9 +89,12 @@ public class OAuthConfigurer extends WebSecurityConfigurerAdapter {
     public AuthenticationEntryPoint authenticationEntryPoint() {
         return (request, response, e) -> {
             LoggingContextHolder.warn(log, request, e);
-            if (e instanceof OAuth2AuthenticationException) {
+            if (e instanceof InvalidBearerTokenException) {
+                SessionContextHolder.response(response, "TOKEN_INVALID",
+                        ApplicationContextHolder.getMessage("Token.Invalid"));
+            } else if (e instanceof OAuth2AuthenticationException) {
                 OAuth2Error error = ((OAuth2AuthenticationException) e).getError();
-                SessionContextHolder.response(response, Result.of(error.getErrorCode(), null, error.getDescription()));
+                SessionContextHolder.response(response, error.getErrorCode(), error.getDescription());
             } else {
                 SessionContextHolder.response(response, HttpStatus.FORBIDDEN, e.getMessage());
             }