【问题标题】:spring security filter chain patterns弹簧安全过滤器链模式
【发布时间】:2015-09-04 13:50:45
【问题描述】:

使用 Spring Security 时,您将过滤器链映射到 URL 模式,以指定如何保​​护这些 URL。这些模式可以包含通配符,例如

/foo/*/bar
/foo/**/bar

我找不到这些通配符的任何文档,但我猜第一个模式会匹配

/foo/baz/bar

但不是

/foo/baz/baz/bar

而第二个模式 (/foo/**/bar) 将匹配这两个

【问题讨论】:

  • * 通配符不包含“/”; ** 通配符包含“/”。

标签: java spring-security


【解决方案1】:

也许这段代码会有所帮助:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/security
                           http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <security:http auto-config="true">

        <security:intercept-url pattern="/login.do"
            access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <security:intercept-url pattern="/logout.do"
            access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <security:intercept-url pattern="/fail2login.do"
            access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <security:intercept-url pattern="/json/*.do"
            access="IS_AUTHENTICATED_ANONYMOUSLY" />

        <security:intercept-url pattern="/*" access="ROLE_ADMIN" />
        <security:form-login login-page="/login.do"
            default-target-url="/home.do" authentication-failure-url="/fail2login.do" />

        <security:session-management>
            <security:concurrency-control
                max-sessions="1" />
        </security:session-management>
        <security:logout logout-success-url="/logout.do"
            delete-cookies="JSESSIONID" invalidate-session="true" />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:jdbc-user-service
                data-source-ref="dataSource"
                users-by-username-query="select userName, password, status from User where userName=?"
                authorities-by-username-query="select us.userName, ur.userRoleName from User us, UserRole ur   
                where ur.userName =?  " />
        </security:authentication-provider>
    </security:authentication-manager>
</beans>

【讨论】:

    【解决方案2】:

    你的假设是正确的。单通配符 * 匹配 url 树的特定级别中的任何内容,而双通配符 ** 匹配任何字符串模式。

    所以

    /foo/*/bar
    

    会匹配

    /foo/abc/bar and /foo/xyz/bar but not /foo/abc/xyz/bar
    

    /foo/**/bar
    

    将匹配以上所有内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      • 2017-03-18
      相关资源
      最近更新 更多