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) { //

相关内容

热门资讯

推荐一款!wpk是真的还是假的... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
一分钟揭秘!!wepoker怎... 一分钟揭秘!!wepoker怎么看牌型,WPK可以开挂吗安装(一贯是有开挂)AI 是一款可以让一直输...
玩家必备教程!wepoker插... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
玩家必看教程!!wejoker... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
玩家必看科普!wepoker游... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wepoker游戏安装教程解析、 A...
一分钟了解!!wepoker养... 一分钟了解!!wepoker养号规律,WPK开挂脚本教程(确实存在有辅助挂)AI 智能体黑科技再上新...
一分钟揭秘!wpk俱乐部是做什... 您好:wpk俱乐部是做什么的这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
一起来讨论!德扑之星辅助器,德... 德扑之星辅助器AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据包,断...
一分钟了解!!wepoker有... 很多人使用云端 AI 时,都担心wepoker有脚本吗、内部资料上传带来隐私风险。这款本地运行的 A...
实测教程!wpk透视工作室,德... 实测教程!wpk透视工作室,德扑之星透视挂插件(固有真的有辅助挂)主打本地运行的 AI 黑科技工具迎...