| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package com.welampiot.security;
- import com.welampiot.common.BaseResult;
- import com.welampiot.common.BusinessException;
- import com.welampiot.common.ResultEnum;
- import com.welampiot.utils.WebUtils;
- 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;
- /**
- * 登录失败处理逻辑
- */
- @Component
- public class CustomizeAuthenticationFailureHandler implements AuthenticationFailureHandler {
- @Override
- public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException, BusinessException {
- //返回json数据失败信息
- // if (e instanceof AccountExpiredException) {
- // //账号过期
- // result = ResultTool.fail(ResultCode.USER_ACCOUNT_EXPIRED);
- // } else if (e instanceof BadCredentialsException) {
- // //密码错误
- // result = ResultTool.fail(ResultCode.USER_CREDENTIALS_ERROR);
- // } else if (e instanceof CredentialsExpiredException) {
- // //密码过期
- // result = ResultTool.fail(ResultCode.USER_CREDENTIALS_EXPIRED);
- // } else if (e instanceof DisabledException) {
- // //账号不可用
- // result = ResultTool.fail(ResultCode.USER_ACCOUNT_DISABLE);
- // } else if (e instanceof LockedException) {
- // //账号锁定
- // result = ResultTool.fail(ResultCode.USER_ACCOUNT_LOCKED);
- // } else if (e instanceof InternalAuthenticationServiceException) {
- // //用户不存在
- // result = ResultTool.fail(ResultCode.USER_ACCOUNT_NOT_EXIST);
- // }else{
- // //其他错误
- // result = ResultTool.fail(ResultCode.COMMON_FAIL);
- // }
- //处理编码方式,防止中文乱码的情况
- // httpServletResponse.setContentType("text/json;charset=utf-8");
- //塞到HttpServletResponse中返回给前台
- // httpServletResponse.getWriter().write(JSON.toJSONString(BaseResult.fail(ResultEnum.LOGIN_FAIL)));
- WebUtils.renderString(httpServletResponse,BaseResult.fail(ResultEnum.LOGIN_FAIL));
- }
- public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, BusinessException e) {
- if (e instanceof BusinessException) {
- WebUtils.renderString(response,BaseResult.fail("40101",e.getMessage(),""));
- return;
- }
- WebUtils.renderString(response,BaseResult.fail(ResultEnum.LOGIN_FAIL));
- }
- }
|