当前位置: 首页 > 知识库问答 >
问题:

使用SpEL原理的Spring

呼延才俊
2023-03-14

我试图使用SpEL在这个文档中https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions做一个查询过滤结果?#{principal.id}

问题是Spring返回一个异常

组织。冬眠QueryException:尚未设置所有命名参数:[1][从产品p JOIN p.store s JOIN p.category c中选择p、p.store、p.category,其中p.store.id=:id和p.KEYORD等p.KEYORD=?1];嵌套的例外是java。lang.IllegalArgumentException:org。冬眠QueryException:尚未设置所有命名参数:[1][从产品p JOIN p.store s JOIN p.category c中选择p、p.store、p.category,其中p.store.id=:id和p.keywords,如:keyword和p.store.ownerId=?1]

我已经设置了以下代码,并且正在执行。

@Service
public class SecurityEvaluationContextExtension extends EvaluationContextExtensionSupport {

    @Override
    public String getExtensionId() {
        return "security";
    }

    @Override
    public SecurityExpressionRoot getRootObject() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println("SER >>>>> " + authentication.getPrincipal().toString() + " -- " );
        return new SecurityExpressionRoot(authentication) {};
    }
}

@Configuration
@EnableJpaRepositories
public class SecurityConfiguration {

    @Bean
    EvaluationContextExtension securityExtension() {
        return new SecurityEvaluationContextExtension();
    }

}

我使用的这个服务器有一个Spring资源服务器,因此使用下面的翻译从授权服务器获取Id。我确认代码执行和翻译都很好,但我得到了上面的异常。

@Service
public class myPrincipalExtractor implements PrincipalExtractor {

    @Override
    public UserInfo extractPrincipal(Map<String, Object> map) {

        Map<String,Object> principal = null;

        if (map.containsKey("principal")) {
            principal = (Map<String, Object>) map.get("principal");
        }

        UserInfo user = new UserInfo();

        if (principal != null ) {
            if (principal.containsKey("id")) {
                user.setId(Long.parseLong(principal.get("id").toString()));
            }

            if (principal.containsKey("username")) {
                user.setUsername(principal.get("username").toString());
            }

            if (principal.containsKey("email")) {
                user.setEmail(principal.get("email").toString());
            }
        }

        System.out.println("----> " + user.getUsername() + " -> " + user.getId());

        return user;
    }
}

问题是。。。

@CrossOrigin
public interface StoreRepository extends CrudRepository<Store, Long>
{
    @Query("select p , p.store, p.category from Product p JOIN p.store s " +
            " JOIN p.category c " +
            " WHERE p.store.id = :id AND p.keywords LIKE %:keyword% AND p.store.ownerId = ?#{principal.id} ")
    List<Product> findByKeywordIgnoreCase(@Param("id") Long id , @Param("keyword") String keyword);
}

更多信息:

我在SecurityExpressionRoot中编写了下面的代码,所以现在我知道,当我在对象中放置SpEL、ID和用户名时,会调用它。还尝试将返回的对象强制转换为Principal,同样的问题也发生了。

p.store.owner我 = ? #{ principal.id}

return new SecurityExpressionRoot(authentication) {
            @Override
            public UserInfo getPrincipal() {
                System.out.println("Fetching the principal has user " + authentication.getPrincipal().toString());
                return (UserInfo) authentication.getPrincipal();
            }
        };

共有1个答案

常波
2023-03-14

在网站示例中(https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions)他们指定你可以使用#{principal.id}执行上面的代码时确实会调用它,但由于某种原因,@Query绑定会失败。但是我试着用另一个例子来运行它

@Query("select p , p.store, p.category from Product p JOIN p.store s " +
            " JOIN p.category c " +
            " WHERE p.store.id = :id AND p.keywords LIKE %:keyword% AND p.store.ownerId = ?#{#security.principal.id} ")

这起作用了。我在这里发现了另一个例子:https://github.com/spring-projects/spring-data-examples/blob/master/jpa/security/src/main/java/example/springdata/jpa/security/SecureBusinessObjectRepository.java

 类似资料:
  • 我正在编写一些代码,以允许使用Spring表达式语言进行动态属性更改。我传入一个bean名称、属性名称和新值的表达式,所有字符串。 这对于类型string、int、boolean和list的属性非常有效。我无法让地图属性工作。我已经看了SPeL留档,包括示例,但我没有看到我所做的任何错误。我得到的例外是没有帮助的。 忽略try/ak块,基本代码如下: 当我的“表达式”是“789,0123,345”

  • 本文向大家介绍Spring Cloud @RefreshScope 原理及使用,包括了Spring Cloud @RefreshScope 原理及使用的使用技巧和注意事项,需要的朋友参考一下 @RefreshScope那些事 要说清楚RefreshScope,先要了解Scope Scope(org.springframework.beans.factory.config.Scope)是Spring

  • 我使用的spring评估语言如下: 我的问题是bigMap中包含的大多数数据都没有被使用。相反,我更愿意通过将函数传递到求值上下文来延迟加载所需的内容,例如: 但我找不到如何做到这一点。有什么建议吗?

  • 我一直在尝试Spring。我希望通过使用SPEL为另一个bean复制一个bean的值和引用属性。 考虑这个bean: 我希望将其值复制到另一个bean,如下所示:

  • 本文向大家介绍Java中的代理原理及代理使用示例,包括了Java中的代理原理及代理使用示例的使用技巧和注意事项,需要的朋友参考一下 今天再测试Socket编程时,无法连接外网。公司用的是Http的代理。上网搜索也没看太懂,所以花了大量时间来学习。看了HTTP和TCP协议的关系好,才有所明白。现在能通过Socket使用HTTP代理了,结果很简单,过程却好难。 1. 先简要说说HTTP和TCP(具体内

  • 本文向大家介绍Android HandlerThread的使用及原理详解,包括了Android HandlerThread的使用及原理详解的使用技巧和注意事项,需要的朋友参考一下 一、HandlerThread的含义 HandlerThread能够新建拥有Looper的线程。这个Looper能够用来新建其他的Handler。(线程中的Looper)需要注意的是,新建的时候需要被回调。 二、Hand