package com.example.security;
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;
import org.springframework.security.web.header.writers.ReferrerPolicyHeaderWriter;
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.headers()
.referrerPolicy(ReferrerPolicyHeaderWriter.ReferrerPolicy.SAME_ORIGIN).and()
.frameOptions().sameOrigin().and()
.headers().defaultsDisabled()
.contentTypeOptions().and()
.httpStrictTransportSecurity()
.includeSubDomains(true)
.maxAgeInSeconds(31536000);
}
}
我已经添加了这个类,但我仍然没有在API响应中得到任何给定的上面的头。我还尝试在提到所有API的类上添加@EnableWebSecurity。下面给出的是响应的标题图像。请检查。
在此处输入图像描述
报告病例的可能原因是
-
Spring安全性可能不会出现在画面中。确保你检查准确的模式(受spring安全保护的api(
-
Web服务器正在筛选集合标头。由于您正在使用
Nginx
,因此您应该检查服务器上是否存在任何错误配置。
此外,Strict-Transport-Security
仅添加在HTTPS请求上,因此以下内容仅与HTTPS相关。
.httpStrictTransportSecurity()
.maxAgeInSeconds(31536000)
.includeSubDomains(true);