Applying @PostFilter annotation to a generic Spring Data Jpa repository method

邮差的信 提交于 2019-12-22 00:34:40

问题


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();

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?


回答1:


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" />

or in a java based configuration

@EnableGlobalMethodSecurity(prePostEnabled = true)

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.



来源:https://stackoverflow.com/questions/22456280/applying-postfilter-annotation-to-a-generic-spring-data-jpa-repository-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!