【问题标题】:How to enable content security policy to selective http patterns in spring security如何在 Spring Security 中启用内容安全策略以选择性地 http 模式
【发布时间】:2015-12-17 21:46:34
【问题描述】:

我的 spring 安全配置如下所示:

<http pattern="/*/yyy/**" security="none" />
<http pattern="/*/zzz/**" security="none"/>


<http create-session="stateless" use-expressions="true">
    <csrf disabled="true" />
    <intercept-url method="GET" pattern="/*/api/products" access="xxxx" />
    <http-basic entry-point-ref="customBasicAuthenticationEntryPoint" />
</http>

现在,对于上面带有 security="none" 的 http 模式,我想为此启用内容安全策略 (CSP)。只要我保持它 security="none",我认为我不能将 CSP 应用于它。

在 Spring Security 中启用 CSP 的标头如下:

<headers>
    <header name="Content-Security-Policy" value="default-src 'self'"/>
</headers>

现在,我只想将此标头应用于我现在有 security="none" 的前两个 http 模式,而不是应用于我在下一个 http 块中添加的其余 URL。我只是找不到办法。可能吗?有人可以推荐吗?

我不需要为前两种模式定义 entry-point-ref。但是,删除 security="none" 会迫使我为它定义一个。请注意,我想要的只是能够为那些选定的模式启用 CSP,仅此而已。请帮忙!

更新:

【问题讨论】:

    标签: java spring spring-security content-security-policy spring-security-ldap


    【解决方案1】:

    使用security="none"表示对URL没有应用安全性,所以用security="none"映射的URL添加一个带有Spring Security的内容安全策略的说法是矛盾的。

    我猜您希望允许任何用户访问这些 URL。如果是这种情况,您可以轻松使用permitAll 表达式。

    然后,您可以使用DelegatingRequestMatcherHeaderWriter 指定哪些 URL 设置了内容安全策略。例如,使用 Spring Security 4+,您可以使用:

    <http>
        <intercept-url pattern="/*/yyy/**" access="permitAll" />
        <intercept-url pattern="/*/zzz/**" access="permitAll"/>
        <intercept-url method="GET" pattern="/*/api/products" access="xxxx" />
    
    
        <headers>
            <header ref="headerWriter"/>
        </headers>
    
        <csrf disabled="true" />
        <http-basic entry-point-ref="customBasicAuthenticationEntryPoint" />
        <!-- ... -->
    </http>
    
    <beans:bean id="headerWriter"
        class="org.springframework.security.web.header.writers.DelegatingRequestMatcherHeaderWriter">
        <beans:constructor-arg>
            <beans:bean class="org.springframework.security.web.util.matcher.OrRequestMatcher">
                 <beans:constructor-arg>
    
                    <beans:bean class="org.springframework.security.web.util.matcher.AntPathRequestMatcher"
                c:pattern="/*/yyy/**"/>
                    <beans:bean class="org.springframework.security.web.util.matcher.AntPathRequestMatcher"
                c:pattern="/*/zzz/**"/>
                </beans:constructor-arg>
            </beans:bean>
        </beans:constructor-arg>
        <beans:constructor-arg>
            <beans:bean
                class="org.springframework.security.web.header.writers.StaticHeadersWriter"
                c:headerName="Content-Security-Policy"
                c:headerValues="default-src 'self'"  
            />
        </beans:constructor-arg>
    </beans:bean>
    

    请注意,如果您使用的是 Spring Security 3,那么您将需要显式列出您想要启用的所有标头(添加任何显式标头意味着仅应用这些标头)。例如:

        <headers>
            <cache-control />
            <content-type-options />
            <hsts />
            <frame-options />
            <xss-protection />
    
            <header ref="headerWriter"/>
        </headers>
    

    您可以在the migration guide 中找到有关差异的更多详细信息。

    【讨论】:

    • 罗布,感谢您的回复。这是否也将 entry-point-ref="customBasicAuthenticationEntryPoint" 应用于这两个 url: 我不希望这两个url通过entry-point-ref
    • 一个AuthenticationEntryPoint用来处理需要认证,但是用户还没有认证的时候。如果您指明任何人(包括未经身份验证的用户)都可以访问这些 URL,则 AuthenticationEntryPoint 将不会用于这些 URL
    • Rob,根据我的观察, 将其带到 entry-point-ref="customBasicAuthenticationEntryPoint “我不想要的
    • 您使用的实际模式和实际 URL 是什么?请记住,intercept-url 元素是按指定的顺序考虑的,并且只使用第一个。
    猜你喜欢
    • 2014-07-26
    • 2021-09-01
    • 2019-02-06
    • 1970-01-01
    • 2017-09-07
    • 2016-06-22
    • 2019-12-27
    • 2020-01-25
    • 2015-04-27
    相关资源
    最近更新 更多