[英]Understanding authentication and session management using Spring boot
我用 mongo db 作为后端做了一个 spring boot 项目。 我想确保在维护某种内存会话的同时对用户进行身份验证和授权(使用 redis 或内置的 spring 会话)
我经历过很多像turorials的这个,这个, 这个等
他们都要求您扩展WebSecurityConfigAdapter
,配置HttpSecurity
并提供UserDetailService
。 我已经通过以下方式完成了。
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public UserDetailsService userDetailsService(){
return new StockUserDetailService();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login/**").permitAll()
.antMatchers("/logout/**").permitAll()
.antMatchers("/admin/**").hasAuthority("ADMIN")
.antMatchers("/broker/**").hasAnyAuthority("BROKER")
.anyRequest().fullyAuthenticated();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService());
}
}
但我不明白的是 sessin 管理在哪里进行,用户应该使用什么url
登录? 如果我编写了一个带有/login
映射的控制器,那么我的操作应该在登录控制器中做什么。 我真的没有得到全貌。
更新
我尝试发布到/login
。 我收到这个错误
{
"timestamp": 1494842451672,
"status": 403,
"error": "Forbidden",
"message": "Could not verify the provided CSRF token because your session was not found.",
"path": "/login"
}
Spring 为您管理/login
路由。 您只需要使用用户凭据向其发送 POST 请求。 如果您想指定另一个登录 URL,您可以使用.loginProcessingUrl("/myCustomLoginUrl")
对于UserDetailsService
您必须提供您自己的实现来实现loadUserByUsername(String userName)
方法,该方法从数据库或您所在的任何持久性存储中检索用户使用并返回具有相应权限的org.springframework.security.core.userdetails.User
。 请参阅以下官方文档: https : //spring.io/guides/gs/securing-web/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.