舞台裏

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

20.2 Custom Headers, 20.2.1 Static Headers

20.2 Custom Headers

Spring Security has mechanisms to make it convenient to add the more common security headers to your application.

Spring Security はより共通のセキュリティヘッダーをアプリケーションに追加することを便利にするメカニズムを持ちます。

However, it also provides hooks to enable adding custom headers.

しかしながら、それはカスタムヘッダーを追加できるフックを提供することでもあります。

20.2.1 Static Headers

There may be times you wish to inject custom security headers into your application that are not supported out of the box.

ときどき、あなたはサポートされていないカスタムのセキュリティヘッダーをアプリケーションに追加したいと思うでしょう。

For example, given the following custom security header:

例えば、以下のカスタムのセキュリティヘッダーを提供します。

X-Custom-Security-Header: header-value

When using the XML namespace, these headers can be added to the response using the <header> element as shown below:

XML namespace を使っている場合は、レスポンスにヘッダーを追加するには <header> 要素を次のように使用します。

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

    <headers>
        <header name="X-Custom-Security-Header" value="header-value"/>
    </headers>
</http>

Similarly, the headers could be added to the response using Java Configuration as shown in the following:

同様に、 Java Configuration でレスポンスにヘッダーを追加する場合は次のようにします。

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    // ...
    .headers()
        .addHeaderWriter(new StaticHeadersWriter("X-Custom-Security-Header","header-value"));
}
}