All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nxyfan.framework.common.safe.DecryptGetAspect Maven / Gradle / Ivy

There is a newer version: 1.6.3
Show newest version
package com.nxyfan.framework.common.safe;

import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
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.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.nxyfan.framework.common.util.CommonCryptogramUtil;

/** 
 *
 * Describe: 对Get请求的参数进行解密
 * Author: Administrator  
 * Create Time: 2024年4月25日 下午2:35:06 
 * Copyright @ 2024 51LIFE  
 */
@Aspect
@Order(1)
@Component
public class DecryptGetAspect {

	// 切入点:只有使用了@CommonApiSafe注解的请求才会执行解密
    @Pointcut("@annotation(com.nxyfan.framework.common.annotation.CommonApiSafe)")
    public void pointcut() { }

    @Around("pointcut()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取request加密传递的参数
        HttpServletRequest request = getRequest();
        // 获取到请求的参数列表进行解密
        Object[] args = joinPoint.getArgs();
        Object[] newArgs = args;
        // get请求
        if(Objects.equals("GET", request.getMethod())) {
            //get请求参数解析
            this.decrypt(newArgs);
        }
        // 执行将解密的结果交给控制器进行处理,并返回处理结果
        return joinPoint.proceed(newArgs);
    }

    /**
     * 获取request
     */
    private HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest();
    }

    // 解密方法
    public void decrypt(Object[] args)  {
        //将请求参数数组进行遍历解密(多个参数)
        for(int i = 0; i < args.length; i++) {
            String encrypt = args[i].toString();
            // 将密文解密为JSON字符串
            String json = CommonCryptogramUtil.doSm2Decrypt(encrypt);
            //将解密后的内容赋予参数数组
            args[i] = json;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy