首选创建一个注解类
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Secret {
String[] reqPropsName() default {"shareId"};
String[] repsPropsName() default {"shareId"};
}
切面处理
import cn.hutool.core.util.ObjectUtil;
import com.light.common.annotation.Secret;
import com.light.common.core.page.TableDataInfo;
import com.light.common.utils.AESUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.List;
@Aspect
@Component
public class SecretAspect {
@Value("${share.divide.staticRequestKey}")
private String staticRequestKey;
private static final Logger log = LoggerFactory.getLogger(SecretAspect.class);
@Pointcut("@annotation(com.light.common.annotation.Secret)")
public void pointCut(){
}
/**
*定义切点
*/
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
Object target = joinPoint.getTarget();
Object[] args = joinPoint.getArgs();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = target.getClass().getMethod(signature.getName(), signature.getParameterTypes());
Secret secret = method.getAnnotation(Secret.class);
Object respObj;
try {
String[] reqPropsName = secret.reqPropsName();
String[] repsPropsName = secret.repsPropsName();
if(!ObjectUtil.isEmpty(args) && args.length == 1){
Object arg = args[0];
for (String propName:reqPropsName){
String propVal = (String) arg.getClass().getMethod(getterName(propName)).invoke(arg);
String propDecrypt = AESUtil.decrypt(propVal,staticRequestKey);
arg.getClass().getMethod(setterName(propName),String.class).invoke(arg,propDecrypt);
}
}else if(args.length>1){
}
respObj = joinPoint.proceed();
if(!ObjectUtil.isEmpty(repsPropsName)){
List<?> rows = ((TableDataInfo) respObj).getRows();
rows.forEach(item->{
for(String propName : repsPropsName){
try {
String propVal = (String) item.getClass().getMethod(getterName(propName)).invoke(item);
String propEncrypt = AESUtil.encrypt(propVal,staticRequestKey);
item.getClass().getMethod(setterName(propName),String.class).invoke(item,propEncrypt);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
}catch (Exception e){
e.printStackTrace();
respObj = joinPoint.proceed();
}
return respObj;
}
private String getterName(String name){
String first = name.substring(0,1);
String last = name.substring(1);
return "get"+first.toUpperCase()+last;
}
private String setterName(String name){
String first = name.substring(0,1);
String last = name.substring(1);
return "set"+first.toUpperCase()+last;
}
最后把注解加到想要加密的字段上就行了,注意只能加密String