0
  • I have a spring boot application
  • I would like to enable HSTS

I added the documented settings to my SecurityConfiguration (see below), but HSTS header is not appearing.

What am I doing wrong?

@Slf4j
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.headers()
                .httpStrictTransportSecurity()
                .includeSubDomains(true)
                .preload(false)
                .maxAgeInSeconds(31536000);

I can't find the header in Chrome Developer Tools (F12 -> Network -> Headers -> Response Header) when accessing the page nor when testing with official site https://gf.dev/hsts-test

enter image description here

mcfly soft
  • 11,289
  • 26
  • 98
  • 202

1 Answers1

2

By default, Spring Security only adds the HSTS header if the connection is secure (https). See the documentation.

However, you can change the RequestMatcher as you want, like so:

http.headers()
                .httpStrictTransportSecurity()
                .includeSubDomains(true)
                .preload(false)
                .maxAgeInSeconds(31536000)
                .requestMatcher(AnyRequestMatcher.INSTANCE);