2016-06-25 2 views
2

스프링 보안 버전 4.1을 사용하고 있습니다. 보안 설정에서 access="hasRole('ROLE_ADMIN')" 또는 access="ROLE_ADMIN"을 지정하면 로그인 할 수는 있지만 관리자 페이지에 액세스 할 수 없습니다.스프링 보안이 "hasRole ('ROLE_ADMIN') '또는 ROLE_ADMIN과 작동하지 않습니다.

DEBUG [http-bio-8080-exec-10] [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] Secure object: FilterInvocation: URL: /admin; Attributes: [hasRole('ROLE_ADMIN')]  
2016-06-25 10:07:53,667 [] DEBUG [http-bio-8080-exec-10] [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] Previously Authenticated: org.springframew[email protected]cc305a73: Principal: [email protected]: Username: francatore             ; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_ADMIN        ; Credentials: [PROTECTED]; Authenticated: true; Details: org.sprin[email protected]166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 7F702A6911A71EA5556C750B6D424FF5; Granted Authorities: ROLE_ADMIN         
2016-06-25 10:07:53,667 [] DEBUG [http-bio-8080-exec-10] [org.springframework.security.access.vote.AffirmativeBased] Voter: org.sp[email protected]170ea084, returned: -1 
2016-06-25 10:07:53,668 [] DEBUG [http-bio-8080-exec-10] [org.springframework.security.web.access.ExceptionTranslationFilter] Access is denied (user is not anonymous); delegating to AccessDeniedHandler 

내가 아마도 어떤 누락 될 수있다 : 아래

<security:http use-expressions="true"> 
    <security:intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" /> 
    <!-- security:intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')"/--> 
    <security:intercept-url pattern="/createmanufsensors" access="isAuthenticated()" /> 
</security:http> 
<security:global-method-security secured-annotations="enabled"></security:global-method-security> 

디버그 오류가?

+0

롤은 사용자가 생성하고 계정 할 때 사용자에게 할당됩니다. 위의 사용자에게는 'ROLE_ADMIN'이 할당되고 나머지 사용자에는 'ROLE_USER'가 할당됩니다. 역할은 데이터베이스의 권한 테이블에 저장됩니다. –

답변

4

여기에 대한 설명이 있습니다. 일반 사용자로 인증되었지만 관리 페이지를 볼 수있는 권한이 없습니다.

당신이 access="hasRole('ROLE_ADMIN')" 표현을 사용하는 경우, 다음 봄 EL 클래스 (즉 SecurityExpressionRoot) 우리가 hasRole() 식 제공이 모든 역할 에 접두사 ROLE_을 추가합니다. 따라서 귀하의 경우 귀하가 hasRole('ROLE_ADMIN')에 제공 한 역할은 ROLE_ROLE_ADMIN으로 해결됩니다.

ROLE_ADMIN의 사용자로 인증 된 이유입니다. 그러나 Spring Security 프레임 워크에서 관리자 페이지를 보려면 사용자는 SecurityExpressionRoot 클래스에 ROLE_이라는 접두사가 추가되어 있기 때문에 ROLE_ROLE_ADMIN의 역할이 있어야합니다.

코드에 접두사 ROLE_ (이 경우 access="hasRole('ADMIN')" )을 제거하십시오. 따라서 스프링 보안은 자동으로 ROLE_ 접두사를 추가합니다. 데이터베이스에 admin 역할을 ROLE_ADMIN으로 지정했는지 확인하십시오.

관련 문제