Включить HTTP Strict Transport Security (HSTS) с приложением весенней загрузки

Я ознакомился со статьей https://docs.spring.io/spring-security/site/docs/4.0.2.RELEASE/reference/html/headers.html чтобы включить заголовок HSTS в моем весеннем загрузочном приложении. Несмотря на внесение необходимых изменений, Strict-Transport-Security в заголовке не появляется ответ.

pom.xml (зависимости)

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>
</dependencies>

WebSecurityConfig.java

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .headers()
            .httpStrictTransportSecurity()
                .includeSubDomains(true)
                .maxAgeInSeconds(31536000);
    }
}

Список заголовков:

cache-control →no-cache, no-store, max-age=0, must-revalidate
content-language →en-GB
content-type →text/html;charset=UTF-8
date →Thu, 24 May 2018 14:10:29 GMT
expires →0
pragma →no-cache
transfer-encoding →chunked
x-application-context →application:9000
x-content-type-options →nosniff
x-frame-options →SAMEORIGIN
x-xss-protection →1; mode=block

Я что-то пропустил?

3 ответа

В соответствии с RFC6797 заголовок HSTS вводится только в ответы HTTPS.

Источник: https://docs.spring.io/spring-security/site/docs/4.0.2.RELEASE/reference/htmlsingle/

Как упоминалось в других ответах, по умолчанию RequestMatcherиспользуется в HstsConfigпроверяет, является ли запрос HTTPS. Вы можете установить другой сопоставитель, если он не работает для вас, потому что TLS не прерывается Spring Boot.

Приведенный ниже код гарантирует, что Strict-Transport-Securityзаголовок установлен во всех ответах:

          http.headers()
          .httpStrictTransportSecurity()
          .requestMatcher(AnyRequestMatcher.INSTANCE)
          ...

Он появится только после первого запроса по HTTPS.

Другие вопросы по тегам