update 优化 脱敏增加角色及权限校验

This commit is contained in:
刘一森 2023-11-09 16:03:53 +08:00
parent 0f16051024
commit 7c533a21d2
6 changed files with 36 additions and 5 deletions

View File

@ -139,4 +139,9 @@ public interface UserConstants {
*/ */
Long SUPER_ADMIN_ID = 1L; Long SUPER_ADMIN_ID = 1L;
/**
* 角色及菜单权限脱敏标识
*/
String SENSITIVE = "sensitive";
} }

View File

@ -2,6 +2,7 @@ package org.dromara.common.sensitive.annotation;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.dromara.common.core.constant.UserConstants;
import org.dromara.common.sensitive.core.SensitiveStrategy; import org.dromara.common.sensitive.core.SensitiveStrategy;
import org.dromara.common.sensitive.handler.SensitiveHandler; import org.dromara.common.sensitive.handler.SensitiveHandler;
@ -21,4 +22,8 @@ import java.lang.annotation.Target;
@JsonSerialize(using = SensitiveHandler.class) @JsonSerialize(using = SensitiveHandler.class)
public @interface Sensitive { public @interface Sensitive {
SensitiveStrategy strategy(); SensitiveStrategy strategy();
String roleKey() default UserConstants.SENSITIVE;
String perms() default UserConstants.SENSITIVE;
} }

View File

@ -13,6 +13,6 @@ public interface SensitiveService {
/** /**
* 是否脱敏 * 是否脱敏
*/ */
boolean isSensitive(); boolean isSensitive(String roleKey,String perms);
} }

View File

@ -26,12 +26,14 @@ import java.util.Objects;
public class SensitiveHandler extends JsonSerializer<String> implements ContextualSerializer { public class SensitiveHandler extends JsonSerializer<String> implements ContextualSerializer {
private SensitiveStrategy strategy; private SensitiveStrategy strategy;
private String roleKey;
private String perms;
@Override @Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException { public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
try { try {
SensitiveService sensitiveService = SpringUtils.getBean(SensitiveService.class); SensitiveService sensitiveService = SpringUtils.getBean(SensitiveService.class);
if (ObjectUtil.isNotNull(sensitiveService) && sensitiveService.isSensitive()) { if (ObjectUtil.isNotNull(sensitiveService) && sensitiveService.isSensitive(roleKey, perms)) {
gen.writeString(strategy.desensitizer().apply(value)); gen.writeString(strategy.desensitizer().apply(value));
} else { } else {
gen.writeString(value); gen.writeString(value);
@ -47,6 +49,8 @@ public class SensitiveHandler extends JsonSerializer<String> implements Contextu
Sensitive annotation = property.getAnnotation(Sensitive.class); Sensitive annotation = property.getAnnotation(Sensitive.class);
if (Objects.nonNull(annotation) && Objects.equals(String.class, property.getType().getRawClass())) { if (Objects.nonNull(annotation) && Objects.equals(String.class, property.getType().getRawClass())) {
this.strategy = annotation.strategy(); this.strategy = annotation.strategy();
this.roleKey = annotation.roleKey();
this.perms = annotation.perms();
return this; return this;
} }
return prov.findValueSerializer(property.getType(), property); return prov.findValueSerializer(property.getType(), property);

View File

@ -50,13 +50,13 @@ public class TestSensitiveController extends BaseController {
/** /**
* 电话 * 电话
*/ */
@Sensitive(strategy = SensitiveStrategy.PHONE) @Sensitive(strategy = SensitiveStrategy.PHONE, roleKey = "common")
private String phone; private String phone;
/** /**
* 地址 * 地址
*/ */
@Sensitive(strategy = SensitiveStrategy.ADDRESS) @Sensitive(strategy = SensitiveStrategy.ADDRESS, perms = "system:user:query")
private String address; private String address;
/** /**

View File

@ -1,5 +1,8 @@
package org.dromara.system.service.impl; package org.dromara.system.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.satoken.utils.LoginHelper; import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.sensitive.core.SensitiveService; import org.dromara.common.sensitive.core.SensitiveService;
import org.dromara.common.tenant.helper.TenantHelper; import org.dromara.common.tenant.helper.TenantHelper;
@ -20,7 +23,21 @@ public class SysSensitiveServiceImpl implements SensitiveService {
* 是否脱敏 * 是否脱敏
*/ */
@Override @Override
public boolean isSensitive() { public boolean isSensitive(String roleKey,String perms) {
if (!StpUtil.isLogin()){
return true;
}
LoginUser loginUser = LoginHelper.getLoginUser();
if (ObjectUtil.isNotNull(loginUser)) {
boolean roleSensitiveFlag = loginUser.getRolePermission().stream().anyMatch(role -> role.contains(roleKey));
if (roleSensitiveFlag) {
return false;
}
boolean permsSensitiveFlag = loginUser.getMenuPermission().stream().anyMatch(menu -> menu.contains(perms));
if (permsSensitiveFlag) {
return false;
}
}
if (TenantHelper.isEnable()) { if (TenantHelper.isEnable()) {
return !LoginHelper.isSuperAdmin() && !LoginHelper.isTenantAdmin(); return !LoginHelper.isSuperAdmin() && !LoginHelper.isTenantAdmin();
} }