Springboot使用AOP机制实现数据加密

本文采用aop实现model中的字段加解密。

创建加密工具类 AseUtil

import org.springframework.util.Base64Utils;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


/**
 * Ase加密
 */
public class AseUtil {
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默认的加密算法

    /**
     * AES 加密操作
     *
     * @param content  待加密内容
     * @param secKey 加密密码
     * @return 返回Base64转码后的加密数据
     */
    public static String encrypt(String content, String secKey) {
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(secKey));// 初始化为加密模式的密码器
            byte[] result = cipher.doFinal(byteContent);// 加密
            return Base64Utils.encodeToString(result);
        } catch (Exception ex) {
            Logger.getLogger(AseUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    /**
     * AES 解密操作
     *
     * @param content
     * @param secKey
     * @return
     */
    public static String decrypt(String content, String secKey) {
        try {
            //实例化
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            //使用密钥初始化,设置为解密模式
            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(secKey));
            //执行操作
            byte[] result = cipher.doFinal(Base64Utils.decodeFromString(content));
            return new String(result, "utf-8");
        } catch (Exception ex) {
            Logger.getLogger(AseUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    /**
     * 生成加密秘钥
     *
     * @return
     */
    private static SecretKeySpec getSecretKey(final String password) {
        //返回生成指定算法密钥生成器的 KeyGenerator 对象
        KeyGenerator kg = null;
        try {
            kg = KeyGenerator.getInstance(KEY_ALGORITHM);
            //AES 要求密钥长度为 128
            kg.init(128, new SecureRandom(password.getBytes()));
            //生成一个密钥
            SecretKey secretKey = kg.generateKey();
            return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);// 转换为AES专用密钥
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(AseUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}

定义注解

@EncryptMethod

使用@EncryptMethod注解要加密参数(model)的方法。

/**
 * 字段加解密
 * @author seawish
 * @date 2019/8/19
 */
@Documented
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Order(Ordered.HIGHEST_PRECEDENCE)
public @interface EncryptField {}

@DecryptMethod

使用@DecryptMethod注解要加密参数(model)的方法。

/**
 * 注解需要加解密参数的方法,实现自动加解密。
 *
 * @author seawish.zheng
 * @date 2019/8/16
 */
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Order(Ordered.HIGHEST_PRECEDENCE)
public @interface DecryptMethod { }

@CryptField

@CryptField修饰model类的属性,被注解的属性将被aop拦截,进行加解密处理。

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import java.lang.annotation.*;
/**
 * 字段加解密
 * @author seawish.zheng
 * @date 2019/8/19
 */
@Documented
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Order(Ordered.HIGHEST_PRECEDENCE)
public @interface CryptField {}

定义切面类 CryptFieldAspect

CryptFieldAspect拦截@EncryptMethod和@DecryptMethod修饰的方法,并对其第一个参数进行的特定属性(使用@CryptField注解的属性)进行加解密处理。

import com.dji.graduate.utils.AseUtil;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * 安全字段加密解密切面
 *
 * @author: zetting
 * @date:2018/12/27
 */
@Order(Ordered.HIGHEST_PRECEDENCE)
@Aspect
@Component
public class CryptFieldAspect {
    private static Logger logger = LoggerFactory.getLogger(CryptFieldAspect.class);
    @Value("${crypt.seckey}")
    private String secretKey;

    @Pointcut("@annotation(EncryptMethod)")
    public void encryptPointCut() {
    }

    @Pointcut("@annotation(DecryptMethod)")
    public void decryptPointCut() {
    }

    /**
     * 加密数据环绕处理
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("encryptPointCut()")
    public Object aroundEncrypt(ProceedingJoinPoint joinPoint) throws Throwable {
        Object requestObj = joinPoint.getArgs()[0];
        handleEncrypt(requestObj); // 加密CryptField注解字段
        Object responseObj = joinPoint.proceed(); // 方法执行
        handleDecrypt(responseObj); // 解密CryptField注解字段

        return responseObj;
    }

    /**
     * 解密数据环绕处理
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("decryptPointCut()")
    public Object aroundDecrypt(ProceedingJoinPoint joinPoint) throws Throwable {
        Object requestObj = joinPoint.getArgs()[0];
        handleDecrypt(requestObj); // 解密CryptField注解字段
        Object responseObj = joinPoint.proceed(); // 方法执行
        handleEncrypt(responseObj); // 加密CryptField注解字段
        return responseObj;
    }

    /**
     * 处理加密
     *
     * @param requestObj
     */
    private void handleEncrypt(Object requestObj) throws IllegalAccessException {
        if (Objects.isNull(requestObj)) {
            return;
        }
        Field[] fields = requestObj.getClass().getDeclaredFields();
        for (Field field : fields) {
            boolean hasSecureField = field.isAnnotationPresent(CryptField.class);
            if (hasSecureField) {
                field.setAccessible(true);
                String plaintextValue = (String) field.get(requestObj);
                String encryptValue = AseUtil.encrypt(plaintextValue, secretKey);
                field.set(requestObj, encryptValue);
            }
        }
    }

    /**
     * 处理解密
     *
     * @param responseObj
     */
    private Object handleDecrypt(Object responseObj) throws IllegalAccessException {
        if (Objects.isNull(responseObj)) {
            return null;
        }

        Field[] fields = responseObj.getClass().getDeclaredFields();
        for (Field field : fields) {
            boolean hasSecureField = field.isAnnotationPresent(CryptField.class);
            if (hasSecureField) {
                field.setAccessible(true);
                String encryptValue = (String) field.get(responseObj);
                String plaintextValue = AseUtil.decrypt(encryptValue, secretKey);
                field.set(responseObj, plaintextValue);
            }
        }
        return responseObj;
    }
}

切面使用

配置属性

在application.properties中配置aes加密所需的seckey。

crypt.seckey = seawishkey

注解方法和字段

注解Dto对象的字段

AccountDto的password属性使用@CryptField注解,其将被aop拦截,进行加解密处理。

@ApiModel(value = "用户Dto")
public class AccountDto extends BaseDto {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(hidden = true)
    @CryptField
    @NotEmpty(groups = {InsertGroup.class})
    @Size(min = 8, max = 20)
    private String password;

注解AddCount方法

addAccount方法使用@EncryptMethod注解,其第一个参数,也就是accountDto对象,将被aop拦截。在方法执行前,accountDto具有@CryptFiled注解的属性都会被加密;而执行完方法后,该对象被加密的字段都会被解密回原样。
如果使用@DecryptMethod注解addAccount方法,则执行@EncryptMethod相反的操作。

   @ApiOperation("添加用户信息")
    @EncryptMethod
    @PostMapping
    @ResponseBody
    public Result addAccount(@Validated({Default.class, InsertGroup.class}) @RequestBody AccountDto accountDto) throws InvocationTargetException, IllegalAccessException {
        logger.info("添加用户,ad: {}", accountDto.getAd());

        return accountService.add(accountDto) ? Result.success(accountDto.getId()) : Result.failureMsg("增加用户失败");
    }

参考文献

  • AOP,自定义注解,反射机制实现前端数据在后端自动解密
  • 【Springboot之切面编程】注解实现敏感字段加解密

本文作者: seawish
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!

你可能感兴趣的:(Springboot使用AOP机制实现数据加密)