简体   繁体   中英

Spring Security Custom Login Processing URL always redirecting to failurehandler

I am developing a code with spring security with annotation based configuration.

But after hitting the login processing url defined in configure method of WebSecurityConfig Class (which extends WebSecurityConfigurerAdapter ) from login page its always redirecting to .failureHandler() method of httpSecurity rather than .successHandler() even if correct username and password is provided.

Following is the configure method and configureGlobal method of WebSecurityConfig class.

@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");

}   

here is the snippet of 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>  

What i am missing ?

i am using spring security 5.1.2.release

Login page and logout page must be accessable for everyone. You have missed .pemitAll() method after login config and after logout config.

Its been working. Actually i missed a password encoder for this. While i gave one its redirecting to success page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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