2015-02-09 119 views
0

我使用spring security 3.2.5進行用戶認證。在登錄頁面提供用戶名和密碼後,它不會重定向到defaultSuccessUrl方法中提到的頁面,它只是重新載入登錄頁面。成功登錄後彈簧安全不重定向

以下是我的代碼,請讓我知道這是什麼問題。

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

} 

表列

ID,USER_ID,密碼

+0

啓用調試日誌,看看會發生什麼。 – holmis83 2015-02-09 17:17:37

+0

日誌中沒有顯示任何內容我可以看到只有頁面在提交後重新加載。 – Prasad 2015-02-09 18:18:10

回答

1

嘗試使用:

​​

這是對我的作品

0

看看這個:

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

表列應該是用戶名,密碼