【问题标题】:Spring's WebSecurity 'ignore' still creating sessionsSpring 的 WebSecurity“忽略”仍在创建会话
【发布时间】:2017-06-11 01:56:30
【问题描述】:

我想避免在用户请求静态资源(如 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 Security 来保护我的应用程序....但是对于对这些静态资源的请求,我不希望创建或验证会话。

【问题讨论】:

    标签: spring session spring-security


    【解决方案1】:

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

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

    HttpServletRequest req = ((HttpServletRequest) request);
    HttpSession session = req.getSession();
    

    我用它来检查会话是否已经存在以及是否需要刷新它(这里解释得太复杂了)。但是methodreq.getSession() 将创建一个新会话,如果它不存在的话。我没有意识到它有这种副作用。

    相反,我应该打电话给:req.getSession(false)。如果会话已经存在,这将返回一个会话,如果不存在则返回 null。

    【讨论】:

      猜你喜欢
      • 2018-04-01
      • 1970-01-01
      • 2017-05-20
      • 2020-02-22
      • 2019-08-02
      • 2019-09-03
      • 1970-01-01
      • 2017-09-17
      • 2020-10-21
      相关资源
      最近更新 更多