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

相关内容

热门资讯

玩家爆料!微信泉州麻将有挂的吗... 玩家爆料!微信泉州麻将有挂的吗(透视)原来是真的有挂(2023已更新)(哔哩哔哩)该软件可以轻松地帮...
2024教程!快玩炸翻天app... 2024教程!快玩炸翻天app辅助(透明挂)总是真的有挂(2020已更新)(哔哩哔哩);1、完成快玩...
发现玩家!白金岛跑胡子怎么搞胜... 发现玩家!白金岛跑胡子怎么搞胜率(透视)果然真的是有挂(2020已更新)(哔哩哔哩);1、用户打开应...
专业讨论!赣牌圈的隐藏机制(透... 专业讨论!赣牌圈的隐藏机制(透视辅助)一直存在有挂(2023已更新)(哔哩哔哩)1、构建自己的赣牌圈...
玩家科普!大凉山生活号有没有挂... 玩家科普!大凉山生活号有没有挂(辅助挂)竟然有挂(2024已更新)(哔哩哔哩)1、完成大凉山生活号有...
必看攻略!决战棋牌有挂吗(透视... 必看攻略!决战棋牌有挂吗(透视)切实真的有挂(2020已更新)(哔哩哔哩)1)决战棋牌有挂吗辅助挂:...
一分钟揭秘!老友互娱外 挂(透... 一分钟揭秘!老友互娱外 挂(透明挂)确实真的有挂(2025已更新)(哔哩哔哩);1、在老友互娱外 挂...
玩家必看!中至赣牌圈小程序怎么... 玩家必看!中至赣牌圈小程序怎么能摸好牌(辅助挂)切实真的有挂(2021已更新)(哔哩哔哩)1、玩家必...
推荐一款!中至上饶打炸提高好牌... 推荐一款!中至上饶打炸提高好牌几率(透视辅助)都是是真的有挂(2023已更新)(哔哩哔哩);1、用户...
每日必看教程!德州ai辅助软件... 每日必看教程!德州ai辅助软件(辅助挂)都是存在有挂(2020已更新)(哔哩哔哩)1、用户打开应用后...