content: package site.alanliang.geekblog.security;
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.security.authentication.BadCredentialsException;
  import org.springframework.security.authentication.DisabledException;
  import org.springframework.security.authentication.LockedException;
  import org.springframework.security.core.AuthenticationException;
  import org.springframework.security.web.authentication.AuthenticationFailureHandler;
  import org.springframework.stereotype.Component;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * @Descriptin TODO
   * @Author AlanLiang
   * Date 2020/4/1 12:43
   * Version 1.0
   **/
  @Component
  @Slf4j
  public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
  
      @Autowired
      private ObjectMapper objectMapper;
  
      @Override
      public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
          log.info("登录失败");
          httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
          httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");
          httpServletResponse.setContentType("application/json;charset=UTF-8");
          httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
  
          Map<String, Object> map = new HashMap<>();
          if (e instanceof BadCredentialsException) {
              map.put("message", "用户名或密码错误，登录失败");
          } else if (e instanceof DisabledException) {
              map.put("message", "用户已被停用，登录失败");
          } else if (e instanceof LockedException) {
              map.put("message", "用户已被锁定，请联系管理员");
          }
  
          httpServletResponse.getWriter().write(objectMapper.writeValueAsString(map));
      }
  }
  