How to give request matcher in Spring Security for x frame options?

孤者浪人 提交于 2019-12-31 04:06:07

问题


I have enabled Spring Security headers.

My code is like this:

<security:headers disabled="false">
       <security:content-security-policy policy-directives="script-src  'self' 'unsafe-inline' 'unsafe-eval'" />
       <security:cache-control disabled="true"/>
</security:headers>

By default X-FRAME-OPTIONS is DENY. But some requests I should enable X-FRAME-OPTIONS as SAMEORIGIN. How to do?


回答1:


You can use a DelegatingRequestMatcherHeaderWriter, see Spring Security Reference:

20.2.3 DelegatingRequestMatcherHeaderWriter

At times you may want to only write a header for certain requests. For example, perhaps you want to only protect your log in page from being framed. You could use the DelegatingRequestMatcherHeaderWriter to do so. When using the XML namespace configuration, this can be done with the following:

<http>
  <!-- ... -->

  <headers>
      <frame-options disabled="true"/>
      <header ref="headerWriter"/>
  </headers>
</http>

<beans:bean id="headerWriter"
class="org.springframework.security.web.header.writers.DelegatingRequestMatcherHeaderWriter">
  <beans:constructor-arg>
      <bean class="org.springframework.security.web.util.matcher.AntPathRequestMatcher"
        c:pattern="/login"/>
  </beans:constructor-arg>
  <beans:constructor-arg>
      <beans:bean
        class="org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter"/>
  </beans:constructor-arg>
</beans:bean>

To use DENYand SAMEORIGIN for different URLs you have to add two header elements with two different DelegatingRequestMatcherHeaderWriter.



来源:https://stackoverflow.com/questions/42111346/how-to-give-request-matcher-in-spring-security-for-x-frame-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!