Spring Security 自定义登录处理 URL 始终重定向到故障处理程序

我正在开发一个带有基于注释的配置的 spring 安全代码。


但是,在从登录页面点击WebSecurityConfig类(扩展WebSecurityConfigurerAdapter )的配置方法中定义的登录处理 url 后,即使提供了正确的用户名和密码,它也总是重定向到httpSecurity 的.failureHandler()方法而不是.successHandler ( ) 方法。


下面是WebSecurityConfig类的configure方法和configureGlobal方法。


@Override

protected void configure(HttpSecurity httpSecurity) throws Exception {


    httpSecurity

    .authorizeRequests()

    .antMatchers("/login.success").access("hasRole('ROLE_USER')")

            .and()

            .formLogin()

            .loginPage("/login.home")

            .usernameParameter("username")

            .passwordParameter("password")

            .loginProcessingUrl("/login.do")

            .successHandler(applicationLoginSuccessHandler)

            .failureHandler(applicationLoginFailureHandler)

            .and()

            .logout()

            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"));

}


@Autowired

public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {

    authenticationManagerBuilder.inMemoryAuthentication().withUser("user").password("pass").roles("USER");


}   

这是login.jsp的片段


<c:url value="login.do" var="loginUrl"/>

<form action="${loginUrl}" method="POST">         

    <c:if test="${param.error != null}">          

        <p>  

            Invalid username and password.  

        </p>  

    </c:if>  

    <c:if test="${param.logout != null}">         

        <p>  

            You have been logged out.  

        </p>  

    </c:if>  

    <p>  

        <label for="username">Username</label>  

        <input type="text" id="username" name="username"/>      

    </p>  

    <p>  

        <label for="password">Password</label>  

        <input type="password" id="password" name="password"/>      

    </p>  

    <input type="hidden"                          

        name="${_csrf.parameterName}"  

        value="${_csrf.token}"/>  

    <button type="submit" class="btn">Log in</button>  

</form>  

我错过了什么?


我正在使用弹簧安全 5.1.2.release


哆啦的时光机
浏览 454回答 2
2回答

繁星点点滴滴

它一直在工作。实际上,我为此错过了密码编码器。虽然我给了一个重定向到成功页面。

慕容森

登录页面和注销页面必须可供所有人访问。&nbsp;在登录配置和注销配置之后,您错过了&nbsp;.pemitAll() 方法。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java