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

时间:2019-02-11 18:48:39

标签: java spring spring-security

我正在使用基于注释的配置开发具有Spring安全性的代码。

但是在从登录页面点击 WebSecurityConfig 类的 configure 方法(扩展了 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>  

我想念什么?

我正在使用Spring Security 5.1.2.release

2 个答案:

答案 0 :(得分:0)

每个人都必须可以访问登录页面和注销页面。在登录配置后和注销配置后,您错过了 .pemitAll()方法

答案 1 :(得分:0)

它一直在工作。实际上,我为此错过了密码编码器。当我将其重定向到成功页面时。