2009-06-12 40 views
28

Trong khi di chuyển một ứng dụng di sản để mùa xuân an ninh tôi đã ngoại lệ sau đây:Làm cách nào để sử dụng vai trò/quyền hạn tùy chỉnh trong Spring Security?

org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterChainProxy': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterChainList': Cannot resolve reference to bean '_filterSecurityInterceptor' while setting bean property 'filters' with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterSecurityInterceptor': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unsupported configuration attributes: [superadmin] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) 
at java.security.AccessController.doPrivileged(Native Method) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) 

Trong ứng dụng cũ có vai trò như "trị cấp cao", "biên tập", "trợ giúp" vv Nhưng trong tất cả các ví dụ Xuân An Tôi chỉ thấy các vai trò như "ROLE_" ("ROLE_ADMIN", v.v ...). Khi tôi đổi tên "superadmin" thành "ROLE_ADMIN" và chỉ sử dụng vai trò này trong cấu hình, mọi thứ hoạt động.

Không làm việc:

<http auto-config="true">          
    <intercept-url pattern="/restricted/**" access="superadmin"/> 
    <form-login 
     authentication-failure-url="/secure/loginAdmin.do?error=true" 
     login-page="/secure/loginAdmin.do" />   
</http> 

trình:

<http auto-config="true">          
    <intercept-url pattern="/restricted/**" access="ROLE_ADMIN"/> 
    <form-login 
     authentication-failure-url="/secure/loginAdmin.do?error=true" 
     login-page="/secure/loginAdmin.do" />   
</http> 

phải là có thể sử dụng tên vai trò tùy chỉnh?

+0

này [câu hỏi] (http://stackoverflow.com/questions/283870/acegi-security-how-do-i-add -another-grantedauthority-to-authentication-to-anonym) có thể hữu ích. – kgiannakakis

Trả lời

39

Bạn đang sử dụng cấu hình mặc định dự kiến ​​vai trò bắt đầu bằng tiền tố "ROLE_". Bạn sẽ phải thêm cấu hình bảo mật tùy chỉnh và đặt rolePrefix thành "";

http://forum.springsource.org/archive/index.php/t-53485.html

+0

tôi có thể có ROLE_FOO hoặc ROLE_BAR hoặc ROLE_ANYTHING_I_WANT không? Liên kết bạn cung cấp không giải quyết được vấn đề, chỉ một số người nói "Không thể đặt RolePrefix cho RoleVoter" –

+6

Có, bạn có thể có bất kỳ vai trò nào bạn muốn. Liên kết hiển thị một ví dụ cấu hình và nó có thể không giúp bạn nhưng nó hữu ích cho D. Wroblewski. Nếu bạn cần thêm trợ giúp chỉ cần đăng câu hỏi mới, rất nhiều người sẵn sàng trả lời câu hỏi đó. – rodrigoap

+0

cảm ơn bạn. đã cứu ngày của tôi –

11

Dưới đây là một cấu hình hoàn chỉnh sử dụng biểu thức truy cập (liên kết được cung cấp bởi @rodrigoap dường như một chút lỗi thời):

<http 
     access-decision-manager-ref="accessDecisionManager" 
     use-expressions="true"> 

<beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased"> 
    <beans:property name="decisionVoters"> 
     <beans:list> 
      <beans:bean class="org.springframework.security.web.access.expression.WebExpressionVoter"/> 
      <beans:bean class="org.springframework.security.access.vote.RoleVoter"> 
       <beans:property name="rolePrefix" value=""/> 
      </beans:bean> 
      <beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter"/> 
     </beans:list> 
    </beans:property> 
</beans:bean> 
7

Bạn cũng luôn có thể sử dụng biểu thức (theo cấu hình use-expressions="true") để bỏ qua tiền tố ROLE_.

Sau khi đọc Xuân An 3.1 mã nguồn, tôi thấy khi use-expressions="true":

Đối <security:http >:
HttpConfigurationBuilder#createFilterSecurityInterceptor() sẽ regist WebExpressionVoter nhưng không RoleVoter, AuthenticatedVoter;

Đối <security:global-method-security >: GlobalMethodSecurityBeanDefinitionParser#registerAccessManager() sẽ regist PreInvocationAuthorizationAdviceVoter (có điều kiện), sau đó luôn regist RoleVoter, AuthenticatedVoter, regist Jsr250Voter có điều kiện;

PreInvocationAuthorizationAdviceVoter sẽ xử lý PreInvocationAttribute (PreInvocationExpressionThuộc tính sẽ được sử dụng dưới dạng triển khai) được tạo theo @PreAuthorize. PreInvocationExpressionAttribute#getAttribute() luôn trả về giá trị rỗng, vì vậy RoleVoterAuthenticatedVoter không bỏ phiếu.

2

Sử dụng Bảo mật mùa xuân 3.2, tính năng này phù hợp với tôi.

Thay đổi vai trò Prefix:

<beans:bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter"> 
    <beans:property name="rolePrefix" value="NEW_PREFIX_"/> 
</beans:bean> 

<beans:bean id="authenticatedVoter" class="org.springframework.security.access.vote.AuthenticatedVoter"/> 

<beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased"> 
    <beans:constructor-arg > 
     <beans:list> 
      <beans:ref bean="roleVoter"/> 
      <beans:ref bean="authenticatedVoter"/> 
     </beans:list> 
    </beans:constructor-arg> 
</beans:bean> 

Tùy thuộc vào nơi bạn muốn áp dụng các tiền tố Vai trò nó có thể được áp dụng ở cấp sơ đồ an ninh hoặc cấp đậu.

<http access-decision-manager-ref="accessDecisionManager" use-expressions="true"> 

Áp dụng Vai trò Prefix tại Service Level:

<beans:bean id="myService" class="com.security.test"> 
    <security:intercept-methods access-decision-manager-ref="accessDecisionManager"> 
     <security:protect access="NEW_PREFIX_ADMIN"/> 
    </security:intercept-methods> 
</beans:bean> 
Các vấn đề liên quan