balteo
balteo

Reputation: 24689

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

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?

Upvotes: 2

Views: 2534

Answers (1)

user3151168
user3151168

Reputation:

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.

Upvotes: 1

Related Questions