Spring 弹簧&x27;s网络安全';忽略';仍在创建会话

Spring 弹簧&x27;s网络安全';忽略';仍在创建会话,spring,session,spring-security,Spring,Session,Spring Security,我希望避免在用户请求css文件等静态资源时创建会话。然而,即使在告诉WebSecurity忽略这个静态资源路径之后,我注意到来自SpringBoot应用程序的所有响应仍然有JSESSIONID cookie。为什么? public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/css/**"); } @Override protected void configure(Ht

我希望避免在用户请求css文件等静态资源时创建会话。然而,即使在告诉WebSecurity忽略这个静态资源路径之后,我注意到来自SpringBoot应用程序的所有响应仍然有JSESSIONID cookie。为什么?

public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/css/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.formLogin()
        .loginPage("/login")
        .loginProcessingUrl("/login")
        .and()
        .authorizeRequests()
        .antMatchers(HttpMethod.GET, "/login", "/error").permitAll() 
        .anyRequest().authenticated(); //all other pages require users to be authenticated

}

我正在使用Spring安全性来保护我的应用程序。。。。但是对于对这些静态资源的请求,我不希望创建或验证会话。

我发现了问题。是我的错。我正在使用Spring安全性和扩展RequestHeaderAuthenticationFilter。而web.ignoring()。。。虽然避免了所有Spring安全身份验证/授权检查,但它仍然通过自定义过滤器运行

在我的一个过滤器中,我有以下行:

HttpServletRequest req = ((HttpServletRequest) request);
HttpSession session = req.getSession();
我使用它来检查会话是否已经存在,以及是否需要刷新它(太复杂了,这里无法解释)。但是,
req.getSession()
将创建一个新会话,如果它还不存在的话。我不知道它有副作用

相反,我应该调用:
req.getSession(false)
。如果会话已经存在,则返回该会话;如果会话不存在,则返回null