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。給出下面是回應的標題影像。請檢查。
在此處輸入影像描述
uj5u.com熱心網友回復:
報告病例的可能原因是
Spring 安全性可能不會出現在圖片中。確保您正在檢查準確的模式(受彈簧安全保護的 api)
Web 服務器正在過濾設定的標頭。當您使用
Nginx時,您應該檢查服務器上的任何錯誤配置。
此外,
Strict-Transport-Security僅在 HTTPS 請求上添加,因此以下內容僅與 https 相關。
.httpStrictTransportSecurity()
.maxAgeInSeconds(31536000)
.includeSubDomains(true);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/439935.html
上一篇:為什么注入bean后得到空值?
