AuthenticationExceptionHandler.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.diagbot.config;
  2. import com.diagbot.facade.SysUserFacade;
  3. import com.diagbot.util.StringUtil;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.security.authentication.AccountExpiredException;
  7. import org.springframework.security.authentication.AccountStatusException;
  8. import org.springframework.security.authentication.BadCredentialsException;
  9. import org.springframework.security.authentication.CredentialsExpiredException;
  10. import org.springframework.security.authentication.DisabledException;
  11. import org.springframework.security.authentication.InsufficientAuthenticationException;
  12. import org.springframework.security.authentication.LockedException;
  13. import org.springframework.security.core.AuthenticationException;
  14. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  15. import org.springframework.stereotype.Component;
  16. import org.springframework.validation.BindException;
  17. import javax.servlet.ServletException;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. /**
  23. * @Description:权限认证异常处理
  24. * @Author songxl
  25. * @Date 2021/12/13
  26. */
  27. @Component
  28. public class AuthenticationExceptionHandler {
  29. @Autowired
  30. private SysUserFacade userFacade;
  31. public void handleException(HttpServletRequest request, HttpServletResponse response,
  32. AuthenticationException authException) throws ServletException {
  33. Map map = new HashMap();
  34. //登录前的获取登录页面的请求接口不知道什么原因会抛出未认证(Full authentication is required to access this resource)
  35. //如果抛出未认证在这个调用这个服务接口返回消息
  36. //响应状态码统一为200
  37. response.setStatus(HttpServletResponse.SC_OK);
  38. if (matchers("/sys/user/getHospitalMark", request)) {
  39. map.put("code", "0");
  40. map.put("msg", "");
  41. map.put("data", userFacade.getHospitalMark());
  42. } else if (authException instanceof BadCredentialsException) {
  43. map.put("code", "00000001");
  44. map.put("msg", "用户或密码不正确");
  45. } else if (authException instanceof AccountStatusException) {
  46. if (authException instanceof LockedException) {
  47. map.put("code", "00000001");
  48. map.put("msg", "账户锁定");
  49. } else if (authException instanceof AccountExpiredException) {//账户过期
  50. map.put("code", "10020011");
  51. map.put("msg", "登录超时。为确保您的账户安全,系统已自动退出,请重新登录。");
  52. } else if (authException instanceof CredentialsExpiredException) {//证书过期
  53. map.put("code", "10020011");
  54. map.put("msg", "登录超时。为确保您的账户安全,系统已自动退出,请重新登录。");
  55. } else if (authException instanceof DisabledException) {
  56. map.put("code", "00000001");
  57. map.put("msg", "账户不可用");
  58. } else {
  59. map.put("code", "00000001");
  60. map.put("msg", "用户状态异常");
  61. }
  62. } else if (authException instanceof InsufficientAuthenticationException) {
  63. map.put("code", "10020011");
  64. map.put("msg", "登录超时。为确保您的账户安全,系统已自动退出,请重新登录。");
  65. } else {
  66. map.put("code", "00000001");
  67. map.put("msg", authException.getMessage());
  68. }
  69. response.setContentType("application/json");
  70. try {
  71. ObjectMapper mapper = new ObjectMapper();
  72. mapper.writeValue(response.getOutputStream(), map);
  73. } catch (Exception e) {
  74. throw new ServletException();
  75. }
  76. }
  77. private boolean matchers(String url, HttpServletRequest request) {
  78. AntPathRequestMatcher matcher = new AntPathRequestMatcher(url);
  79. if (matcher.matches(request)) {
  80. return true;
  81. }
  82. return false;
  83. }
  84. }