update 优化 脱敏注解 @Sensitive 增加角色和权限校验

2.X
疯狂的狮子Li 1 year ago
parent 006c4fb3d0
commit d0b7c8fc51

@ -21,4 +21,8 @@ import java.lang.annotation.Target;
@JsonSerialize(using = SensitiveHandler.class)
public @interface Sensitive {
SensitiveStrategy strategy();
String roleKey() default "";
String perms() default "";
}

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

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

@ -49,25 +49,25 @@ public class TestSensitiveController extends BaseController {
/**
*
*/
@Sensitive(strategy = SensitiveStrategy.PHONE)
@Sensitive(strategy = SensitiveStrategy.PHONE, roleKey = "common")
private String phone;
/**
*
*/
@Sensitive(strategy = SensitiveStrategy.ADDRESS)
@Sensitive(strategy = SensitiveStrategy.ADDRESS, perms = "system:user:query")
private String address;
/**
*
*/
@Sensitive(strategy = SensitiveStrategy.EMAIL)
@Sensitive(strategy = SensitiveStrategy.EMAIL, roleKey = "common", perms = "system:user:query1")
private String email;
/**
*
*/
@Sensitive(strategy = SensitiveStrategy.BANK_CARD)
@Sensitive(strategy = SensitiveStrategy.BANK_CARD, roleKey = "common1", perms = "system:user:query")
private String bankCard;
}

@ -1,7 +1,10 @@
package org.dromara.demo.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.sensitive.core.SensitiveService;
import org.dromara.common.tenant.helper.TenantHelper;
import org.springframework.stereotype.Service;
/**
@ -18,7 +21,25 @@ public class SensitiveServiceImpl implements SensitiveService {
*
*/
@Override
public boolean isSensitive() {
public boolean isSensitive(String roleKey, String perms) {
if (!StpUtil.isLogin()) {
return true;
}
boolean roleExist = StringUtils.isNotBlank(roleKey);
boolean permsExist = StringUtils.isNotBlank(perms);
if (roleExist && permsExist) {
if (StpUtil.hasRole(roleKey) && StpUtil.hasPermission(perms)) {
return false;
}
} else if (roleExist && StpUtil.hasRole(roleKey)) {
return false;
} else if (permsExist && StpUtil.hasPermission(perms)) {
return false;
}
if (TenantHelper.isEnable()) {
return !LoginHelper.isSuperAdmin() && !LoginHelper.isTenantAdmin();
}
return !LoginHelper.isSuperAdmin();
}

@ -1,5 +1,7 @@
package org.dromara.system.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.sensitive.core.SensitiveService;
import org.dromara.common.tenant.helper.TenantHelper;
@ -20,7 +22,22 @@ public class SysSensitiveServiceImpl implements SensitiveService {
*
*/
@Override
public boolean isSensitive() {
public boolean isSensitive(String roleKey, String perms) {
if (!StpUtil.isLogin()) {
return true;
}
boolean roleExist = StringUtils.isNotEmpty(roleKey);
boolean permsExist = StringUtils.isNotEmpty(perms);
if (roleExist && permsExist) {
if (StpUtil.hasRole(roleKey) && StpUtil.hasPermission(perms)) {
return false;
}
} else if (roleExist && StpUtil.hasRole(roleKey)) {
return false;
} else if (permsExist && StpUtil.hasPermission(perms)) {
return false;
}
if (TenantHelper.isEnable()) {
return !LoginHelper.isSuperAdmin() && !LoginHelper.isTenantAdmin();
}

Loading…
Cancel
Save