我需要在我的Spring Security conf中禁用高速緩存控制標頭。禁用Spring Security標題不起作用
根據文檔的簡單http.headers.disable()
應該這樣做,但我還是看到響應
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Expires:0
Pragma:no-cache
頭。
我當前的安全配置是:
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
頭,所有的緩存頭的地方後寫這過濾器無權訪問「覆蓋」它們。即使我在安全過濾器鏈的最後位置添加此過濾器,也會發生這種情況。
使用攔截
我嘗試了以下攔截器:
@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", "");
}
這(相當可預見的)剛剛添加的頭,這意味着原來no-cache
頭仍出現在除了加入的影片由攔截者。
我在我的智慧結束在這裏。我如何擺脫由Spring安全設置的緩存控制頭?
你的假設是錯誤的。對於已有配置的子路徑,我沒有多個安全配置。我確實有其他路徑的其他安全配置(並非與「/ mypath」有關),但這些應該不是真正的問題,我不需要在那裏禁用頭文件。 – j0ntech