spring security does not redirect after successful login

老子叫甜甜 提交于 2020-07-18 15:46:17

问题


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


回答1:


try to use :

defaultSuccessUrl("/",true)

it's works for me




回答2:


have a look at this:

  auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery(
                "SELECT username, password, 'true' as enabled FROM scl_user_info   WHERE user_id = ?");

table column should be username, password



来源:https://stackoverflow.com/questions/28405863/spring-security-does-not-redirect-after-successful-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!