【问题标题】:Spring Security caching my authenticationSpring Security 缓存我的身份验证
【发布时间】:2018-11-23 07:58:44
【问题描述】:

我刚刚在我的 Spring Boot 项目类路径中添加了 Spring Security。我没有进行 Java 配置或 XML 配置。

问题是当我向我的资源 localhost:8080/users 发送请求时,我的第一个请求通常会通过 (通过基本身份验证) 进行身份验证,但随后的请求不需要任何身份验证标头。即使我重新启动服务器,请求仍在验证中,无需输入任何凭据。

我想关闭这个“缓存”。

我尝试过很多客户。邮递员、SOAP-UI、浏览器..已经阅读 this,但没有用

【问题讨论】:

  • 如果确实存在缓存,它的生命周期将仅延伸到应用程序的生命周期。您确定不是每次都发送标头吗?
  • @Makoto 是的,我确定。我不是每次都发送标题
  • 用于cookies中的JSESSIONID。

标签: java spring spring-security


【解决方案1】:

您必须将会话创建策略设置为 STATELESS。否则 Spring 安全性将使用 cookie。

(您可以在 Postman 中发送按钮下方的 cookie 菜单中删除 cookie。)

示例配置:

@Configuration
@EnableWebSecurity(debug = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement().sessionCreationPolicy(
                   SessionCreationPolicy.STATELESS)
                .and()
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .httpBasic();
    }

    ...

}

【讨论】:

    【解决方案2】:

    我的开箱即用弹簧执行器遇到了这个问题。

    我必须添加:.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 到我的ActuatorSecurityConfig 班级。

    package com.foo;
    import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
    import org.springframework.boot.actuate.context.ShutdownEndpoint;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.config.http.SessionCreationPolicy;
    
    @Configuration
    public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .csrf().disable()
                    .authorizeRequests()
                    .requestMatchers(EndpointRequest.to(ShutdownEndpoint.class))
                    .denyAll()
                    .requestMatchers(EndpointRequest.toAnyEndpoint())
                    .hasRole("ACTUATOR_ADMIN")
                    .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
                    .permitAll()
                    .antMatchers("/foo/**")
                    .permitAll()
                    .antMatchers("/**")
                    .authenticated()
                    .and()
                    .httpBasic()
                    .and()
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    
        }
    }
    

    【讨论】:

      【解决方案3】:

      对于那些在 spring xml 中配置了安全性的幸运儿,这个也激活了无状态安全性:

      <http create-session="stateless">...</http>

      【讨论】:

        猜你喜欢
        • 2016-06-18
        • 1970-01-01
        • 2022-01-15
        • 2014-02-26
        • 2013-01-15
        • 2019-08-25
        • 2013-10-30
        • 2014-04-07
        • 2012-02-18
        相关资源
        最近更新 更多