【问题标题】:Spring security works with access="ROLE_USER" but not with ELSpring security 适用于 access="ROLE_USER" 但不适用于 EL
【发布时间】:2012-10-01 06:55:51
【问题描述】:

我正在学习使用 Spring Security,并将其集成到 Web 应用程序中。我正在使用 Spring 和 Spring Security 3.1.2 版。

如果我在安全配置中指定access="ROLE_USER",则身份验证正常工作,即我首先收到 401,登录后,我可以访问资源。

<http>
    <http-basic />
    <logout />
    <intercept-url
        pattern="/exports/**"
        access="ROLE_USER" />
</http>

但是,如果我切换到 EL,我的支票将不再有效:

<http use-expressions="true">
    <http-basic />
    <logout />
    <intercept-url
        pattern="/exports/**"
        access="hasRole('USER')" />
</http>

我以为这两个配置是等价的,但是第二个没有授权查看资源(403错误)。

查看日志:

DEBUG org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@844f42c6: Principal: org.springframework.security.core.userdetails.User@c052d588: Username: test; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_USER; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_USER

DEBUG org.springframework.security.access.vote.AffirmativeBased - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@1a15597, returned: -1

DEBUG org.springframework.security.web.access.ExceptionTranslationFilter - Access is denied (user is not anonymous); delegating to AccessDeniedHandler

如果我理解正确,WebExpressionVoter 投票反对我,尽管身份验证有效。

我错过了什么?

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    解决方法很简单:在hasRole()中也加入ROLE_,如:

    access="hasRole('ROLE_USER')"
    

    manual 第 16.2 节中的一个示例误导了我。

    【讨论】:

    • 嘿,我知道这是一个老问题,但在您的研究中,您是否发现为什么需要这样做?为什么它必须以“ROLE_”开头?所有角色名称都需要这种语法吗?
    • "前缀 ROLE_ 的使用是一个标记,表明这些属性是角色,应该由 Spring Security 的 RoleVoter 使用。这仅在使用基于投票者的 AccessDecisionManager 时才相关。" @ 987654322@
    猜你喜欢
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2015-11-03
    • 2015-10-02
    • 1970-01-01
    • 2021-03-25
    • 2014-11-29
    相关资源
    最近更新 更多