2012-09-30 41 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