2017-03-07 16 views
2

캐싱에 대한 일부 URL 패턴을 필터링하려고합니다. 내가 시도한 것은 WebSecurityConfigurerAdapter 구현에 일부 코드를 넣는 것입니다.스프링 보안 - 특정 URL 패턴에서 캐시 제어를 제거하는 방법

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

    // For cache 
    http.headers().defaultsDisabled() 
      .cacheControl() 
      .and().frameOptions(); 

    securityConfigService.configure(http,this); 
} 

그러나이 코드는 모든 웹 응용 프로그램에 적용됩니다. 특정 URL 또는 Content-Type (images)에 어떻게 적용 할 수 있습니까?

이미 RegexRequestMatcher으로 시도했지만 나에게 적합하지 않습니다. SpringSecurityResponseHeaders을하지만,이 경우에 대한 샘플이 없습니다 :

// For cache 
     http.requestMatcher(new RegexRequestMatcher("/page/", "GET")) 
       .headers().defaultsDisabled() 
       .cacheControl() 
       .and().frameOptions(); 

나는이 기사를 읽어 보시기 바랍니다.

감사합니다.

P. 즉, 특정 URL 및 리소스에 대해 SpringSecurity defaults을 삭제하려고합니다.

+0

아마도 귀하의 질문을 잘 이해하지 못합니다. 일반적인 요청에 대한 캐시 컨트롤을 추가하기를 원하기 때문에 스프링 보안이 왜 이와 관련되어 있습니까? 이 필터를 추가 할 수 있습니까? – Simon

+0

@Simon Security 기본 설정은 일반 호출에 유효해야하지만'image/* '유형에 대해서만이 옵션을 원합니다. 그래서 그 옵션을 구분하고 싶습니다. –

+0

나는이 점을 이해하지만, 나에게이 필터는 매우 간단하다. 필터링 할 url 패턴 (당신의 경우 image/*)을 설정할 수있다. 응답에 일부 헤더 만 설정하면 모든 것이 설정되어야합니다. 당신이 Spring Security를 ​​선호한다면, 필터 체인이 있습니다. 마지막 필터 다음에 하나의 필터를 추가하고 응답을 설정하십시오. – Simon

답변

1

여러 개의 WebSecurityConfigurerAdapter가있는 것은 어떻습니까? 하나의 어댑터는 특정 URL에 대한 캐시 제어를 가질 수 있고 다른 어댑터는 해당 URL에 대해 캐시 제어를 사용할 수 없습니다.

+1

이것은 다른 사람들을 돕기위한 추가 정보입니다. 여러 개의 'WebSecurityConfigurerAdapter'를 가지려면 구현물의'@ Order '를 변경해야합니다. 기본값은 숨겨져 있지만 값은 '100'이며 중복되어서는 안됩니다. 이 [doc] (http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter.html)을 참조 할 수 있습니다. –

0

Filter으로 해결했습니다. 아래는 제 구현의 AbstractAnnotationConfigDispatcherServletInitializer입니다. onStartup 메서드 오버라이드. 내가 무슨 짓을

FilterRegistration.Dynamic springSecurityFilterChain = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy()); 
if(springSecurityFilterChain != null){ 
    springSecurityFilterChain.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/render/*", "/service/*"); 
    // I removed pattern url "/image/*" :) 
} 

는 MappingUrlPatterns에서 /image/*을 제거합니다. 답변 해 주셔서 감사합니다.

+1

있습니다. 이것을하는 또 다른 규칙적인 방법. ['WebSecurity # ignoring'] (http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/config/annotation/web/builders/WebSecurity.html#ignoring)을 사용하십시오. -). 고마워요, – dur

+0

. 그게 내가 원하는거야. bb –