Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring security does not redirect after successful login

i use spring security 3.2.5 for user authentication. after i provide user name and password in login page it is not redirecting to page mentioned in defaultSuccessUrl method and it just reloads login page.

following is my code please let me know what is wrong in this.

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource dataSource;

@Override
public void configure(WebSecurity webSecurity) throws Exception{
    webSecurity
        .ignoring()
            .antMatchers("/resources/**")
            .antMatchers(HttpMethod.POST, "/login");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http 
    .authorizeRequests()
    .anyRequest()
    .authenticated()
    .and()
            .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/")
                .failureUrl("/login?error=true")
                //.usernameParameter("username").passwordParameter("password")
                .permitAll()
            .and()
            .logout()
                .logoutSuccessUrl("/login")
                .permitAll()
            .and()
            .csrf();
}

@Override
protected void configure(AuthenticationManagerBuilder auth)
        throws Exception {
    auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery(
            "SELECT user_id, password, 'true' as enabled FROM scl_user_info   WHERE user_id = ?");
}
}

login.jsp

<form action="<c:url value="/j_spring_security_check" />" method="POST">
   <input type="text" class="form-control" placeholder="Username" name="j_username"/>
   <input type="password" class="form-control" placeholder="Password" name="j_password"/>
   <input type="checkbox" />
   <span class="lbl"> Remember Me</span>
   <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
   <button type="submit" class="width-35 pull-right btn btn-sm btn-primary">
         <i class="ace-icon fa fa-key"></i>
         <span class="bigger-110">Login</span>
    </button>
</form>

HomeController

 @Controller
 public class HomeController {

@RequestMapping("/")
public String defaultPage() {
    return "home";
}

@RequestMapping("/login")
public String login(@RequestParam(value = "error", required = false) String error,
        @RequestParam(value = "logout", required = false) String logout) {
    return "login";
}

}

table columns

id, user_id, password

like image 392
Prasad Avatar asked Feb 09 '15 08:02

Prasad


1 Answers

try to use :

defaultSuccessUrl("/",true)

it's works for me

like image 177
Galih Tias Avatar answered Nov 13 '22 23:11

Galih Tias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!