java
spring security headers frameOptions 선택적 disable
고.니
2019. 3. 19. 23:30
반응형
Spring Boot 환경에서 Spring security를 사용하고 있습니다.
원래 ...headers().frameOptions().sameOrigin() 을 기본으로 전체 적용해서 사용하고 있었습니다.
그런데 외부에 iframe에 들어가야하는 페이지가 하나 추가적으로 필요하게 되었습니다.
그냥 url 1개만 X-Frame-Options 헤더를 제거하면 되는데 이게 생각처럼 쉽지가 않았습니다.
아무리 구글링해도 명쾌한 해답이 없었습니다.
무수한 삽질 끝에 regexMatcher 를 이용해 URL 기반으로 Spring security 설정을 분리할 수 있었습니다.
/link/iframe 이라는 URL 만 X-Frame-Options 헤더를 제거하는 코드 샘플입니다.
@EnableWebSecurity @Configuration public class DefaultHttpSecurityConfig extends WebSecurityConfigurerAdapter { ... @Override protected void configure(HttpSecurity http) throws Exception { http.antMatcher("/link/iframe")// // --------------- .authorizeRequests()// // .antMatchers("/link/iframe").permitAll()// .and().headers().frameOptions().disable(); http.regexMatcher("^((?!" + "/WEB-INF/jsp/link/iframe.jsp" + ").)*$") // --------------- .authorizeRequests()// // .antMatchers(// ... ).permitAll()// .anyRequest().authenticated()// .and().headers().frameOptions().sameOrigin()// ... // --------------- ; ... } ... } |
특이사항으로 regexMatcher는 url이 아닌 view 실제 경로로 비교를 합니다.
org.springframework.security.web.util.matcher.RegexRequestMatcher 클래서의 matches 메서드에서 확인할 수 있습니다.
당연히 URL 비교로 생각하고 이 부분에서 삽질을 많이 했네요. ( 도대체 왜 view파일로 비교하는거지 ㅡㅡ? )
반응형