添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Spring Security provides a default set of Security HTTP Response Headers to provide secure defaults. While each of these headers are considered best practice, it should be noted that not all clients use the headers, so additional testing is encouraged.

You can customize specific headers. For example, assume that you want the defaults but you wish to specify SAMEORIGIN for X-Frame-Options .

You can do so with the following configuration:

Customize Default Security Headers
.headers(headers -> headers .frameOptions(frameOptions -> frameOptions .mode(Mode.SAMEORIGIN) return http.build(); fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { frameOptions { mode = Mode.SAMEORIGIN

If you do not want the defaults to be added and want explicit control over what should be used, you can disable the defaults:

Disable HTTP Security Response Headers
fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { disable()

However, if you actually want to cache specific responses, your application can selectively add them to the ServerHttpResponse to override the header set by Spring Security. This is useful to ensure that such things as CSS, JavaScript, and images are properly cached.

When using Spring WebFlux, you typically do so within your configuration. You can find details on how to do so in the Static Resources portion of the Spring Reference documentation.

If necessary, you can also disable Spring Security’s cache control HTTP response headers.

Cache Control Disabled
fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { cache { disable() // ... .headers(headers -> headers .contentTypeOptions(contentTypeOptions -> contentTypeOptions.disable()) return http.build(); fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { contentTypeOptions { disable()

By default, Spring Security provides the Strict Transport Security header. However, you can customize the results explicitly. For example, the following example explicitly provides HSTS:

Strict Transport Security
fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { hsts { includeSubdomains = true preload = true maxAge = Duration.ofDays(365)

By default, Spring Security disables rendering within an iframe by using X-Frame-Options .

You can customize frame options to use the same origin:

X-Frame-Options: SAMEORIGIN
fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { frameOptions { mode = SAMEORIGIN

By default, Spring Security instructs browsers to disable the XSS Auditor by using <<headers-xss-protection,X-XSS-Protection header>. You can disable the X-XSS-Protection header entirely:

X-XSS-Protection Customization
fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { xssProtection { disable() // ... .headers(headers -> headers .xssProtection(xssProtection -> xssProtection.headerValue(XXssProtectionServerHttpHeadersWriter.HeaderValue.ENABLED_MODE_BLOCK)) return http.build(); fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { xssProtection { headerValue = XXssProtectionServerHttpHeadersWriter.HeaderValue.ENABLED_MODE_BLOCK

By default, Spring Security does not add Content Security Policy , because a reasonable default is impossible to know without the context of the application. The web application author must declare the security policies to enforce and/or monitor for the protected resources.

For example, consider the following security policy:

Content Security Policy Example
Content-Security-Policy: script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/

Given the preceding policy, you can enable the CSP header:

Content Security Policy
.headers(headers -> headers .contentSecurityPolicy(policy -> policy .policyDirectives("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/") return http.build(); fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { contentSecurityPolicy { policyDirectives = "script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/" .headers(headers -> headers .contentSecurityPolicy(policy -> policy .policyDirectives("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/") .reportOnly() return http.build(); fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { contentSecurityPolicy { policyDirectives = "script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/" reportOnly = true

By default, Spring Security does not add Referrer Policy headers. You can enable the Referrer Policy header using configuration as shown below:

Referrer Policy Configuration
.headers(headers -> headers .referrerPolicy(referrer -> referrer .policy(ReferrerPolicy.SAME_ORIGIN) return http.build(); fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { referrerPolicy { policy = ReferrerPolicy.SAME_ORIGIN

By default, Spring Security does not add Feature Policy headers. Consider the following Feature-Policy header:

Feature-Policy Example
Feature-Policy: geolocation 'self'

You can enable the preceding Feature Policy header:

Feature-Policy Configuration
fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { featurePolicy("geolocation 'self'")

By default, Spring Security does not add Permissions Policy headers. Consider the following Permissions-Policy header:

Permissions-Policy Example
Permissions-Policy: geolocation=(self)

You can enable the preceding Permissions Policy header:

Permissions-Policy Configuration
.headers(headers -> headers .permissionsPolicy(permissions -> permissions .policy("geolocation=(self)") return http.build(); fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { return http { // ... headers { permissionsPolicy { policy = "geolocation=(self)"

By default, Spring Security does not add Clear-Site-Data headers. Consider the following Clear-Site-Data header:

Clear-Site-Data Example
Clear-Site-Data: "cache", "cookies"

You can send the Clear-Site-Data header on logout:

Clear-Site-Data Configuration
@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
	ServerLogoutHandler securityContext = new SecurityContextServerLogoutHandler();
	ClearSiteDataServerHttpHeadersWriter writer = new ClearSiteDataServerHttpHeadersWriter(CACHE, COOKIES);
	ServerLogoutHandler clearSiteData = new HeaderWriterServerLogoutHandler(writer);
	DelegatingServerLogoutHandler logoutHandler = new DelegatingServerLogoutHandler(securityContext, clearSiteData);
		// ...
		.logout()
			.logoutHandler(logoutHandler);
	return http.build();
@Bean
fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
    val securityContext: ServerLogoutHandler = SecurityContextServerLogoutHandler()
    val writer = ClearSiteDataServerHttpHeadersWriter(CACHE, COOKIES)
    val clearSiteData: ServerLogoutHandler = HeaderWriterServerLogoutHandler(writer)
    val customLogoutHandler = DelegatingServerLogoutHandler(securityContext, clearSiteData)
    return http {
        // ...
        logout {
            logoutHandler = customLogoutHandler
        

© VMware, Inc. or its affiliates. Terms of UsePrivacyTrademark Guidelines Thank youYour California Privacy RightsCookie Settings

Apache®, Apache Tomcat®, Apache Kafka®, Apache Cassandra™, and Apache Geode™ are trademarks or registered trademarks of the Apache Software Foundation in the United States and/or other countries. Java™, Java™ SE, Java™ EE, and OpenJDK™ are trademarks of Oracle and/or its affiliates. Kubernetes® is a registered trademark of the Linux Foundation in the United States and other countries. Linux® is the registered trademark of Linus Torvalds in the United States and other countries. Windows® and Microsoft® Azure are registered trademarks of Microsoft Corporation. “AWS” and “Amazon Web Services” are trademarks or registered trademarks of Amazon.com Inc. or its affiliates. All other trademarks and copyrights are property of their respective owners and are only mentioned for informative purposes. Other names may be trademarks of their respective owners.