Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top