shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.iplatform.security;
 
import com.iplatform.security.exception.PcUserStopAppException;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.web.ResponseCode;
import com.walker.web.ResponseValue;
import com.walker.web.util.ServletUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
public class DefaultAuthenticationFailureHandler implements AuthenticationFailureHandler {
 
    protected final transient Logger logger = LoggerFactory.getLogger(getClass());
 
    @Override
    public void onAuthenticationFailure(HttpServletRequest request
            , HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        String message = null;  //提示信息
        int code = ResponseCode.USER_CREDENTIALS_ERROR.getCode();         //错误编码
 
        if(exception instanceof AccountExpiredException){
            message = ResponseCode.USER_ACCOUNT_EXPIRED.getMessage();
            logger.debug("---------> " + message);
        }else if(exception instanceof BadCredentialsException){
            message = ResponseCode.USER_CREDENTIALS_ERROR.getMessage();
        }else if(exception instanceof CredentialsExpiredException){
            message = "密码过期,登录失败!";
        }else if(exception instanceof DisabledException){
            message = ResponseCode.USER_ACCOUNT_DISABLE.getMessage();
        }else if(exception instanceof LockedException){
            message = ResponseCode.USER_ACCOUNT_LOCKED.getMessage();
        }else if(exception instanceof InternalAuthenticationServiceException){
            message = ResponseCode.USER_CREDENTIALS_ERROR.getMessage();
        }else if(exception instanceof PcUserStopAppException){
            message = PcUserStopAppException.MESSAGE;
        }else{
            message = "登录失败!";
        }
 
        try {
            ServletUtils.renderString(response, JsonUtils.objectToJsonString(ResponseValue.error(code, message)));
        } catch (Exception e) {
            logger.error("认证失败:" + request.getRequestURI(), e);
        }
    }
}