2010-01-20 36 views
18

Tôi đang cố gắng thiết lập bảo mật Mùa xuân 3 bằng cách sử dụng xác thực JDBC. Mọi thứ đang hoạt động tốt ngoài khi tôi cố gắng chỉ định nhiều vai trò truy cập vào một url chặn. Ví dụ: Tôi muốn bất cứ ai có vai trò ROLE_USER và ROLE_ADMIN để có thể truy cập vào tất cả các trang, tôi sử dụng dòng follwoing trong tập tin cấu hình mùa xuân của tôi -Bảo mật mùa xuân 3 chỉ định nhiều vai trò truy cập chặn chặn url

<security:intercept-url pattern="/**" access="ROLE_USER, ROLE_ADMIN" /> 

Tuy nhiên điều này ném các lỗi sau -

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unsupported configuration attributes: [ ROLE_ADMIN] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1401) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:558) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:852) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:422) 
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:261) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:192) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843) 
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342) 
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) 
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719) 
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) 
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) 
    at org.apache.catalina.core.StandardService.start(StandardService.java:516) 
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) 
    at org.apache.catalina.startup.Catalina.start(Catalina.java:578) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) 
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) 
Caused by: java.lang.IllegalArgumentException: Unsupported configuration attributes: [ ROLE_ADMIN] 
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.afterPropertiesSet(AbstractSecurityInterceptor.java:154) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1460) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1398) 
    ... 27 more 

Nếu chỉ định rằng chỉ có một trong các vai trò có thể truy cập bất kỳ url thì nó là tốt (tốt cho một trong hai vai trò). Thay đổi thứ tự mà tôi chỉ định vai trò cũng không tạo ra sự khác biệt nào. Nó giống như một cái gì đó đã thay đổi trong Spring Security 3 mà bây giờ không thể xử lý các vai trò truy cập mulitple được chỉ định.

Tôi đã thực hiện thành công việc này trước đây với Spring Security 2 và đang sử dụng hầu như cùng một cấu hình. Bất kỳ ý tưởng?

tập tin an ninh cấu hình của tôi được liệt kê dưới đây -

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:security="http://www.springframework.org/schema/security" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/security 
       http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 

    <security:http auto-config="true" access-denied-page="/denied.jsp" >  
     <security:form-login 
      default-target-url="/app/home" 
      always-use-default-target="true" /> 

     <security:intercept-url pattern="/**" access="ROLE_USER, ROLE_ADMIN" /> 

     <security:logout invalidate-session="true" logout-url="/logout" logout-success-url="" /> 
    </security:http> 

    <security:authentication-manager> 
     <security:authentication-provider> 
      <security:jdbc-user-service data-source-ref="dataSource" 
       users-by-username-query='select "username", "password", "enabled" 
        from users where "username" = ?' 
       authorities-by-username-query='select "username", "authority"      from user_roles where "username" = ?' /> 
     </security:authentication-provider> 
    </security:authentication-manager> 
</beans> 

Trả lời

-2

tôi quyết định hạ xuống Xuân An 2.0.5 mà không thay đổi bất cứ điều gì khác để kiểm tra xem đây là một lỗi trong 3, và lo-and-kìa nó là!

Tôi nghĩ tôi cũng tìm thấy một lỗi mở liên quan ở đây - https://jira.springsource.org/browse/SEC-1342

Giải pháp - sử dụng 2.0.5 nếu muốn sử dụng tính năng này.

45

tôi đã có vấn đề tương tự nhưng sử dụng biểu thức để có được xung quanh vấn đề này:

Bạn nên nhúng

use-expressions="true" 

trong cấu hình hiện tại của bạn. Vì vậy:

<security:http auto-config="true" access-denied-page="/denied.jsp" > 

trở thành

<security:http auto-config="true" access-denied-page="/denied.jsp" use-expressions="true"> 

Và sau đó:

<security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')" /> 
11

Tôi không chắc chắn về vấn đề này, thực sự tôi đang sử dụng nó hiện trong dự án của tôi và không thấy vấn đề. hãy thử xóa dấu cách sau "," ý tôi là thử sử dụng ROLE_USER, ROLE_ADMIN

+3

Teja bạn nói đúng, nó không thể xử lý dấu cách sau dấu phẩy. Nó hoạt động tốt mà không có không gian. – user223695

+0

Thông báo lỗi làm rõ rằng đây thực sự là vấn đề: "Thuộc tính cấu hình không được hỗ trợ: [ROLE_ADMIN]" –

4

Tôi gặp sự cố tương tự và tìm thấy câu trả lời here.
Sử dụng dòng đó để cấp quyền truy cập cho người sử dụng với cả hai vai trò:
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER') and hasRole('ROLE_ADMIN')" />
Nếu bạn muốn cấp quyền truy cập cho người sử dụng với một trong những vai trò được liệt kê, sử dụng:
<security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER', 'ROLE_ADMIN')" />

Ngoài ra, bạn cần phải thêm khả năng sử dụng GỬI trong bảo mật * .xml của bạn, thêm use-expressions="true" vào thẻ <http>.

7

Tôi gặp sự cố tương tự khi cố di chuyển từ Xuân 3.x đến 4.x. Cuối cùng tôi thấy rằng tham số "sử dụng biểu thức" của thẻ "http" đã trở thành "true" theo mặc định trong Spring 4.x thay vì false (giống như trong phiên bản cũ).

P.S. Câu hỏi này là rất cũ cho bây giờ, nhưng tôi tìm thấy điều này trong Google. Vì vậy, ai đó khác có thể tìm thấy nó quá và thông tin này có thể hữu ích sau đó.

Các vấn đề liên quan