PostFilter批注应用于通用Spring

PostFilter批注应用于通用Spring

本文介绍了将@PostFilter批注应用于通用Spring Data Jpa存储库方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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?

推荐答案

是的,您可以将@PostFilter添加到Spring Data Repository提供的任何方法中.只需覆盖现有方法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存储库方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 19:27