【问题标题】:Extending WebSecurityConfigurerAdapter is not adding CSP header扩展 WebSecurityConfigurerAdapter 不添加 CSP 标头
【发布时间】:2020-12-27 01:05:53
【问题描述】:

在我的 Spring MVC (5.0.3) 应用程序中,我添加了一个类 SecurityConfig,它扩展了 WebSecurityConfigurerAdapter,如下所示:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter 
{

    @Override
    protected void configure(HttpSecurity http) throws Exception 
    {
        http.headers().contentSecurityPolicy("script-src 'self'");
    }
    
}

我的期望是在浏览 http://localhost:2020/webapp/ 之后,我会在浏览器的开发者控制台(网络选项卡)中看到 CSP 标头。但是,响应头中没有添加任何与 CSP 相关的内容。

我通过在http.headers().contentSecurityPolicy("script-src 'self'"); 行应用断点进行了检查,它在上下文加载期间命中了一次。

为什么没有在响应中添加 CSP 标头有什么问题?

更新:我什至尝试使用http.headers(),根据文档,此调用没有附加方法,将添加默认安全标头。但就我而言,没有添加任何默认安全标头。

根据要求,我已经上传了最小的、可重复的示例@Google Drive,click here 以供下载

【问题讨论】:

  • 抱歉,我无法重现您的问题。您能否提供一个mvce 示例?
  • @s7vr 请检查有问题的 MVCE 更新。
  • 您好,您是在使用 Vue.js 或 Angular 之类的单页应用程序框架作为前端,而对于运行他们提供的开发服务器的前端,我刚刚在我的一个项目中尝试使用 Vue .js,如果通过代理服务器发出请求,我没有得到内容安全策略,但如果我直接调用由 spring boot 启动的 tomcat 服务器,配置就在那里,即使我尝试使用邮递员调用直接在后端的端点,我得到内容安全策略

标签: java spring-mvc spring-security


【解决方案1】:

查看了 mvce,您似乎没有注册 spring 安全过滤器链。在 web.xml 中添加以下内容,它应该可以正常工作。

<filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

【讨论】:

  • 在 mvce 中,将其添加到 web.xml 后一切正常,但在添加后的实际项目中,我收到错误 No bean named 'springSecurityFilterChain' available。经过一番搜索,from this source我知道我有init。一个 bean 到 root-context.xml 中(因为我在我的应用程序中使用 ContextLoaderListener),但质疑为什么在 mvce 应用程序中不需要它。
  • 我只想将 spring security 用于安全标头,而不是用于身份验证和授权目的。可以吗?
  • 对不起,不是beans而是spring security xml文件需要让这个过滤器工作,但是mvce应用程序没有任何这样的文件那么它为什么也能工作?
  • 你好!我也按照这个问题寻求同样的帮助。我也面临着这个问题正如 Amogh 所说,如果我在 mvce 中使用这个文件管理器,那么标题会被添加,但是当我在我的应用程序中添加这个过滤器时,它会抛出异常,因为没有名为“springSecurityFilterChain”的 bean 可用。如何解决这个问题?
  • 对不起,我不知道。也许您可以创建一个包含所有详细信息的新问题,并且有人可以帮助您。如果我发现了什么,我会更新答案。
【解决方案2】:

根据docs,您必须提供源 URL,以便用户代理阻止尝试从其他源加载脚本,而不是在 script-src 指令中声明的源。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter 
{

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

【讨论】:

  • 但是我不想为除“self”之外的脚本添加任何其他来源,那么为什么我应该指定 url。而且我在文档中也没有找到任何内容说“你必须提及受信任的 URL,只指定“自我”是行不通的”
  • 当然不是,文档会通过示例向您展示您应该做什么,而不是您不应该做什么。在这种情况下,即使是你的“自我”,他们也在指定来源,我只是指出这一点。
  • 我还检查了http.headers(),根据文档,此调用仅headers(),没有其他方法,它将添加默认安全标头。但就我而言,没有添加任何默认安全标头。检查 mvce 的问题。
  • 如你所说,我已经检查了更多仅供参考,我已经添加了这一行 http.headers().contentSecurityPolicy("script-src 'self' https://trustedscripts.example.com"); 但没有成功。
【解决方案3】:

您需要使用以下格式将 contentSecurityPolicy 添加到您的 api 调用中。您可以在此处进一步探索您的春季版本(5.0.3)https://docs.spring.io/spring-security/site/docs/5.0.3.RELEASE/reference/html/headers.html#headers-csp

Spring(5.5.X) Security 默认不添加内容安全策略,因为没有应用程序的上下文就不可能知道合理的默认值。 Web 应用程序作者必须声明安全策略以强制和/或监视受保护的资源。参考 14.2.8。这里https://docs.spring.io/spring-security/site/docs/5.5.x/reference/html5/#servlet-headers-csp

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

【讨论】:

    猜你喜欢
    • 2020-04-17
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 2019-02-27
    相关资源
    最近更新 更多