gpt4 book ai didi

java - 禁用 Spring Security header 不起作用

转载 作者:太空狗 更新时间:2023-10-29 22:47:49 24 4
gpt4 key购买 nike

我需要在我的 Spring Security conf 中禁用缓存控制 header 。

根据文档,一个简单的 http.headers.disable() 应该可以做到,但我仍然看到了

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

响应中的 header 。

我当前的安全配置是:

http.antMatcher("/myPath/**") // "myPath" is of course not the real path
.headers().disable()
.authorizeRequests()
// ... abbreviated
.anyRequest().authenticated();

到目前为止我尝试过的事情:

application.properties

我添加了 security.headers.cache=false 行,但这没有任何区别。

使用过滤器

我尝试了以下过滤器:

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(request, new HttpServletResponseWrapper((HttpServletResponse) response) {
@Override
public void setHeader(String name, String value) {
if (name.equalsIgnoreCase("Cache-Control")) {
value = "";
} else if (name.equalsIgnoreCase("Expires")) {
value = "";
} else if (name.equalsIgnoreCase("Pragma")) {
value = "";
}
super.setHeader(name, value);
}
});
}

添加日志记录后,我看到此过滤器仅写入 X-XSS-Protection header ,所有缓存 header 都稍后写入某处,并且此过滤器无权“覆盖”它们。即使我将此过滤器添加到安全过滤器链的最后位置,也会发生这种情况。

使用拦截器

我尝试了以下拦截器:

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
String requestUri = request.getRequestURI();
response.setHeader("Cache-Control", "max-age=3600");
response.setHeader("Expires", "3600");
response.setHeader("Pragma", "");
}

这(完全可以预见)只是添加了 header ,这意味着除了拦截器添加的 header 之外,原始的 no-cache header 仍然出现。

我已经无计可施了。如何去掉 Spring security 设置的缓存控制 header ?

最佳答案

可能会有帮助:

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
// ...
.headers()
.defaultsDisabled()
.cacheControl();
}
}

http://docs.spring.io/spring-security/site/docs/current/reference/html/headers.html#headers-cache-control

关于java - 禁用 Spring Security header 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36006029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com