2016-03-15 90 views
15

我需要在我的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安全設置的緩存控制頭?

回答

-2

你是正確的,使用

http 
    .headers().disable() 
    ... 

將禁用您的頭。如果你只是想緩存控制禁用,您可以使用以下命令:

http 
    .headers() 
     .cacheControl().disable() 
     .and() 
    ... 

我已經發布a sample演示沿着with a test這方面的工作。

我的猜測是您遇到的問題是您有多個HttpSecurity配置。請記住,如果您有:

http 
    .antMatchers("/myPath/**") 
    ... 

/myPath/開始只有網址會受到影響。另外,如果您有多個HttpSecurity實例,則每個HttpSecurity實例將按順序進行考慮,並且只使用第一個HttpSecurity實例。例如,如果您有:

@Configuration 
@Order(1) 
public class MyPathAdminWebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .antMatchers("/myPath/admin/**") 
      .authorizeRequests() 
       .anyRequest().hasRole("ADMIN"); 
    } 
} 

@Configuration 
@Order(2) 
public class MyPathWebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .antMatchers("/myPath/**") 
      .headers() 
       .cacheControl().disable(); 
    } 
} 

@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .anyRequest().authenticated(); 
    } 
} 

如果請求/ mypath中/管理/ ABC

首先MyPathAdminWebSecurityConfig考慮。由於/myPath/admin//myPath/admin/開頭,我們將使用MyPathAdminWebSecurityConfig而不考慮任何其他配置。這意味着您將希望爲此請求獲取標題。

+0

你的假設是錯誤的。對於已有配置的子路徑,我沒有多個安全配置。我確實有其他路徑的其他安全配置(並非與「/ mypath」有關),但這些應該不是真正的問題,我不需要在那裏禁用頭文件。 – j0ntech

1

您將需要一個擴展WebSecurityConfigurerAdapter的類,並使用兩種超級配置方法來配置過濾器和身份驗證提供程序。例如,在最低限度以下工作:

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; 
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; 

@Configuration 
@EnableWebMvcSecurity 
@EnableGlobalMethodSecurity(securedEnabled = true) 
public class SecurityConfigDemo extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private DemoAuthenticationProvider demoAuthenticationProvider; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception {   

    // Prevent the HTTP response header of "Pragma: no-cache". 
    http.headers().cacheControl().disable(); 

    } 

    @Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception {   
     auth.authenticationProvider(demoAuthenticationProvider);   
    }  

} 

您也可以完全disabe Spring Security進行公共靜態資源如下(在同一個班以上):

@Override 
public void configure(WebSecurity web) throws Exception { 
    web.ignoring().antMatchers("/static/public/**"); 
} 

這需要配置兩個資源處理程序來獲得緩存控制頭右側:

@Configuration 
public class MvcConfigurer extends WebMvcConfigurerAdapter 
     implements EmbeddedServletContainerCustomizer { 
    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     // Resources without Spring Security. No cache control response headers. 
     registry.addResourceHandler("/static/public/**") 
      .addResourceLocations("classpath:/static/public/"); 

     // Resources controlled by Spring Security, which 
     // adds "Cache-Control: must-revalidate". 
     registry.addResourceHandler("/static/**") 
      .addResourceLocations("classpath:/static/") 
      .setCachePeriod(3600*24); 
    } 
} 
+0

它沒有工作... –

0

所以,我發現自己的答案: 我終於做出了Cache-Control頭通過創建我的陽明配置文件的新條目,以改變它的值稱爲spring.resources.cachePeriod並將其設置爲不同的值比0。壞消息是所有的資源都使用這個設置,所以根據資源情況,沒有辦法讓它變得不同,據我所知。

this question的答案幫了很大忙。

0

所以我有一個類似的問題,希望我的大多數REST端點有標準的「不要緩存我」頭Spring是注入,但在一個端點上,我想插入我自己的。

在給ResponseEntry的HttpHeaders對象中指定自己的函數不起作用。

什麼工作是直接在HttpServletResponse上直接設置標頭。

Spring正在設置「Cache-Control」,「Pragma」和「Expires」。下面演示瞭如何覆蓋和1分鐘的高速緩存設置:

response.setHeader("Cache-Control", "max-age=60"); 
response.setHeader("Pragma", ""); 
HttpHeaders headers = new HttpHeaders(); 
headers.setExpires(System.currentTimeMillis()+60000); 
return new ResponseEntity<>(body, headers, HttpStatus.OK); 
0

我在我的應用程序類實現ID連接通過@EnableOAuth2Sso後出現這個問題。約六小時的調試,並通過文檔閱讀後,原來這個@EnableOAuth2Sso必須放在WebSecurityConfigurerAdapter或自定義設置將被默認設置所覆蓋:

// In Application.java 
@SpringBootApplication 
public class Application { 
    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

//In WebSecurity.java 
@Configuration 
@EnableOAuth2Sso // <- This MUST be here, not above 
public class WebSecurity extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.headers().disable(); 
    } 
} 

// In Application.java 
@SpringBootApplication 
@EnableOAuth2Sso // <- Will overwrite config below 
public class Application { 
    //... 
} 

@Configuration 
public class WebSecurity extends WebSecurityConfigurerAdapter { 
    //... 
}