我正在使用当前版本的Spring Data Rest 和 Spring Data JPA,并拥有以下实体:
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private String password;
private String email;
...getter/setter methods...
}
我也在使用Spring Security
。
我的用户仓库:
@RepositoryRestResource(
collectionResourceRel = "user",
path = "user",
excerptProjection = UserSimpleProjection.class)
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
}
例如:
- 用户1已登录
- 用户1请求
http://localhost:8080/user/1
- 所有字段都可见 - 用户1请求
http://localhost:8080/user/2
- 只有id
和name
可见。
我尝试使用Jackson的不同解决方案,但没有一个能解决我的问题:
然后我发现了投影。
我创建了一个投影:
@Projection(name = "simple", types = User.class)
public interface UserSimpleProjection {
public Long getId();
public String getName();
}
还有另一个详细的:
@Projection(name = "detailed", types = User.class)
public interface UserDetailProjection extends UserSimpleProjection{
public String getEmail();
}
目前为止还不错,我得到了不同的结果,这取决于我的请求。
是否有一种方法可以根据Spring Security自动切换投影并/或限制不同角色的不同投影?