我正在尝试调整我的应用程序配置以设置 ETag 支持。
我刚刚检查过this https://stackoverflow.com/questions/26151057/add-a-servlet-filter-in-a-spring-boot-application/26157610所以问题来了,让我说一下我的代码与它的不同之处:
- 我不使用任何 xml 配置文件。
- 我为系统的每个方面使用不同的配置类。我的
WebConfig
看起来像这样:
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = { "xxx", "yyy" })
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public Filter shallowETagHeaderFilter() {
return new ShallowEtagHeaderFilter();
}
...
}
- 我的 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);
}
}
- 我还有一个初始化类,它是空的:
@Order(value=1)
public class SecurityWebAppInitializer extends AbstractSecurityWebApplicationInitializer {
}
我没有看到任何地方ShallowEtagHeaderFilter
已添加到默认链或其他内容中,我如何在此设置中使用它?
Alright,
根据this http://docs.spring.io/spring-security/site/docs/3.2.0.CI-SNAPSHOT/reference/html/headers.html post:
[...]为了帮助缓解这种情况,Spring Security 添加了缓存控制支持,它将在您的响应中插入以下标头。
缓存控制:无缓存、无存储、max-age=0、必须重新验证
编译指示:无缓存
过期时间: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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)