使用Security框架,登录时authenticate()方法报 stackover

原因: AuthenticationManager 注入错误

错误注入:

 	@Override
    @Bean
    protected AuthenticationManager authenticationManager()
            throws Exception {
     
        return super.authenticationManager();
    }

正确:

	@Override
    @Bean
    public AuthenticationManager authenticationManagerBean()
            throws Exception {
     
        return super.authenticationManagerBean();
    }

原理:
使用Security框架,登录时authenticate()方法报 stackover_第1张图片在登录时,通过UsernamePasswordAuthenticationFilter类中 attemptAuthentication()方法进行登录信息验证

public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
     
        if (this.postOnly && !request.getMethod().equals("POST")) {
     
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        } else {
     
            String username = this.obtainUsername(request);
            String password = this.obtainPassword(request);
            if (username == null) {
     
                username = "";
            }

            if (password == null) {
     
                password = "";
            }

            username = username.trim();
            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
            this.setDetails(request, authRequest);
            return this.getAuthenticationManager().authenticate(authRequest);
        }
    }

调用 authenticate()方法时,如果AuthenticationManager注入错误,
会调用实现类
使用Security框架,登录时authenticate()方法报 stackover_第2张图片
正确为:
在这里插入图片描述

你可能感兴趣的:(java,spring)