|
@@ -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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|