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

相关内容

热门资讯

详细说明!微扑克平台(wEpo... 微扑克平台新手教程相关信息汇总(需添加指定薇757446909获取下载链接);详细说明!微扑克平台(...
分享一款(德州之星软件)外挂透... 分享一款(德州之星软件)外挂透明挂辅助挂(辅助挂)透视辅助(真的有挂)-哔哩哔哩德州之星软件平台为新...
透明挂透视(wEpoke)外挂... 透明挂透视(wEpoke)外挂透明挂辅助机制(透视)确实真的有挂(2025已更新)(哔哩哔哩);一、...
透明教学(智星德州菠萝)外挂透... WePoker透视辅助版本稳定性对比与推荐‌:透明教学(智星德州菠萝)外挂透明挂辅助工具(透视)透视...
实测分享!德扑手机上(Wepo... WePoker透视辅助版本稳定性对比与推荐‌:实测分享!德扑手机上(WepokE)外挂透明挂辅助软件...
透视模拟器(德扑之星软件)外挂... 透视模拟器(德扑之星软件)外挂透明挂辅助器(辅助挂)果真真的有挂(2024已更新)(哔哩哔哩);人气...
辅助透视(德扑之星记分牌)外挂... 辅助透视(德扑之星记分牌)外挂透明挂辅助器安装(辅助挂)透视辅助(有挂分享)-哔哩哔哩;免费德扑之星...
重大通报(aapoker辅助工... 重大通报(aapoker辅助工具)外挂透明挂辅助器(辅助挂)透视辅助(有挂解惑)-哔哩哔哩关于aap...
今日头条!wpk插件挂(WEp... 今日头条!wpk插件挂(WEpoke)外挂透明挂辅助APP(辅助挂)科技教程(详细教程)-哔哩哔哩1...
一分钟了解(德扑平台)外挂透明... 《德扑平台软件透明挂》是一款多人竞技的德扑平台辅助透视游戏,你将微扑克对手来到同一个战场,为至高无上...