设为首页 加入收藏

TOP

基于拦截器+mybatis+注解 实现对敏感字段进行加解密(三)
2023-07-26 08:16:12 】 浏览:128
Tags:于拦截 mybatis 注解 段进行 加解密
算法
*/ private final String KEY_ALGORITHM = "AES"; /** * 算法/模式/补码方式 */ private final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding"; /** * 编码格式 */ private final String CODE = "utf-8"; /** * base64验证规则 */ private final String BASE64_RULE = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$"; /** * 正则验证对象 */ private final Pattern PATTERN = Pattern.compile(BASE64_RULE); /** * 加解密 密钥key */ @Value("${encrypt.key}") public static String encryptKey; /** * @param content 加密字符串 * @return 加密结果 */ public String encrypt(String content) { return encrypt(content, encryptKey); } /** * 加密 * * @param content 加密参数 * @param key 加密key * @return 结果字符串 */ public String encrypt(String content, String key) { //判断如果已经是base64加密字符串则返回原字符串 if (isBase64(content)) { return content; } byte[] encrypted = encrypt2bytes(content, key); if (null == encrypted || encrypted.length < 1) { log.info("加密字符串[{}]转字节为null", content); return null; } return Base64Utils.encodeToString(encrypted); } /** * @param content 加密字符串 * @param key 加密key * @return 返回加密字节 */ public byte[] encrypt2bytes(String content, String key) { try { byte[] raw = key.getBytes(CODE); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, KEY_ALGORITHM); Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); return cipher.doFinal(content.getBytes(CODE)); } catch (Exception e) { log.error("failed to encrypt: {} of {}", content, e); return null; } } /** * @param content 加密字符串 * @return 返回加密结果 */ public String decrypt(String content) { try { return decrypt(content, encryptKey); } catch (Exception e) { log.error("failed to decrypt: {}, e: {}", content, e); return null; } } /** * 解密 * * @param content 解密字符串 * @param key 解密key * @return 解密结果 */ public String decrypt(String content, String key) throws Exception { //不是base64格式字符串则不进行解密 if (!isBase64(content)) { return content; } return decrypt(Base64Utils.decodeFromString(content), key); } /** * @param content 解密字节 * @param key 解密key * @return 返回解密内容 */ public String decrypt(byte[] content, String key) throws Exception { if (key == null) { log.error("AES key should not be null"); return null; } byte[] raw = key.getBytes(CODE); SecretKeySpec keySpec = new SecretKeySpec(raw, KEY_ALGORITHM); Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, keySpec); try { byte[] original = cipher.doFinal(content); return new String(original, CODE); } catch (Exception e) { log.error("failed to decrypt content: {}/ key: {}, e: {}", content, key, e); return null; } } /** * 判断是否为 base64加密 * * @param str 参数 * @return 结果 */ private boolean isBase64(String str) { Matcher matcher = PATTERN.matcher(str); return matcher.matches(); } }
  • 4. 自定义业务处理Service
import *****************.SensitiveField;
import *****************.AesService;
import *****************.AesFieldUtils;
import *****************.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.Objects;

/**
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Spring Cloud 2022.0.1 Spring Cl.. 下一篇springboot 多数据源 实例(sybas..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目