spring boot通过aop实现对接口响应数据加密--响应时间较慢

首选创建一个注解类

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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值