【问题标题】:How to add X-Frame-Options to just some responses in Spring Security 3.2如何将 X-Frame-Options 添加到 Spring Security 3.2 中的一些响应中
【发布时间】:2015-01-17 04:34:49
【问题描述】:
我想在我的 Spring 应用程序中将 X-Frame-Options 标头添加到除某些页面之外的所有页面。 Spring Security 3.2 提供了很好的功能,可以通过 <headers> <frame-options /> </headers> 配置将该标头添加到所有响应中。
但是是否可以从某些路径中排除此标头?我考虑子类化XFrameOptionsHeaderWriter 并在里面做一些路径正则表达式匹配,但看起来有点难看。也许有更方便的方法来完成这个?
【问题讨论】:
标签:
spring
spring-security
x-frame-options
【解决方案1】:
我发现了如何使用 XML 配置:
<http>
<headers>
<header ref="xFrameOptionsHeaderWriter" />
</headers>
</http>
<beans:bean id="xFrameOptionsHeaderWriter" class="org.springframework.security.web.header.writers.DelegatingRequestMatcherHeaderWriter">
<!-- Argument 1: RequestMatcher. This matcher will match all but some paths. -->
<beans:constructor-arg>
<beans:bean class="org.springframework.security.web.util.matcher.NegatedRequestMatcher">
<beans:constructor-arg>
<beans:bean class="org.springframework.security.web.util.matcher.OrRequestMatcher">
<beans:constructor-arg>
<beans:list>
<beans:bean class="org.springframework.security.web.util.matcher.AntPathRequestMatcher" c:pattern="/**/some-path/**" />
<beans:bean class="org.springframework.security.web.util.matcher.AntPathRequestMatcher" c:pattern="/**/another-path/**" />
</beans:list>
</beans:constructor-arg>
</beans:bean>
</beans:constructor-arg>
</beans:bean>
</beans:constructor-arg>
<!-- Argument 2: HeaderWriter -->
<beans:constructor-arg>
<beans:bean class="org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter" c:frameOptionsMode="SAMEORIGIN" />
</beans:constructor-arg>
</beans:bean>