복붙노트

[SPRING] 여러 역할을 확인하려면 <sec : authorize access = "hasRole ( 'ROLES)">를 어떻게 사용합니까?

SPRING

여러 역할을 확인하려면 를 어떻게 사용합니까?

Spring Security JSP taglibs를 사용하여 롤을 기반으로 조건부로 일부 컨텐츠를 표시하려고합니다. 그러나 Spring Security 3.1.x에서는 오직 하나의 역할만을 확인하고 있습니다.

사용할 수 있지만 ifAllGranted는 사용되지 않습니다.

어떤 도움?

해결법

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

    1.봄 보안에는 특별한 보안 표현이 있습니다.

    봄 보안에는 특별한 보안 표현이 있습니다.

    나는 그것을 사용한 적이 없지만 그것이 정확히 당신이 찾고있는 것이라고 생각합니다.

    사용 예 :

    <security:authorize access="hasAnyRole('ADMIN', 'DEVELOPER')">
        ...
    </security:authorize>
    

    다음은 표준 스프링 보안 표현식이 설명되어있는 참조 문서에 대한 링크입니다. 또한 필요한 경우 사용자 지정 식을 만드는 방법을 설명하는 토론이 있습니다.

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

    2.@ dimas의 대답은 질문과 논리적으로 일치하지 않습니다. ifAllGranted는 hasAnyRole로 직접 바꿀 수 없습니다.

    @ dimas의 대답은 질문과 논리적으로 일치하지 않습니다. ifAllGranted는 hasAnyRole로 직접 바꿀 수 없습니다.

    Spring Security 3-> 4 마이그레이션 가이드 :

    늙은:

    <sec:authorize ifAllGranted="ROLE_ADMIN,ROLE_USER">
        <p>Must have ROLE_ADMIN and ROLE_USER</p>
    </sec:authorize>
    

    신규 (SPEL) :

    <sec:authorize access="hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')">
        <p>Must have ROLE_ADMIN and ROLE_USER</p>
    </sec:authorize>
    

    ifAllRole으로 ifAllGranted를 직접 바꾸면 spring은 AND 대신 AND를 사용하여 명령문을 평가하게됩니다. 즉, 인증 된 주체에 지정된 역할 중 하나 이상이 포함되어 있으면 hasAnyRole은 true를 반환하는 반면, 인증 된 주체에 지정된 모든 역할이 포함되어 있으면 ifAllGranted 메서드는 true를 반환하는 반면 Spring (현재 스프링 보안 4에서는 사용되지 않음)이 true를 반환했습니다.

    TL; DR : Spring Security Taglib의 새로운 인증 표현 언어를 사용하여 ifAllGranted의 동작을 복제하려면 hasRole ( 'ROLE_1') 및 hasRole ( 'ROLE_2') 패턴을 사용해야합니다.

  3. ==============================

    3.hasAnyRole ( 'ROLE_ADMIN', 'ROLE_USER')을 사용했지만 오류가 발생하여 빈 생성이 발생했습니다.

    hasAnyRole ( 'ROLE_ADMIN', 'ROLE_USER')을 사용했지만 오류가 발생하여 빈 생성이 발생했습니다.

    Error creating bean with name     'org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0': Cannot create inner bean '(inner bean)' of type [org.springframework.security.web.access.expression.ExpressionBasedFilterInvocationSecurityMetadataSource] while setting bean property 'securityMetadataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.security.web.access.expression.ExpressionBasedFilterInvocationSecurityMetadataSource]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Expected a single expression attribute for [/user/*]
    

    그럼 내가 시도

    액세스 = "hasRole ( 'ROLE_ADMIN') 또는 hasRole ( 'ROLE_USER')"그것은 나를 위해 잘 작동합니다.

    내 사용자 중 하나로서 관리자뿐만 아니라 사용자입니다.

    이를 위해 use-expressions = "true"를 추가해야합니다. auto-config = "true"다음에 http 태그

    <http use-expressions="true" auto-config="true" >.....</http>
    
  4. from https://stackoverflow.com/questions/11469211/how-to-use-secauthorize-access-hasroleroles-for-checking-multiple-roles by cc-by-sa and MIT license