【问题标题】:Spring - SpEL evaluates entity argument as null reference in @PreAuthorize("hasPermission")Spring - SpEL 将实体参数评估为 @PreAuthorize("hasPermission") 中的空引用
【发布时间】:2016-12-27 23:13:16
【问题描述】:

我遇到了问题,SpEL 在此存储库的第二种方法中将实体参数评估为空引用。第一种方法效果很好,并且 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 中断言目标对象不能为空。

@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);
    }
}

【问题讨论】:

    标签: java spring spring-el


    【解决方案1】:

    当在 interfaces 中从 spel 中引用方法参数时,需要使用 Spring Data 的 @Param 对其进行注释以明确命名它们:

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

    如果参数没有注释,Spring 必须使用反射来发现参数名称。这仅适用于接口方法,如果

    • 您正在运行 Spring 4+
    • 您正在运行 Java 8
    • 接口是用 JDK 8 编译的,并指定了 -parameters 标志

    对于类方法,Spring 有另一个选择——它可以使用调试信息。这适用于 Spring 3 和更早版本的 Java,但同样依赖于编译时标志才能工作(即-g)。

    为了可移植性,最好对需要引用的所有参数进行注释。

    参考:Access Control using @PreAuthorize and @PostAuthorize

    【讨论】:

    • 谢谢! (以及一些其他字符来填充空间)。
    猜你喜欢
    • 2013-10-23
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2018-03-10
    相关资源
    最近更新 更多