Content-Security-Policy Spring Security

24
假设有一个运行良好的Spring Security和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安全配置的configure函数下进行操作,但我不知道具体怎么做,即我想.headers().something.something(self)。
 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
//          .csrf().disable()
//          .headers().disable()
            .authorizeRequests()
                .antMatchers(   "/register",
                                "/static/**",
                                "/h2/**",
                                "/resources/**",
                            "/resources/static/css/**", 
                                "/resources/static/img/**" , 
                                "/resources/static/js/**", 
                                "/resources/static/pdf/**"                              
                                ).permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }
3个回答

35

31

只需要像这样使用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'"))
    // ...

您可以参考Spring安全文档


非常好用,Christophe谢谢。我注意到的一件事是,当页面加载时,chromium / rekonq / opera会显示css,但是启用“.addHeaderWriter(new StaticHeaderWriter(“X-Content-Security-Policy”,“script-src 'self'”))”时,firefox不会显示css。还要注意方法名称“StaticHeaderWriter”中缺少“s”,即“StaticHeadersWriter”。我看到您发布的参考文档中也是这种情况。 - Tito
1
好的,我找到了为什么在Firefox中无法正常工作的原因。我需要使用“X-Content-Security-Policy”和“Content-Security-Policy”这两个标头。有关过渡说明,请参见此处:https://developer.mozilla.org/en-US/docs/Web/Security/CSP/Using_Content_Security_Policy - Tito
3
更新给任何读者看到的人。如果底部代码不能运行,你需要在每行末尾添加“.and()”。例如:.contentTypeOptions()..and() .xssProtection().and() - John Duskin
3
你还可以尝试这种写法:".headers(headers -> headers.contentSecurityPolicy("script-src 'self'"))",而不需要重置默认设置(参见 OP 提供的链接)。 - John Duskin

8

如Spring安全文档中所述: https://docs.spring.io/spring-security/site/docs/current/reference/html/headers.html

@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();
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接