복붙노트

[SPRING] 스프링 보안 : @PreAuthorize에서 Hibernate 엔티티 매개 변수에 액세스 할 수없는 이유는 무엇입니까?

SPRING

스프링 보안 : @PreAuthorize에서 Hibernate 엔티티 매개 변수에 액세스 할 수없는 이유는 무엇입니까?

@PreAuthorize를 적용 할 인터페이스 메소드가 다음과 같습니다.

@PreAuthorize("doSomething(#user.id)")
void something(User user, List<User> accessList);

여기서 User는 Hibernate 엔티티 객체이다. 그것은 나에게 오류를 준다 :

어노테이션을 제거한 것처럼 사용자 매개 변수가 null 인 방법이 없으며이 인터페이스 메소드를 구현하는 메소드에서 사용자 값을 검사 할 때 거기에 유효한 User 객체가 있습니다. 또한이 메서드를 호출하기 전에 사용자 개체가 올바르게 구성되었는지 확인했습니다.

SPEL 파서가 사용자 필드를 null로 간주하는 이유는 무엇인지 알 수 없습니다.

해결법

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

    1.디버거에서 메소드 보안 EvaluationContext에서 Object lookup Variable (String name) 메서드 내에서 수행중인 작업을 확인할 수 있습니다.

    디버거에서 메소드 보안 EvaluationContext에서 Object lookup Variable (String name) 메서드 내에서 수행중인 작업을 확인할 수 있습니다.

        @Override
        public Object lookupVariable(String name) {
        Object variable = super.lookupVariable(name);
    
        if (variable != null) {
            return variable;
        }
    
        if (!argumentsAdded) {
            addArgumentsAsVariables();
            argumentsAdded = true;
        }
    

    따라서 SPEL 변수에 대한 메소드 인수의 변환이 Spring에서 매우 명확하게 구현되므로 addArgumentsAsVariables () 메소드에서 실제로 진행중인 작업을 볼 수 있습니다.

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

    2.Spring Security는 이제이 문제에 대한 더 나은 대답을 가지고 있습니다 :

    Spring Security는 이제이 문제에 대한 더 나은 대답을 가지고 있습니다 :

    http://docs.spring.io/spring-security/site/docs/3.2.5.RELEASE/reference/htmlsingle/#access-control-using-preauthorize-and-postauthorize

    기본적으로

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

    3.loadArgsAsVariables () 메소드, 버전 3.1.0에서 LazyParamAwareEvaluationContext를 확인할 수 있습니다.

    loadArgsAsVariables () 메소드, 버전 3.1.0에서 LazyParamAwareEvaluationContext를 확인할 수 있습니다.

    인터페이스를 구현하기 때문에 다른 Entity에 대해 동일한 키입니다.

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

    4.제목에 우리가 최대 절전 모드 속성에 액세스 할 수 없다는 것을 나타내는 것처럼 뭔가를 추가해야합니다.

    제목에 우리가 최대 절전 모드 속성에 액세스 할 수 없다는 것을 나타내는 것처럼 뭔가를 추가해야합니다.

    hasPermission,로드 된 객체 및 직렬화 된 객체의 두 가지 버전이 있습니다. 다음은 테스트 사례의 일부 코드입니다.

    @PreAuthorize("isAuthenticated() and hasPermission(#organization, 'edit')")
    public long protectedMethod(Organization organization)
    {
        return organization.getId();
    }
    

    그리고 후자의 경우 우리는 실제로 우리가 조직의 id 속성에 액세스 할 수 있음을 봅니다 (이것은 최대 절전 모드 엔티티입니다).

    @PreAuthorize("isAuthenticated() and hasPermission(#organization.getId(), 'organization', 'edit')")
    public long protectedMethodSerializableEdtion(Organization organization)
    {
        return organization.getId();
    }
    
  5. from https://stackoverflow.com/questions/10767469/spring-security-why-cant-we-access-hibernate-entitiy-parameters-in-preauthor by cc-by-sa and MIT license