【发布时间】:2020-11-25 10:59:04
【问题描述】:
在使用spring session的spring boot项目中,如何为不同的url配置两个session管理策略?
- 对于 Angular 前端,我想使用默认实现并创建
X-Auth-Token令牌(如果需要,创建会话) - 但对于暴露的 API 端点,我想使用无状态会话管理
我尝试了以下配置,但根本没有创建会话。我认为第二个块正在覆盖sessionCreationPolicy,因为它位于末尾
SecurityConfig.java
@Override
public void configure(HttpSecurity http) throws Exception
{
//Requests from angular app
http.authorizeRequests()
.antMatchers("/", "/login","/api/v1/user/login", "/api/v1/user/authenticate", "/api/v1/user/logout", "/api/v1/health/find/status").permitAll()
.antMatchers("/api/v1/person/**").hasAnyAuthority(ROLE_USER)
.and()
.httpBasic()
.and()
.exceptionHandling().authenticationEntryPoint(customBasicAuthenticationEntryPoint)
.and()
.logout()
.invalidateHttpSession(true).clearAuthentication(true)
.and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
//Requests from external systems
http.authorizeRequests()
.antMatchers("/api/v1/external/**").hasAnyAuthority(ROLE_API_USER)
.and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
更新
为 API 端点和 Angular 应用添加了自定义 WebSecurity 配置器适配器,如下所示。添加后,API 端点不会创建会话,但 Angular HTTP 请求也不会创建会话
@Configuration
@EnableWebSecurity
public class SecurityConfig
{
....
@Configuration
@Order(1)
public class ExternalApiSecurityConfig extends WebSecurityConfigurerAdapter
{
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
{
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).eraseCredentials(true);
}
@Override
public void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests()
.antMatchers("/api/v1/search/**").hasAnyAuthority(ROLE_API_USER, ROLE_SYS_ADMIN)
.and()
.httpBasic()
.and()
.exceptionHandling().authenticationEntryPoint(customBasicAuthenticationEntryPoint)
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.headers()
.frameOptions().disable();
// Uses CorsConfigurationSource bean defined below
http.cors().configurationSource(corsConfigurationSource());
http.csrf().disable();
}
}
@Configuration
@Order(2)
public class DefaultSecurityConfig extends WebSecurityConfigurerAdapter
{
@Bean(BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception
{
return super.authenticationManagerBean();
}
@Override
public void configure(WebSecurity webSecurity)
{
webSecurity.ignoring().antMatchers("/static/**");
}
@Override
public void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests()
.antMatchers("/", "/login","/api/v1/user/login", "/api/v1/user/authenticate", "/api/v1/user/logout", "/api/v1/health/find/status").permitAll()
.antMatchers("/api/v1/person/**").hasAnyAuthority(ROLE_USER)
.and()
.httpBasic()
.and()
.exceptionHandling().authenticationEntryPoint(customBasicAuthenticationEntryPoint)
.and()
.logout()
.invalidateHttpSession(true).clearAuthentication(true)
.and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
{
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).eraseCredentials(true);
auth.authenticationProvider(getDaoAuthenticationProvider()).eraseCredentials(true);
}
}
....
}
【问题讨论】:
标签: spring-boot spring-security spring-session