Spring 安全性 header 未反映在 API 响应中

Spring Security header are not reflecting in API response

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);
    }
}

我已经添加了这个 class 但我仍然没有在 API 响应中得到任何高于 headers 的信息。 我还尝试在提到所有 API 的 class 上添加 @EnableWebSecurity。 下面给出的是 headers 作为回应的图片。请检查。

enter image description here

报告的病例可能的原因是

  • Spring 安全性可能不会出现。确保你是 检查准确的模式(api 受 spring 安全保护)

  • Web 服务器正在过滤集合 headers。由于您正在使用 Nginx,因此您应该检查服务器上是否存在任何错误配置。

此外, Strict-Transport-Security 仅在 HTTPS 请求中添加,因此以下仅与 https 相关。

.httpStrictTransportSecurity()
.maxAgeInSeconds(31536000)
.includeSubDomains(true);