【问题标题】:Spring Security: Case Insensitive RolesSpring Security:不区分大小写的角色
【发布时间】:2014-05-23 15:00:06
【问题描述】:

我在基于 Spring MVC 的 Web 应用程序中设置了 Spring Security。但是由于某些外部系统限制,我希望用户 roles 使用小写字母。

但是当使用 In Memory Users 进行本地测试时,应用程序仅在经过身份验证的用户在 UPPER_CASE 中具有角色时才允许访问,并在我将角色更改为小写时立即给出 403。

是否有这样的限制,只有大写的角色。我在文档中找不到任何提及它的内容?

我还发现 filter-invocation-definition-source 的属性 lowercase-comparisons.. 这是用于比较 URL 或角色吗?

下面是FilterSecurityInterceptor的定义:

<bean id="fsi" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="accessDecisionManager" ref="accessDecisionManager" />
    <property name="objectDefinitionSource">
        <sec:filter-invocation-definition-source lowercase-comparisons="true">
            <sec:intercept-url pattern="/logout.jsp"            access="ROLE_ANONYMOUS" />
            <sec:intercept-url pattern="/welcome.htm"           access="ROLE_executer,ROLE_viewer,ROLE_admin_user" />

            <!-- Write Access -->
            <sec:intercept-url pattern="/addNewRecord.htm"      access="ROLE_executer,ROLE_admin_user" />
            <sec:intercept-url pattern="/updateRecord.htm"      access="ROLE_executer,ROLE_admin_user" />
            <sec:intercept-url pattern="/deleteRecord.htm"      access="ROLE_executer,ROLE_admin_user" />
            <sec:intercept-url pattern="/uploadFile.htm"        access="ROLE_executer,ROLE_admin_user" />

            <!-- Read Access to All Other-->
            <sec:intercept-url pattern="/**"                    access="ROLE_executer,ROLE_viewer,ROLE_admin_user"/>                        
        </sec:filter-invocation-definition-source>
    </property>
</bean> 

感谢您的帮助。

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    角色不必大写。但是,在正常配置中,RoleVoter 会查找前缀 ROLE_,它区分大小写。见this FAQ

    您可以将角色投票者配置为具有空前缀(或小写前缀,如果您想要的话),或者您可以使用基于表达式的访问 - 请参阅this answer

    或者,您可以使用GrantedAuthoritiesMapper 配置您的AuthenticationProvider,它将外部系统中的角色转换为Spring Security 的RoleVoter 可以使用的值 - 请参阅this answer

    【讨论】:

    • 这是正确的。值得指出的另一件事是,这些“角色”不是典型意义上的角色(角色/特权)。这些仅仅是以字符串表示的 GrantedAuthority。 RoleVoter 检查用户授予权限中是否存在这些授予权限。
    • 如果您愿意,它们可以是“典型角色”。 GrantedAuthority 只是一个内部接口,其含义取决于投票者。 RoleVoter 只是将字符串表示与为 URL 或安全方法列出的访问控制属性进行比较,这是对应用程序中基于角色的访问的非常典型的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 2019-07-01
    • 2014-12-26
    • 1970-01-01
    相关资源
    最近更新 更多