繁体   English   中英

春季安全性没有类型为AuthenticationSuccessHandler和AuthenticationFalureHandler的合格Bean

[英]spring security No qualifying bean of type AuthenticationSuccessHandler and AuthenticationFalureHandler

@Component("MyAuthFilter")
        public class MyAuthFilter extends UsernamePasswordAuthenticationFilter {

    private int errCode = 0;

    @Autowired
    @Qualifier("authenticationManager")
    //@Override
    public void setAuthenticationManager(AuthenticationManager authenticationManager, AuthenticationSuccessHandler successHandler, AuthenticationFailureHandler failureHandler) {
        super.setAuthenticationManager(authenticationManager);
        this.setAuthenticationSuccessHandler(successHandler);
        this.setAuthenticationFailureHandler(failureHandler);
    }

    @Override
    public AuthenticationFailureHandler getFailureHandler() {
        SimpleUrlAuthenticationFailureHandler handler = new SimpleUrlAuthenticationFailureHandler();
        handler.setDefaultFailureUrl("/login?error=" + errCode);
        return handler;
    }

    @Override
    public AuthenticationSuccessHandler getSuccessHandler() {
        SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
        handler.setDefaultTargetUrl("/courses");
        return handler;
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException {

        System.out.println("running my own version of UsernmePasswordFilter ... ");

        String login = (String) request.getParameter("login");
        String password = (String) request.getParameter("password");
        errCode = validate(login,password);       
        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(login, password);
        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);

        return this.getAuthenticationManager().authenticate(authRequest);
    }

    private int validate(String login,String password){

        if (login.isEmpty() && password.isEmpty()){
            return 4;
        } 
        if (login.isEmpty() && !password.isEmpty()){
            return 2;
        } 
            if (!login.isEmpty() && password.isEmpty()){
            return 3;
        } 

        return 1;
    }
}

这是MyAuthFilter。

这是我的spring-security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-4.2.xsd">

    <http auto-config="false" use-expressions="true">
        <intercept-url pattern="/courses*" access="hasRole('ROLE_USER')" />
        <custom-filter  before="FORM_LOGIN_FILTER" ref="MyAuthFilter" />
        <form-login
            login-page="/login"
            default-target-url="/courses"
            authentication-failure-url="/login"
            username-parameter="loginField"
            password-parameter="passwordField" />
        <csrf disabled="true" />
    </http>

    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="ars" password="1234" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

当我尝试启动我的应用程序时,我遇到了一个例外

没有类型为AuthenticationSuccessHandler的合格Bean

和FailureHandler的相同错误。 我将不胜感激任何帮助。

您的AuthenticationSuccessHandler未声明为Bean。 您应该将其创建为Bean,然后通过属性authentication-success-handler-ref =“ nameOfYouSuccessHandlerBean”在spring-security.xml中的标签中注册

因此,它看起来像:配置Java文件中的某些位置:

    @Bean
    public AuthenticationSuccessHandler mySuccessHandler() {
        SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
        handler.setDefaultTargetUrl("/courses");
        return handler;
    }

在spring-security.xml中

 <form-login
            login-page="/login"
            default-target-url="/courses"
            authentication-failure-url="/login"
            username-parameter="loginField"
            authentication-success-handler-ref="mySuccessHandler"            
            password-parameter="passwordField" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM