在使用 Spring Data JPA 和 Spring Data REST 的应用程序中,假设您有一个如下所示的实体类:
@Entity
public class Person {
@Id @GeneratedValue
private int id;
private String name;
@JsonIgnore
private String superSecretValue;
...
}
我们希望 Spring Data REST 公开该实体的所有字段,除了superSecretValue
,所以我们用这个字段注释了@JsonIgnore
.
然而,在某些情况下,我们确实希望访问superSecretValue
,因此我们创建一个投影,它将返回包括该字段在内的所有字段:
@Projection(name = "withSecret", types = {Person.class})
public interface PersonWithSecret {
String getName();
String getSuperSecretValue();
}
惊人的。现在我们可以访问Person
实体包括 the superSecretValue
像这样的字段:
curl http://localhost:8080/persons?projection=withSecret
我的问题是我们怎样才能确保这一点投影?我们如何配置以便任何人都可以检索Person
实体without the superSecretValue
领域......但仅限于具有特定角色的人(例如,ROLE_ADMIN
)可以使用投影来检索隐藏字段吗?
我发现了无数使用的例子@PreAuthorize
or @Secured
用于保护 Spring Data JPA 存储库 CRUD 方法的注释(例如save()
, delete()
)...但没有如何限制 Spring Data REST 投影的使用的示例。