Security Headers Filter

Play的application.conf中关于SecurityHeadersFilter的一些配置说明:

# The X-Frame-Options header. If null, the header is not set.
play.filters.headers.frameOptions = "SAMEORIGIN"

# The X-XSS-Protection header. If null, the header is not set.
play.filters.headers.xssProtection = "1; mode=block"

# The X-Content-Type-Options header. If null, the header is not set.
play.filters.headers.contentTypeOptions = "nosniff"

# The X-Permitted-Cross-Domain-Policies header. If null, the header is not set.
play.filters.headers.permittedCrossDomainPolicies = "master-only"

# The Content-Security-Policy header. If null, the header is not set.
play.filters.headers.contentSecurityPolicy = "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"
  • X-Frame-Options :

    • DENY:不允许被任何页面嵌入;
    • SAMEORIGIN:不允许被本域以外的页面嵌入;
    • ALLOW-FROM uri:不允许被指定的域名以外的页面嵌入(Chrome现阶段不支持);
  • X-XSS-Protection :

    • 0:禁用XSS保护;
    • 1:启用XSS保护;
    • 1; mode=block:启用XSS保护,并在检查到XSS攻击时,停止渲染页面(例如IE8中,检查到攻击时,整个页面会被一个#替换);
  • X-Content-Type-Options :
    互联网上的资源有各种类型,通常浏览器会根据响应头的Content-Type字段来分辨它们的类型。例如:”text/html”代表html文档,”image/png”是PNG图片,”text/css”是CSS样式文档。然而,有些资源的Content-Type是错的或者未定义。这时,某些浏览器会启用MIME-sniffing来猜测该资源的类型,解析内容并执行。
    例如,我们即使给一个html文档指定Content-Type为”text/plain”,在IE8-中这个文档依然会被当做html来解析。利用浏览器的这个特性,攻击者甚至可以让原本应该解析为图片的请求被解析为JavaScript。

    • nosniff : 禁用浏览器的类型猜测
  • X-Content-Security-Policy :
    Content Security Policy,简称CSP。顾名思义,这个规范与内容安全有关,主要是用来定义页面可以加载哪些资源,减少XSS的发生。

    指令示例:

指令指令值示例说明
default-src‘self’ cnd.a.com定义针对所有类型(js、image、css、web font,ajax请求,iframe,多媒体等)资源的默认加载策略,某类型资源如果没有单独定义策略,就使用默认的。
script-src‘self’ js.a.com定义针对JavaScript的加载策略。
style-src‘self’ css.a.com定义针对样式的加载策略。
img-src‘self’ img.a.com定义针对图片的加载策略。
connect-src‘self’针对Ajax、WebSocket等请求的加载策略。不允许的情况下,浏览器会模拟一个状态为400的响应。
font-srcfont.a.com针对Web Font的加载策略。
object-src‘self’针对<object><embed><applet>等标签引入的flash等插件的加载策略。
media-srcmedia.a.com针对audio><video>等标签引入的html多媒体的加载策略。
frame-src‘self’针对frame的加载策略。
sandboxallow-forms对请求的资源启用sandbox(类似于iframe的sandbox属性)。
report-uri/report-uri告诉浏览器如果请求的资源不被策略允许时,往哪个地址提交日志信息。 特别的:如果想让浏览器只汇报日志,不阻止任何内容,可以改用Content-Security-Policy-Report-Only头。

指令值示例:

指令指令示例说明
img-src允许任何内容。
‘none’img-src ‘none’不允许任何内容。
‘self’img-src ‘self’允许来自相同来源的内容(相同的协议、域名和端口)。
dataimg-src data允许data:协议(如base64编码的图片)。
www.a.comimg-src img.a.com允许加载指定域名的资源。
*.a.comimg-src *.a.com允许加载a.com任何子域的资源。
https://img.comimg-src https://img.com允许加载img.com的https资源(协议需匹配)。
https:img-src https:允许加载https资源。
‘unsafe-inline’script-src ‘unsafe-inline’允许加载inline资源(例如常见的style属性,onclick,inline js和inline css等等)。
‘unsafe-eval’script-src ‘unsafe-eval’允许加载动态js代码,例如eval()。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Security中处理跨域请求可以通过以下几种方式实现: 1. 使用CORS(跨域资源共享)配置:可以在Spring Security配置类中添加CorsConfigurationSource Bean,并设置允许跨域的请求来源、方法、头部等信息。示例代码如下: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and().csrf().disable() // 其他配置... } @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); configuration.addAllowedOrigin("http://example.com"); configuration.addAllowedMethod("GET"); configuration.addAllowedHeader("Authorization"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; } } ``` 2. 自定义Filter处理跨域请求:可以创建一个自定义的Filter来处理跨域请求。示例代码如下: ```java @Component public class CorsFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { response.setHeader("Access-Control-Allow-Origin", "http://example.com"); response.setHeader("Access-Control-Allow-Methods", "GET"); response.setHeader("Access-Control-Allow-Headers", "Authorization"); filterChain.doFilter(request, response); } } ``` 3. 使用Spring Security的CSRF保护:如果你启用了Spring Security的CSRF保护(默认情况下是启用的),则需要在跨域请求中包含CSRF令牌。可以通过在前端的请求中添加CSRF令牌,并在Spring Security的配置中禁用CSRF保护来处理跨域请求。 以上是一些处理Spring Security跨域请求的常见方法,你可以根据自己的需求选择适合的方式进行配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值