java - 无法从 SpEL 找到 Spring Security 主体

标签 java spring spring-security spring-data-jpa

我想通过 JPA 查询从 CrudRepositoryRestResource 获取数据:

public interface IContactRepository extends PagingAndSortingRepository<Contact, Long> {

    @Query("select contact from Contact contact where contact.owner.login = ?#{principal.username}")
    @RestResource( path = "my")
    List<Contact> findByOwner();
}

但是我明白了:

Property or field 'principal' cannot be found on object of type 'java.lang.Object[]' - maybe not public?

我可以直接得到本金:

SecurityContextHolder.getContext().getAuthentication().getPrincipal()

但我需要在界面中使用它才能直接访问 RestResource

最佳答案

我遇到了同样的问题,我通过以下方式解决了它:

provide a bean of type SecurityEvaluationContextExtension

作为documentation说。

以下是来自 doc 的 Java 和 XML 配置:

Java 配置

@Bean
public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
    return new SecurityEvaluationContextExtension();
}

XML 配置

<bean class="org.springframework.security.data.repository.query.SecurityEvaluationContextExtension"/>

希望这有帮助:)

关于java - 无法从 SpEL 找到 Spring Security 主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40293109/

相关文章:

java - 为什么 Hikari 构建了一个错误的数据库 URL?

java - Thymeleaf 应用不同的标题模式

java - 为什么我看不到 ID token 或身份验证 token ?

java - GWT 和 Spring 安全

java - 将 spring boot 与 vaadin 结合使用时,js 文件出现 404

java - 如何将数组转换为 args 列表以在 Java 中调用方法

java - 在stream和noneMatch方面遇到了一些真正的麻烦

java - 如何更新 Java Gui 中的变量?

java.lang.NoClassDefFoundError : org/springframework/transaction/interceptor/TransactionInterceptor error: Spring Mvc Project 错误

java - 登录/使用 Api 与 Spring Security 基本身份验证不起作用