SpringSecurity系列——安全Http响应头day8-2(源于官网5.7.2版本)

SpringSecurity系列——安全Http响应头day8-2(源于官网5.7.2版本)

  • 安全Http响应头
    • 默认的安全头
      • 默认包含的安全头
      • 自定义默认安全标头
      • 禁用默认安全头
      • 禁用所有安全头(完全无效)
    • 缓存控制
      • 默认的缓存控制响应头
      • 禁用默认安全头开启缓存控制
      • 禁用缓存控制(cacheControl)
    • 内容类型(Content-Type)
      • 禁用Content-Type
    • HSTS严格传输安全
      • 默认的标头
      • 默认的严格的传输安全性
    • X框架选项(X-Frame-Options)
      • 默认X-Frame-Options
      • 设置为同源
    • X-XSS-保护
      • 默认的X-XSS-Protection
      • 自定义X-XSS-Protection(关闭)
    • 内容安全策略 (CSP) (可作为XSS第一道防线)
      • 启用CSP的标头
      • 内容安全策略示例
      • 使用 report-uri 的内容安全策略
      • 使用report-only的安全策略报告
      • 官方地址
      • 常见用例
      • 启用CSP
      • 启用 CSP report-only标头
    • 清除站点数据 (建议结合使用)
      • 清理站点数据配置
    • 跨域策略(请参照使用CORS)
    • 自定义Header
      • 自定义X-Custom-Security-Header

安全Http响应头

安全 HTTP 响应标头 可用于提高 Web 应用程序的安全性

默认的安全头

Spring Security 提供了一 组默认的 Security HTTP Response Headers 来提供安全的默认值。 虽然这些标头中的每一个都被认为是最佳实践,但应注意并非所有客户端都使用标头,因此鼓励进行额外测试。
您可以自定义特定的Header

默认包含的安全头

Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Content-Type-Options: nosniff
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

自定义默认安全标头

@EnableWebSecurity
public class WebSecurityConfig {
   

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
   
		http
			// ...
			.headers(headers -> headers
				.frameOptions(frameOptions -> frameOptions
					.sameOrigin()
				)
			);
		return http.build();
	}
}

如果您不想添加默认值并希望明确控制应该使用的内容,则可以禁用默认值

禁用默认安全头

http.headers(headers -> headers.defaultsDisabled());

以这种方式会禁止默认的安全头,除非我们自己指出

禁用所有安全头(完全无效)

@EnableWebSecurity
public class WebSecurityConfig {
   

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
   
		http
			// ...
			.headers(headers -> headers.disable());
		return http.build();
	}
}

缓存控制

Spring Security 默认包含 Cache Control 标头。
Spring Security 的默认设置是禁用缓存以保护用户的内容

但是,如果您确实想要缓存特定的响应,您的应用程序可以选择性地调用 HttpServletResponse.setHeader(String,String) 来覆盖 Spring Security 设置的标头。 这对于确保正确缓存 CSS、JavaScript 和图像等内容很有用。

默认的缓存控制响应头

Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0

禁用默认安全头开启缓存控制

@EnableWebSecurity
public class WebSecurityConfig {
   

	@Bean
	public SecurityFilter

你可能感兴趣的:(笔记,#,SpringSecurity,Java学习,安全,http,java)