如何使spring boot永远不会发出会话cookie?
我正在使用spring boot开发Restful API服务器。 我将项目配置为使用基本身份validation,如下所示。
@ComponentScan @EnableAutoConfiguration @EnableWebSecurity @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { ... @Override protected void configure(HttpSecurity http) throws Exception { http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER).and() .csrf().disable() .authorizeRequests().anyRequest().hasRole("USER").and() .httpBasic(); } ... }
但是当我通过Chrome-Postman-Plugin测试API时,在第一次调用之后,服务器永远不需要用户凭据。 我注意到’JSESSIONID’cookie已经创建了。
我的项目中没有其他安全配置。 我想知道为什么会这样……
您是否尝试过使用SessionCreationPolicy.STATELESS
。 春季文档中 STATELESS
与NEVER
之间存在细微差别:
STATELESS
:Spring Security永远不会创建HttpSession
,它永远不会使用它来获取SecurityContext
。
NEVER
:Spring Security永远不会创建HttpSession
, 但如果它已经存在 , 它将使用HttpSession 。
因此,我建议您清除所有Cookie,将其切换为STATELESS
试。 当你切换到NEVER
时,你可能已经有了一个HttpSession
。