2012-09-30 45 views
0

我正在学习使用Spring安全,我已经集成在一个Web应用程序。我正在使用Spring和Spring Security版本3.1.2。春季安全工作与访问=“ROLE_USER”,但不是与EL

如果我在安全配置中指定了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.springframew[email protected]844f42c6: Principal: [email protected]: Username: test; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_USER; Credentials: [PROTECTED]; Authenticated: true; Details: org.sprin[email protected]957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_USER 

DEBUG org.springframework.security.access.vote.AffirmativeBased - Voter: org.sp[email protected]1a15597, returned: -1 

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

如果我理解正确的是WebExpressionVoter对投票我,尽管认证工作。

我错过了什么?

回答

1

将溶液简单:只添加ROLE_hasRole(),如:

access="hasRole('ROLE_USER')" 

我被一个例子在manual的部分16.2误导。

+0

嘿,我知道这是一个古老的问题,但在你的研究中,你发现为什么这是必需的?为什么它必须以“ROLE_”开头?所有角色名称都需要这种语法吗? – user2223059

+0

“前缀ROLE_的使用是一个标记,用于指示这些属性是角色,并且应该由Spring Security的RoleVoter使用,这只有在使用基于选举器的AccessDecisionManager时才会涉及。” http:// docs。 spring.io/spring-security/site/docs/3.1.x/reference/technical-overview.html – stivlo