In an application using Spring Data JPA and Spring Data REST, let's say you have an entity class like this:
@Entity public class Person { @Id @GeneratedValue private int id; private String name; @JsonIgnore private String superSecretValue; ... }
We want Spring Data REST to expose all of these EXCEPT entity fields for superSecretValue , and therefore we annotated this field using @JsonIgnore .
However, in some cases, we want to access superSecretValue , and therefore we create a projection that will return all fields, including this one:
@Projection(name = "withSecret", types = {Person.class}) public interface PersonWithSecret { String getName(); String getSuperSecretValue(); }
Tall. So now we can access the Person objects, including the superSecretValue field, like this:
curl http:
My question is how can we provide this projection ? How can we set things up so that everyone can retrieve Person objects without a superSecretValue field ... but only people with a specific role (say ROLE_ADMIN ) can use a projection to retrieve a hidden field?
I have found endless examples of using @PreAuthorize or @Secured annotations to protect Spring JPA JPA data repository methods (e.g. save() , delete() ) ... but there are no examples on how to limit the use of Spring REST data projection.
java spring rest spring-data spring-data-rest
Steve perkins
source share