我正在使用Spring Boot,Spring MVC和Spring Security。我添加了JWT授权,因此我需要使我的应用程序会话无状态,因此我将相应的参数添加到我的安全配置:
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
但是,当我向我的应用程序提出任何请求时,我会得到jsessionId作为cookie。我试图通过将此代码添加到我的JWT过滤器中来解决问题:
Cookie[] cookies = httpServletRequest.getCookies();
if(cookies!=null)
for (int i = 0; i < cookies.length; i++) {
cookies[i].setMaxAge(0);
httpServletResponse.addCookie(cookies[i]);
}
但没有帮助,所以如何最终删除它?
我的完整安全代码:
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests()
.antMatchers("/user/login").permitAll().antMatchers("/user/get/**").hasRole(Role.BOT.toString()).antMatchers("/", "/login**","/callback/", "/webjars/**", "/error**")
.permitAll().anyRequest().authenticated();
http.apply(new JwtFilterConfiguer(provider));
}
您希望cookie在会话期间持续存在的-1个信号的最大信号。您要将最大设置为0。
来自[API文档] [1]:
负值意味着cookie不会持续存储,并且在Web浏览器退出时将被删除。零值会导致cookie被删除。
请您可以按照此链接也可以'https://www.baeldung.com/java-servlet-cookies-session'