【问题标题】:Add ShallowEtagHeaderFilter in Spring Boot MVC在 Spring Boot MVC 中添加 ShallowEtagHeaderFilter
【发布时间】:2014-12-31 18:14:24
【问题描述】:

我正在尝试调整我的应用程序配置以设置 ETag 支持。

我刚刚检查了this SO 问题,所以让我说一下我的代码与它的不同之处:

  1. 我不使用任何 xml 配置文件。
  2. 我为系统的各个方面使用了不同的配置类。我的WebConfig 看起来像这样:
@Configuration @EnableAutoConfiguration @ComponentScan(basePackages = { "xxx", "yyy" }) public class WebConfig extends WebMvcConfigurerAdapter { @Bean public Filter shallowETagHeaderFilter() { return new ShallowEtagHeaderFilter(); } ... }
  1. 我的 SecurityConfig 如下所示:
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { ... @Override protected void configure(final HttpSecurity http) throws Exception { http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and().exceptionHandling() .authenticationEntryPoint(authenticationEntryPoint()) .and().authorizeRequests() .antMatchers(HttpMethod.GET, "/**").authenticated() .antMatchers(HttpMethod.POST, "/**").authenticated() .antMatchers(HttpMethod.HEAD, "/**").authenticated() .and().csrf().disable() .addFilterBefore(authenticationTokenProcessingFilter(), UsernamePasswordAuthenticationFilter.class); } }
  1. 我还有一个初始化类,它是空的:
@Order(value=1) public class SecurityWebAppInitializer extends AbstractSecurityWebApplicationInitializer { }

我没有看到 ShallowEtagHeaderFilter 被添加到默认链或任何其他地方,我如何在此设置中使用它?

【问题讨论】:

  • 你的问题/问题是? Spring Boot 应该会自动添加过滤器。您的初始化程序未使用,因此不会执行或添加任何内容。 Spring Boot 已启用 Spring Security,因此不需要您的 @EnableWebSecurity。
  • 好吧,在这种情况下它不会自动设置它。如何修改我的初始化程序以启动 ShallowEtagHeaderFilter?
  • 你不能因为初始化程序在 Spring Boot 应用程序中没有做任何事情。
  • 我真的没有关注。所以我不应该使用初始化器?
  • 您是否阅读了 Spring Boot 文档并查看了示例?它会创建一个可执行的 jar/war,或者如果您想将其部署为一个 war,而不是一个可执行的 jar 文件,则需要一个自定义初始化程序。

标签: spring annotations spring-boot servlet-filters etag


【解决方案1】:

好的,

据this发帖:

[...] 为了帮助缓解这种情况,Spring Security 添加了缓存控制支持,它将在您的响应中插入以下标头。

Cache-Control: no-cache, no-store, max-age=0, must-revalidate

编译指示:无缓存

过期:0

所以,发生的事情是添加了 ETag 支持,但 Spring Security 在响应中使其无效。看来如果要同时使用 Spring Security 和 ETag 支持,则需要声明以下代码行(箭头突出显示):

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    ...

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and().exceptionHandling()
                    .authenticationEntryPoint(authenticationEntryPoint())
            .and().authorizeRequests()
                    .antMatchers(HttpMethod.GET, "/**").authenticated()
                    .antMatchers(HttpMethod.POST, "/**").authenticated()
                    .antMatchers(HttpMethod.HEAD, "/**").authenticated()
        .and().csrf().disable()    
        .addFilterBefore(authenticationTokenProcessingFilter(), UsernamePasswordAuthenticationFilter.class);
        ===> http.headers().cacheControl().disable();
    }

}

【讨论】:

    猜你喜欢
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2017-02-22
    • 2021-02-09
    • 2023-03-31
    • 1970-01-01
    • 2019-01-30
    相关资源
    最近更新 更多