我使用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 :(得分:5)
尝试使用:
defaultSuccessUrl("/",true)
它对我有用
答案 1 :(得分:0)
看看这个:
auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery(
"SELECT username, password, 'true' as enabled FROM scl_user_info WHERE user_id = ?");
表格列应为用户名,密码