Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
内容安全策略Spring安全_Spring_Security_Spring Security_Content Security Policy - Fatal编程技术网

内容安全策略Spring安全

内容安全策略Spring安全,spring,security,spring-security,content-security-policy,Spring,Security,Spring Security,Content Security Policy,假设有一个spring安全性和spring mvc的工作hello world示例 当我使用wireshark进行跟踪时,我在http请求上看到以下标志 X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: 0 Strict-Transpo

假设有一个spring安全性和spring mvc的工作hello world示例

当我使用wireshark进行跟踪时,我在http请求上看到以下标志

X-Content-Type-Options: nosniff 
X-XSS-Protection: 1; mode=block 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate 
Pragma: no-cache 
Expires: 0 
Strict-Transport-Security: max-age=31536000 ; includeSubDomains 
X-Frame-Options: DENY 
Set-Cookie: JSESSIONID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; Path=/; Secure; HttpOnly 
我想将此添加到我的标题中:

Content-Security-Policy: script-src 'self'
我知道X-Frame-Options的功能几乎相同,但它仍能让我睡得更好。 现在我想我需要在我的spring安全配置的配置功能下完成这项工作,但是我不知道具体是如何完成的,也就是说,我想 .headers().something.something(self)


只需像这样使用addHeaderWriter方法:

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends
   WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      // ...
      .headers()
        .addHeaderWriter(new StaticHeadersWriter("X-Content-Security-Policy","script-src 'self'"))
      // ...
  }
}
请注意,一旦您指定了任何应该包含的头,那么只有这些头才会被包含

要包含默认标题,可以执行以下操作:

http
  .headers()
    .contentTypeOptions()
    .xssProtection()
    .cacheControl()
    .httpStrictTransportSecurity()
    .frameOptions()
    .addHeaderWriter(new StaticHeadersWriter("X-Content-Security-Policy","script-src 'self'"))
    // ...

当使用
StaticHeadersWriter
的方法工作时,您可以参考。

,在最新版本的Spring Security中,可以使用一种特殊的方法:

headers()
    .contentSecurityPolicy("script-src 'self'");

有关详细信息,请参阅文档:

如Spring安全文档中所述:


很有魅力,克里斯托夫谢谢你。我注意到的一点是,chromium/rekonq/opera确实在加载页面时显示css,但firefox在启用“.addHeaderWriter(新的StaticHeaderWriter(“X-Content-Security-Policy”,“脚本src'self')”时不显示css。还要注意方法“StaticHeaderWriter”名称中缺少的“s”,即“StaticHeaderWriter”。我已经看到你发布的referece文档也是如此。好吧,我找到了为什么这在firefox中不起作用的原因。我需要使用购买的标题“X-Content-Security-Polic”和“Content-Security-Polic”在这里对阅读本文的任何人进行更新。如果底部的代码没有运行,您需要在每一行的末尾添加“.and()”,例如contentTypeOptions()…和().xssprotion()。和(),您还可以执行“.headers(headers->headers.contentSecurityPolicy(“script src'self')”),而不是重置默认值(请参阅OP提供的链接)。
headers()
    .contentSecurityPolicy("script-src 'self'");
@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    // ...
    .headers()
        .contentSecurityPolicy("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/")
        .reportOnly();
}
}