AOP实现返回值解密
创始人
2024-09-03 08:00:34
0

使用AOP技术对返回值进行解密处理,下面是示例代码:

统一返回值对象类:

public class Result {
    private int code;
    private String msg;
    private Object data;
    // 省略...
    // getter/setter...
}

自定义加解密类:

@Component
public class MyPasswordEncoder {
    private final String secret = "my_secret_key";

    // 加密
    public String encode(String str) {
        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        byte[] key = secret.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] result = cipher.doFinal(bytes);
            return Base64.getEncoder().encodeToString(result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // 解密
    public String decode(String str) {
        byte[] bytes = Base64.getDecoder().decode(str);
        byte[] key = secret.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] result = cipher.doFinal(bytes);
            return new String(result, StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

AOP切面类:

@Component
@Aspect
public class DecryptAspect {
    @Autowired
    private MyPasswordEncoder myPasswordEncoder;

    @Pointcut("execution(public * com.example.demo.controller..*.*(..))")
    public void decrypt() {
    }

    @Around("decrypt()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        Object result = pjp.proceed(args); // 执行业务方法
        if (result instanceof Result) { // 判断返回值类型为Result
            Result r = (Result) result;
            Object data = r.getData();
            if (data instanceof String) { //

相关内容

热门资讯

透视神器!hardrock透视... 透视神器!hardrock透视工具,切实是有挂(透视)曝光教程(有挂攻略);1、上手简单,内置详细流...
透视透视!智星菠萝有挂吗,we... 透视透视!智星菠萝有挂吗,werplan怎么作弊,细节揭秘(有挂详情)1、下载好智星菠萝有挂吗辅助软...
透视肯定!wepoker破解是... 透视肯定!wepoker破解是真的还是假的,一直真的是有挂(透视)科技教程(有挂规律);1、这是跨平...
透视教程!智星德州有脚本吗,约... 透视教程!智星德州有脚本吗,约局吧开挂,教你教程(有挂介绍)进入游戏-大厅左侧-新手福利-激活码辅助...
透视有挂!epoker免费透视... 透视有挂!epoker免费透视脚本,最初存在有挂(透视)新2025版(有挂规律)1、epoker免费...
透视教程!pokemmo手机版... 透视教程!pokemmo手机版修改器,大菠萝789辅助器下载,攻略教程(有挂介绍);大菠萝789辅助...
透视有挂!wepoker私局代... 透视有挂!wepoker私局代打,切实真的是有挂(透视)黑科技教程(有挂插件)1、上手简单,内置详细...
透视好牌!pokemmo脚本辅... 透视好牌!pokemmo脚本辅助器,德州机器人代打脚本,揭秘教程(有挂脚本)1、德州机器人代打脚本a...
透视最新!wpk透视辅助靠谱吗... 透视最新!wpk透视辅助靠谱吗,先前真的是有挂(透视)安装教程(有挂黑科技)亲,关键说明,wpk透视...
透视新版!cloudpoker... 透视新版!cloudpoker怎么开挂,德州透视是真的假的,揭秘攻略(有挂攻略)在进入德州透视是真的...