Pārlūkot izejas kodu

登录超时bug修改

songxinlu 3 gadi atpakaļ
vecāks
revīzija
b0f7a051db

+ 43 - 0
src/main/java/com/diagbot/config/AuthExceptionEntryPoint.java

@@ -0,0 +1,43 @@
+package com.diagbot.config;
+
+import com.diagbot.util.StringUtil;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.security.core.AuthenticationException;
+import org.springframework.security.web.AuthenticationEntryPoint;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @Description:
+ * @Author songxl
+ * @Date 2021/11/30
+ */
+public class AuthExceptionEntryPoint implements AuthenticationEntryPoint {
+
+
+    @Override
+    public void commence(HttpServletRequest request, HttpServletResponse response,
+                         AuthenticationException authException)
+            throws ServletException {
+        Map map = new HashMap();
+        if (StringUtil.isNotEmpty(authException.getMessage())&&authException.getMessage().contains("Access token expired")) {
+            map.put("code", "10020011");
+            map.put("msg", "登录超时。为确保您的账户安全,系统已自动退出,请重新登录。");
+        }else {
+            map.put("code", "00000001");
+            map.put("msg", authException.getMessage());
+        }
+        response.setContentType("application/json");
+        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+        try {
+            ObjectMapper mapper = new ObjectMapper();
+            mapper.writeValue(response.getOutputStream(), map);
+        } catch (Exception e) {
+            throw new ServletException();
+        }
+    }
+}

+ 0 - 25
src/main/java/com/diagbot/config/CustomExceptionTranslator.java

@@ -1,25 +0,0 @@
-package com.diagbot.config;
-
-import org.springframework.http.ResponseEntity;
-import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
-import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
-
-/**
- * @Description:
- * @Author songxl
- * @Date 2021/11/22
- */
-public class CustomExceptionTranslator extends DefaultWebResponseExceptionTranslator {
-
-    @Override
-    public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
-        ResponseEntity<OAuth2Exception> translate = super.translate(e);
-        OAuth2Exception body = translate.getBody();
-        CustomOauthException customOauthException = new CustomOauthException(body.getMessage(),body.getOAuth2ErrorCode(),
-                body.getHttpErrorCode());
-        ResponseEntity<OAuth2Exception> response = new ResponseEntity<>(customOauthException, translate.getHeaders(),
-                translate.getStatusCode());
-        return response;
-    }
-
-}

+ 0 - 31
src/main/java/com/diagbot/config/CustomOauthException.java

@@ -1,31 +0,0 @@
-package com.diagbot.config;
-
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
-
-/**
- * @Description:
- * @Author songxl
- * @Date 2021/11/22
- */
-@JsonSerialize(using = CustomOauthExceptionSerializer.class)
-public class CustomOauthException extends OAuth2Exception {
-
-    private String oAuth2ErrorCode;
-
-    private int httpErrorCode;
-
-    public CustomOauthException(String msg, String oAuth2ErrorCode, int httpErrorCode) {
-        super(msg);
-        this.oAuth2ErrorCode = oAuth2ErrorCode;
-        this.httpErrorCode = httpErrorCode;
-    }
-
-    public String getoAuth2ErrorCode() {
-        return oAuth2ErrorCode;
-    }
-
-    public int getHttpErrorCode() {
-        return httpErrorCode;
-    }
-}

+ 0 - 35
src/main/java/com/diagbot/config/CustomOauthExceptionSerializer.java

@@ -1,35 +0,0 @@
-package com.diagbot.config;
-
-import com.fasterxml.jackson.core.JsonGenerator;
-import com.fasterxml.jackson.databind.SerializerProvider;
-import com.fasterxml.jackson.databind.ser.std.StdSerializer;
-
-import java.io.IOException;
-
-/**
- * @Description:
- * @Author songxl
- * @Date 2021/11/22
- */
-public class CustomOauthExceptionSerializer extends StdSerializer<CustomOauthException> {
-
-    public CustomOauthExceptionSerializer() {
-        super(CustomOauthException.class);
-    }
-
-    @Override
-    public void serialize(CustomOauthException value, JsonGenerator gen, SerializerProvider provider) throws IOException {
-        //value内容适当的做一些错误类型判断
-        if ("invalid_token".equals(value.getoAuth2ErrorCode())) {
-            gen.writeStartObject();
-            gen.writeObjectField("code", "10020011");
-            gen.writeObjectField("msg", "登录超时。为确保您的账户安全,系统已自动退出,请重新登录。");
-            gen.writeEndObject();
-        }else {
-            gen.writeStartObject();
-            gen.writeObjectField("code", "00000001");
-            gen.writeObjectField("msg", "操作失败,请确认请求是否有误!!!");
-            gen.writeEndObject();
-        }
-    }
-}

+ 0 - 1
src/main/java/com/diagbot/config/OAuth2Configurer.java

@@ -48,7 +48,6 @@ public class OAuth2Configurer extends AuthorizationServerConfigurerAdapter {
             accessToken = Integer.parseInt(sysDictionaryFacade.getDictionaryWithKey().get("31").get("accessToken"));
             refreshToken = Integer.parseInt(sysDictionaryFacade.getDictionaryWithKey().get("31").get("refreshToken"));
         }
-        sysDictionaryFacade.getDictionaryWithKey().get("31").get("");
         clients.inMemory()
                 .withClient("uaa-service")
                 .secret("{noop}123456")

+ 3 - 3
src/main/java/com/diagbot/config/ResourceServerConfigurer.java

@@ -34,6 +34,8 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
     @Override
     public void configure(HttpSecurity http) throws Exception {
         http.cors()
+                .and()
+                .exceptionHandling().authenticationEntryPoint(new AuthExceptionEntryPoint())
                 .and()
                 .csrf().disable()
                 .authorizeRequests()
@@ -261,9 +263,7 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
     public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
         log.info("Configuring ResourceServerSecurityConfigurer");
         resources.resourceId("user-service").tokenStore(new JwtTokenStore(jwtTokenEnhancerClient()));
-        OAuth2AuthenticationEntryPoint authenticationEntryPoint = new OAuth2AuthenticationEntryPoint();
-        authenticationEntryPoint.setExceptionTranslator(new CustomExceptionTranslator());
-        resources.authenticationEntryPoint(authenticationEntryPoint);
+        resources.authenticationEntryPoint(new AuthExceptionEntryPoint());
     }
     @Autowired
     private CustomAccessTokenConverter customAccessTokenConverter;