舞台裏

Qiita が表でこっちが裏。こっそりやっていく。

20.2.3 DelegatingRequestMatcherHeaderWriter

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.

あなたは DelegatingRequestMatcherHeaderWriter を使うことでそれができます。

When using the XML namespace configuration, this can be done with the following:

XML namespace configuration を使っている場合は、次のようにすることで実現できます。

<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>

We could also prevent framing of content to the log in page using java configuration:

私たちは、 Java Configuration を使ってログインページのコンテンツをフレーム化することを防ぐこともできます。

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    RequestMatcher matcher = new AntPathRequestMatcher("/login");
    DelegatingRequestMatcherHeaderWriter headerWriter =
        new DelegatingRequestMatcherHeaderWriter(matcher,new XFrameOptionsHeaderWriter());
    http
    // ...
    .headers()
        .frameOptions().disabled()
        .addHeaderWriter(headerWriter);
}
}