Java 如何在Springboot中根据用户的特定条件或角色@JsonIgnore
嗨,我是春天靴子的新手。我想实现以下逻辑,我如何才能做到这一点, 假设我有一个模型Java 如何在Springboot中根据用户的特定条件或角色@JsonIgnore,java,spring-boot,spring-mvc,jackson-databind,Java,Spring Boot,Spring Mvc,Jackson Databind,嗨,我是春天靴子的新手。我想实现以下逻辑,我如何才能做到这一点, 假设我有一个模型 class Person { // Accessible by Level 3 or Few Roles private int id; private String name; private String address; // Accessible by Level 2 or Few Roles private String phoneNumber;
class Person {
// Accessible by Level 3 or Few Roles
private int id;
private String name;
private String address;
// Accessible by Level 2 or Few Roles
private String phoneNumber;
//Accessible by Only Level 1 or Admin Roles
private String aadharNo;
@JsonIgnore
private LocalDateTime deletedAt;
}
级别是需要通过请求将哪些数据导出为JSON的说明符。例如,角色的优先级很高
如果phoneNumber的指定为JsonInclude(如果访问级别2且角色是特权用户,现在是具有
即使其指定为级别2,普通角色也无法访问此
我怎样才能做到这一点。有什么建议吗?您可以通过使用
@JsonView
和@RestControllerAdvice
来完成
class View {
public static class Level3 {}
public static class Level2 extends Level3 {}
public static class Level1 extends Level2 {}
}
class Person {
@JsonView(View.Level3.class)
private int id;
private String name;
private String address;
@JsonView(View.Level2.class)
private String phoneNumber;
@JsonView(View.Level1.class)
private String aadharNo;
@JsonIgnore
private LocalDateTime deletedAt;
}
并添加此配置以检查spring安全授权
@RestControllerAdvice
class SecurityJsonViewControllerAdvice extends AbstractMappingJacksonResponseBodyAdvice {
@Override
protected void beforeBodyWriteInternal(
MappingJacksonValue bodyContainer,
MediaType contentType,
MethodParameter returnType,
ServerHttpRequest request,
ServerHttpResponse response) {
if (SecurityContextHolder.getContext().getAuthentication() != null
&& SecurityContextHolder.getContext().getAuthentication().getAuthorities() != null) {
Collection<? extends GrantedAuthority> authorities
= SecurityContextHolder.getContext().getAuthentication().getAuthorities();
List<Class> jsonViews = authorities.stream()
.map(GrantedAuthority::getAuthority)
.map(AppConfig.Role::valueOf)
.map(View.MAPPING::get)
.collect(Collectors.toList());
if (jsonViews.size() == 1) {
bodyContainer.setSerializationView(jsonViews.get(0));
return;
}
throw new IllegalArgumentException("Ambiguous @JsonView declaration for roles "
+ authorities.stream()
.map(GrantedAuthority::getAuthority).collect(Collectors.joining(",")));
}
}
}
@RestControllerAdvice
类SecurityJsonViewControllerAdvice扩展了AbstractMappingJacksonResponseBodyAdvice{
@凌驾
BodyWriteInternal之前的受保护无效(
MappingJacksonValue bodyContainer,
MediaType内容类型,
MethodParameter返回类型,
ServerHttpRequest请求,
服务器HttpResponse(响应){
如果(SecurityContextHolder.getContext().getAuthentication()!=null
&&SecurityContextHolder.getContext().getAuthentication().GetAuthories()!=null){
收集我不确定是否有任何使用spring的选项可以为您带来开箱即用的功能。但这听起来像是一个特定于业务的案例,应该通过定制服务或类似的方式来处理,以满足您的特定需求。我认为默认spring中不存在任何方法,我需要找到正确的方法,如何处理我能做到。值得添加一个将writer/reader与如下视图结合使用的示例:String result=json.writerWithView(view.Level3.class).writeValueAsString(person)
@Golam我不明白你在RestControllerAdvice中做了什么。你能解释一下吗。我在哪里指定级别?级别不是我案例的角色,所以我必须在某个地方设置用户或请求具有级别1、2、3访问权限。在这种情况下。是否需要已经实现控制器方法?我们可以编写任何注释吗ion或filter使用n个控制器方法,这样我就不需要在每个控制器方法中写入字符串结果了?您需要使用restcontrolleradvice在这里应用spring授权