将 @PostFilter 注释应用于通用 Spring Data Jpa 存储库方法 [英] Applying @PostFilter annotation to a generic Spring Data Jpa repository method
问题描述
我想在 Spring Data Jpa 存储库通用方法(例如 findAll
)上使用 @PostFilter
注释,如下所示:
I want to use the @PostFilter
annotation on a Spring Data Jpa repository generic method (such as a findAll
) as follows:
@PostFilter("filterObject.isActivated()==true")
public List<Advertisement> findAll();
记住这些方法是由 Spring Data Jpa自动"提供的,因此不会在应用程序代码中公开,我该如何做到这一点?
How can I do that bearing in mind the those methods are provided "automagically" by Spring Data Jpa and are therefore not exposed in the application code?
推荐答案
是的,您可以向 Spring Data Repository 提供的任何方法添加 @PostFilter
.只需覆盖现有方法 findAll() 并添加您的 @PostFilter
注释,如您的示例所示.不要忘记在定义存储库的配置中添加
Yes, you can add a @PostFilter
to any method provided by a Spring Data Repository. Just override existing method findAll() and add your @PostFilter
annotation as depicted in your example. Don't forget to add to your configuration where your repositories are defined
<global-method-security pre-post-annotations="enabled" />
或在基于 Java 的配置中
or in a java based configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
分别.记住.这仅适用于集合和数组.对于每个其他返回类型,如 页面 你得到一个 IllegalArgumentException.见 DefaultMethodSecurityExpressionHandler#filter 实现细节.
respectively. Keep in mind. This works just for collections and arrays. For every other return type like Page you get an IllegalArgumentException. See DefaultMethodSecurityExpressionHandler#filter for implementation details.
这篇关于将 @PostFilter 注释应用于通用 Spring Data Jpa 存储库方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!