我不需要这里的角色,也不需要任何数据库。这份文件对于我的要求来说太多了。我只想通过登录页面对用户进行身份验证。用户成功通过身份验证后,我将创建一个 cookie。所有传入的请求都会被检查是否有有效的 cookie。
我的想法:将有一个过滤器检查每个传入的请求,并且如果 cookie 过期,将有一个入口点将用户重定向到登录页面。
但是我不知道将这些简单的需求与 spring security 集成......或者我应该使用其他东西吗?
最佳答案
如果您使用 Java Config,那么 Spring Security 配置将如下所示:
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private YourSpecificProvider provider; //simply wraps your API calls
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**", "/js/**", "/fonts/**", "/images/**").permitAll() //add whatever else needs to be served without authentication
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(provider);
}
}
通过该设置,Spring Security 将自动处理添加 cookie/检查 cookie/重定向等所有必要步骤。
您当然必须在 /login
根据您使用 Spring 的方式,您需要将 Spring Security 添加到 Servlet 过滤器链
关于java - 简单的 Spring Security 要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24178127/