복붙노트

[SPRING] 스프링 보안에서 커스텀 역할 / 권한을 사용하려면 어떻게해야합니까?

SPRING

스프링 보안에서 커스텀 역할 / 권한을 사용하려면 어떻게해야합니까?

레거시 애플리케이션을 스프링 보안으로 마이그레이션하는 동안 다음 예외가 있습니다.

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)

예전 응용 프로그램에는 "superadmin", "editor", "helpdesk"등과 같은 역할이 있습니다. 그러나 모든 Spring Security 예제에서는 "ROLE_"( "ROLE_ADMIN"등)과 같은 역할 만 볼 수 있습니다. "superadmin"의 이름을 "ROLE_ADMIN"으로 변경하고 config에서이 역할 만 사용하면 모든 것이 작동합니다.

작동하지 않음 :

 <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> 

공장:

<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> 

사용자 정의 역할 이름을 사용할 수 있습니까?

해결법

  1. ==============================

    1.역할이 "ROLE_"접두어로 시작될 것으로 예상하는 기본 구성을 사용하고 있습니다. 사용자 지정 보안 구성을 추가하고 rolePrefix를 ""로 설정해야합니다.

    역할이 "ROLE_"접두어로 시작될 것으로 예상하는 기본 구성을 사용하고 있습니다. 사용자 지정 보안 구성을 추가하고 rolePrefix를 ""로 설정해야합니다.

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

  2. ==============================

    2.다음은 액세스 표현식을 사용한 전체 구성입니다 (@rodrigoap에서 제공하는 링크는 약간 오래된 것 같습니다).

    다음은 액세스 표현식을 사용한 전체 구성입니다 (@rodrigoap에서 제공하는 링크는 약간 오래된 것 같습니다).

    <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>
    
  3. ==============================

    3.ROLE_ 접두어를 무시하기 위해 항상 expression (use-expression = "true"로 설정)을 사용할 수도 있습니다.

    ROLE_ 접두어를 무시하기 위해 항상 expression (use-expression = "true"로 설정)을 사용할 수도 있습니다.

    Spring Security 3.1 소스 코드를 읽은 후, use-expressions = "true"일 때를 발견했습니다 :

    의 경우 : HttpConfigurationBuilder # createFilterSecurityInterceptor ()는 WebExpressionVoter를 등록하지만 RoleVoter, AuthenticatedVoter는 등록하지 않습니다.

    의 경우 : GlobalMethodSecurityBeanDefinitionParser # registerAccessManager ()는 PreInvocationAuthorizationAdviceVoter를 등록하고 (조건부로) 항상 등록합니다. RoleVoter, AuthenticatedVoter, Jsr250Voter를 조건부로 등록합니다.

    PreInvocationAuthorizationAdviceVoter는 @PreAuthorize에 따라 생성 된 PreInvocationAttribute (PreInvocationExpressionAttribute가 구현으로 사용됨)를 처리합니다. PreInvocationExpressionAttribute # getAttribute ()는 항상 null을 반환하므로 RoleVoter, AuthenticatedVoter는 투표하지 않습니다.

  4. ==============================

    4.스프링 시큐리티 3.2를 사용하여, 저에게 도움이되었습니다.

    스프링 시큐리티 3.2를 사용하여, 저에게 도움이되었습니다.

    역할 접두어 변경 :

    <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>
    

    역할 접두어를 적용 할 위치에 따라 보안 스키마 수준 또는 Bean 수준에서 적용 할 수 있습니다.

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

    서비스 레벨에서 역할 접두어 적용 :

    <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>
    
  5. ==============================

    5.이것은 또한 도움이 될 수 있습니다 :

    이것은 또한 도움이 될 수 있습니다 :

    http://forum.springsource.org/showthread.php?96391-Spring-Security-Plug-in-ROLE_-prefix-mandatory

    기본적으로 grails-app / conf / spring / resources.groovy에 다음과 같이 써야한다고합니다.

    roleVoter(org.springframework.security.access.vote.RoleVoter) {
        rolePrefix = ''
    }
    

    그것은 나를 위해 일했습니다.

  6. from https://stackoverflow.com/questions/986892/how-do-i-use-custom-roles-authorities-in-spring-security by cc-by-sa and MIT license