복붙노트

[SPRING] Spring - SpEL은 엔티티 인수를 @PreAuthorize ( "hasPermission")의 null 참조로 평가합니다.

SPRING

Spring - SpEL은 엔티티 인수를 @PreAuthorize ( "hasPermission")의 null 참조로 평가합니다.

SpEL이이 저장소의 두 번째 방법에서 엔티티 인수를 null 참조로 평가하고 있다는 문제가 있습니다. 이 첫 번째 방법은 잘 작동하고 ID는 Long으로 올바르게 평가됩니다.

@NoRepositoryBean
public interface SecuredPagingAndSortingRepository<T extends AuditedEntity, ID extends Serializable>
        extends PagingAndSortingRepository<T, ID> {

    @Override
    @RestResource(exported = false)
    @PreAuthorize("hasPermission(#id, null, 'owner')")
    void delete(ID id);

    @Override
    @PreAuthorize("hasPermission(#entity, 'owner')")
    void delete(T entity);
}

이것은 내 사용자 지정 PermissionEvaluator입니다.

@Slf4j
@Component
public class CustomPermissionEvaluator implements PermissionEvaluator {

    private final PermissionResolverFactory permissionResolverFactory;

    @Autowired
    public CustomPermissionEvaluator(PermissionResolverFactory permissionResolverFactory) {
        this.permissionResolverFactory = permissionResolverFactory;
    }

    @Override
    public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        Assert.notNull(userDetails, "User details cannot be null");
        Assert.notNull(targetDomainObject, "Target object cannot be null");
        log.debug("Permmission: " + permission + " check on: " + targetDomainObject + " for user: " + userDetails.getUsername());

        PermissionType permissionType = PermissionType.valueOf(((String) permission).toUpperCase());
        return permissionResolverFactory.getPermissionResolver(permissionType).resolve(targetDomainObject.getClass(), authentication, (AuditedEntity) targetDomainObject);
    }

    @Override
    public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
        // TODO
        return false;
    }
}

이 테스트는 CustomPermissionEvaluator에서 대상 객체가 null 일 수 없다는 주장 때문에 통과되지 않습니다.

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
@ContextConfiguration(classes = SqapApiApplication.class)
public class PermissionsIT {
    @Autowired
    private TestGroupRepository testGroupRepository;

    @Autowired
    private UserRepository userRepository;

    UserEntity user;

    @Before
    public void before() {
        user = new UserEntity("user", "password1", true, Sets.newHashSet(RoleType.ROLE_USER));
        user = userRepository.save(user);
    }

    @Test
    @WithMockUser(username="user")
    public void shouldDeleteWhenIsOwner() throws Exception {
        TestGroupEntity testGroupEntity = new TestGroupEntity("testGroup", "testdesc", Sets.newHashSet(new AbxTestEntity(1, "abx", "desc", null)));
        user.addTestGroup(testGroupEntity);
        user = userRepository.save(user);
        TestGroupEntity createdEntity = testGroupRepository.findAll().iterator().next();
        testGroupRepository.delete(createdEntity);
    }
}

해결법

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

    1.인터페이스에서 spel의 메소드 매개 변수를 참조 할 때 스프링 데이터의 @Param에 주석을 달아 명시 적으로 이름을 지정하면됩니다.

    인터페이스에서 spel의 메소드 매개 변수를 참조 할 때 스프링 데이터의 @Param에 주석을 달아 명시 적으로 이름을 지정하면됩니다.

    @PreAuthorize("hasPermission(#entity, 'owner')")
    void delete(@Param("entity") T entity);
    

    매개 변수에 주석이없는 경우 Spring은 매개 변수 이름을 검색하기 위해 리플렉션을 사용해야합니다. 이는 인터페이스 메소드에서만 가능합니다.

    클래스 메소드의 경우 Spring에는 디버그 정보를 사용할 수있는 또 다른 옵션이 있습니다. 이것은 Spring 3 및 이전 버전의 Java에서 작동하지만 다시 컴파일 할 때 작동하는 플래그 (예 : -g)에 의존합니다.

    이식성을 위해 참조해야하는 모든 매개 변수에 주석을 달아 두는 것이 좋습니다.

    참조 : @PreAuthorize 및 @PostAuthorize를 사용하여 액세스 제어

  2. from https://stackoverflow.com/questions/41353363/spring-spel-evaluates-entity-argument-as-null-reference-in-preauthorizehasp by cc-by-sa and MIT license