mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
package com.panzhihua.auth.handel;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
 
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.utlis.ResultUtil;
 
import lombok.extern.slf4j.Slf4j;
 
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: 登录失败
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2020-11-24 16:02
 **/
@Slf4j
@Component
public class UserLoginFailureHandler implements AuthenticationFailureHandler {
    /**
     * 登录失败返回结果
     * 
     * @Author youcong
     */
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) {
        log.info("登录失败");
        // 这些对于操作的处理类可以根据不同异常进行不同处理
        if (exception instanceof UsernameNotFoundException) {
            ResultUtil.responseJson(response, R.fail("该账号不存在"));
        }
        if (exception instanceof LockedException) {
            ResultUtil.responseJson(response, R.fail("用户被冻结"));
        }
        if (exception instanceof BadCredentialsException) {
            ResultUtil.responseJson(response, R.fail("密码错误"));
        }
        ResultUtil.responseJson(response, R.fail("登录失败"));
    }
}