스프링 보안의 차이점이 무엇인지 명확하지 않습니다.
@PreAuthorize("hasRole('ROLE_USER')")
public void create(Contact contact)
과
@Secured("ROLE_USER")
public void create(Contact contact)
PreAuthorize는 spring el과 함께 작동 할 수 있지만 내 샘플에는 실제 차이점이 있습니까?
답변
실제 차이점은 Spring Expression Language (SpEL)@PreAuthorize
과 함께 사용할 수 있다는 것입니다 . 당신은 할 수 있습니다 :
- 의 액세스 방법 및 속성
SecurityExpressionRoot
. -
액세스 메소드 인수 (디버그 정보 또는 custom을 사용하여 컴파일해야 함
ParameterNameDiscoverer
) :@PreAuthorize("#contact.name == principal.name") public void doSomething(Contact contact)
- (고급 기능) 고유 한 메소드를 추가하십시오 (재정의
MethodSecurityExpressionHandler
하고로 설정<global-method-security><expression-handler ... /></...>
).
답변
사용자에게 Role1 과 Role2 가있는 경우에만 메소드 액세스와 같은 작업을 수행 하려면 @PreAuthorize를 사용해야합니다.
@PreAuthorize("hasRole('ROLE_role1') and hasRole('ROLE_role2')")
사용
@Secured({"role1", "role2"}) // is treated as an OR
답변
간단하게
@PreAuthorize
님보다 최신 @Secured
입니다.
따라서 @PreAuthorize
“표현 기반”이므로 사용하는 것이 좋습니다. hasRole, hasAnyRole, permitAll 등의 표현식을 사용할 수 있습니다.
표현식에 대해 배우려면 다음 예제 표현식을 참조하십시오 .
답변
@PreAuthorize
다릅니다 @Secured
. 보다 강력합니다 .
-
이전
@Secured
주석에서는 표현식을 사용할 수 없었습니다. -
Spring Security 3부터는 스프링 표현식 언어 (SpEL)를 지원하고 표현식 기반 액세스 제어를 제공 하므로보다 유연한 어노테이션
@PreAuthorize
과@PostAuthorize
@PreFilter 및 @PostFilter가 선호됩니다. -
@Secured("ROLE_ADMIN")
주석은와 동일합니다@PreAuthorize ("hasRole('ROLE_ADMIN')")
. -
은
@Secured({"ROLE_USER","ROLE_ADMIN")
ROLE_USER로 간주됩니다 또는 ROLE_ADMIN.
그래서 당신은 사용하여 AND 조건을 표현할 수 없습니다
@ 보안 . 를 사용하여 동일하게 정의 할 수 있으므로
@PreAuthorize("hasRole('ADMIN') OR hasRole('USER')")
이해하기 쉽습니다. AND, OR 또는 NOT (!) 도 표현할 수 있습니다 .@PreAuthorize ( “!는 isAnonymous () 및 hasRole ( ‘ADMIN’)”)
답변
+-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
| | @Secured | @PreAuthorize |
+-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
| Spring EL expressions | Does'nt supports. | Supports |
+-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
| Multiple roles conjunctions with AND operator | Does'nt supports.(If there are multiple roles defined | Supports |
| |they will be automatically combined with OR operator) | |
+-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
| To enable annotation | Add following line to spring-security.xml | Add following line to spring-security.xml |
| | <global-method-security secured-annotations="enabled" /> | <global-method-security pre-post-annotations="enabled"/> |
+-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
| Example | @Secured({ROLE_ADMIN , ROLE_USER}) | @PreAuthorize("hasRole('ROLE_USER') and hasRole('ROLE_ADMIN')") |
| | public void addUser(UserInfo user){...} | public void addUser(UserInfo user){...} |
+-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
답변
