spring boot集成RSA加密数据传输

最近看有小伙伴在弄项目使用密文传输数据,然后我看了下,集成这个大佬的依赖不错,但是我发现他的注解不支持加在类上,我进行了一点改动后完美支持。
项目demo地址 https://github.com/songshijun1995/spring-boot-RSA
大致说下不引入依赖,直接集成的步骤。

  1. 配置yml文件,公钥和密钥可以用其他软件生成
rsa:
  encrypt:
    open: true # 是否开启加密 true  or  false
    showLog: true # 是否打印加解密log true  or  false
    # RSA公钥 软件生成
    publicKey: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCiquj4HR5VBtSJiKTyr18/vLaO5fJb6L/BrawFR4u+QCP3SF6SEd4pwp5Ev2R5pS34YGU00XFCejPgDfsSnRITanvv5a5wnNLFJMaz7ACxCrZjmW3z+ZppR/I19mJsaTOOopChUMJNBdvxUO13suwYad1Nhk5fmAJn0xQJgeWR/QIDAQAB
    # RSA私钥 软件生成
    privateKey: MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKKq6PgdHlUG1ImIpPKvXz+8to7l8lvov8GtrAVHi75AI/dIXpIR3inCnkS/ZHmlLfhgZTTRcUJ6M+AN+xKdEhNqe+/lrnCc0sUkxrPsALEKtmOZbfP5mmlH8jX2YmxpM46ikKFQwk0F2/FQ7Xey7Bhp3U2GTl+YAmfTFAmB5ZH9AgMBAAECgYANiipQFKRksWfZds08AgrslDmh1VQCAHKNnXYXDmh8Unxr5dMxV1llonRoBoJHec9EwElMRy6lOOS+fotqdjZ90yTGTl5wPLX7Yan+OfeOJCkDdPhIurcPPi0RYXKSRags/SX3mvkhauxGIUY0xPDJJDJ/Kaby7HrT9PQELSbV5QJBAN3zio37hJxH5G7T2WMNbxA5sB4Ey5rOc+f7c5fARFs9QC2vbIyHP73pR9igZ4Pm8OiMcuWLrK0kvZfuCuFruBcCQQC7ny61wekt6bfCCfK0mtZt+SEEINqDsXICIyzgEShfURWoIPKlUQseqhtLg7vkPErkBoA8I3y9ozejwV8zIT8LAkEAhAaCvMKIt43sTCCoh0tObZBjOvgPRR7Zw3zH3dT41G0y5/oZz94EBKvnmOyRptyRIUOqdPEI3lWkkeN/hWfWMQJBAKOXVUwHqsBss9vNfsD47RTwj1ghKUaAlu7EKuGoNDJ/6ckyCUAZ3P88xRXf5BlKdOZDwNYu/xn+0YnIFrDnQScCQFobGRWnckBrm/GZ59/9vl5H1Z2MSbuDHgJBuxe6cq5RUhxFMW/KJ4hgfeiwp7xfpt162yMGlcqPNHyCTf6bVnA=
  1. 新建两个工具类
package com.rsa.util;

import org.apache.commons.codec.binary.Base64;

public class Base64Util {
    public Base64Util() {
    }

    public static byte[] decode(String base64) throws Exception {
        return Base64.decodeBase64(base64);
    }

    public static String encode(byte[] bytes) throws Exception {
        return new String(Base64.encodeBase64(bytes));
    }
}
package com.rsa.util;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSAUtil {
    public static final String KEY_ALGORITHM = "RSA";
    private static final int MAX_ENCRYPT_BLOCK = 117;
    private static final int MAX_DECRYPT_BLOCK = 256;

    public RSAUtil() {
    }

    public static byte[] encrypt(byte[] data, String publicKey) throws Exception {
        byte[] keyBytes = Base64Util.decode(publicKey);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key publicK = keyFactory.generatePublic(x509KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, publicK);
        int inputLen = data.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段加密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }

        byte[] encryptedData = out.toByteArray();
        out.close();
        return encryptedData;
    }

    public static byte[] decrypt(byte[] text, String privateKey) throws Exception {
        byte[] keyBytes = Base64Util.decode(privateKey);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, privateK);
        int inputLen = text.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段解密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(text, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(text, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
        }

        byte[] decryptedData = out.toByteArray();
        out.close();
        return decryptedData;
    }
}
  1. 新建SecretKeyConfig配置文件,加载yml里的配置
package com.rsa.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "rsa.encrypt")
public class SecretKeyConfig {
    private String privateKey;
    private String publicKey;
    private String charset = "UTF-8";
    private boolean open = true;
    private boolean showLog = false;
}
  1. 添加三个注解,EnableSecurity、Decrypt、Encrypt
package com.rsa.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.rsa.advice.EncryptRequestBodyAdvice;
import com.rsa.advice.EncryptResponseBodyAdvice;
import com.rsa.config.SecretKeyConfig;
import org.springframework.context.annotation.Import;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Import({SecretKeyConfig.class, EncryptResponseBodyAdvice.class, EncryptRequestBodyAdvice.class})
public @interface EnableSecurity {
}

package com.rsa.annotation;

import java.lang.annotation.*;

//加密注解
@Target({ElementType.TYPE,ElementType.METHOD}) // 可以作用在类上和方法上
@Retention(RetentionPolicy.RUNTIME) // 运行时起作用
@Documented
public @interface Encrypt {
}

package com.rsa.annotation;

import java.lang.annotation.*;

//解密注解
@Target({ElementType.TYPE,ElementType.METHOD}) // 可以作用在类上和方法上
@Retention(RetentionPolicy.RUNTIME) // 运行时起作用
@Documented
public @interface Decrypt {
}
  1. 新建DecryptHttpInputMessage类
package com.rsa.advice;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;

import com.rsa.util.Base64Util;
import com.rsa.util.RSAUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;

public class DecryptHttpInputMessage implements HttpInputMessage {
    private final HttpHeaders headers;
    private final InputStream body;

    public DecryptHttpInputMessage(HttpInputMessage inputMessage, String privateKey, String charset, boolean showLog) throws Exception {
        if (StringUtils.isEmpty(privateKey)) {
            throw new IllegalArgumentException("privateKey is null");
        } else {
            this.headers = inputMessage.getHeaders();
            String content = (String)(new BufferedReader(new InputStreamReader(inputMessage.getBody()))).lines().collect(Collectors.joining(System.lineSeparator()));
            String decryptBody;
            Logger log = LoggerFactory.getLogger(this.getClass());
            if (content.startsWith("{")) {
                log.info("Unencrypted without decryption:{}", content);
                decryptBody = content;
            } else {
                StringBuilder json = new StringBuilder();
                content = content.replaceAll(" ", "+");
                if (!StringUtils.isEmpty(content)) {
                    String[] contents = content.split("\\|");
                    String[] var9 = contents;
                    int var10 = contents.length;

                    for(int var11 = 0; var11 < var10; ++var11) {
                        String value = var9[var11];
                        value = new String(RSAUtil.decrypt(Base64Util.decode(value), privateKey), charset);
                        json.append(value);
                    }
                }

                decryptBody = json.toString();
                if (showLog) {
                    log.info("Encrypted data received:{},After decryption:{}", content, decryptBody);
                }
            }

            this.body = new ByteArrayInputStream(decryptBody.getBytes());
        }
    }

    public InputStream getBody() {
        return this.body;
    }

    public HttpHeaders getHeaders() {
        return this.headers;
    }
}
  1. 新建EncryptRequestBodyAdvice类
package com.rsa.advice;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Objects;

import com.rsa.annotation.Decrypt;
import com.rsa.annotation.Encrypt;
import com.rsa.config.SecretKeyConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

@ControllerAdvice
public class EncryptRequestBodyAdvice implements RequestBodyAdvice {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private boolean encrypt;
    @Autowired
    private SecretKeyConfig secretKeyConfig;

    public EncryptRequestBodyAdvice() {
    }

    public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        Annotation[] annotations = methodParameter.getDeclaringClass().getAnnotations();
        if (annotations.length > 0 && this.secretKeyConfig.isOpen()) {
            for (Annotation annotation : annotations) {
                if (annotation instanceof Encrypt) {
                    return this.encrypt = true;
                }
            }
        }

       return this.encrypt = Objects.requireNonNull(methodParameter.getMethod()).isAnnotationPresent(Decrypt.class) && this.secretKeyConfig.isOpen();
    }

    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        if (this.encrypt) {
            try {
                return new DecryptHttpInputMessage(inputMessage, this.secretKeyConfig.getPrivateKey(), this.secretKeyConfig.getCharset(), this.secretKeyConfig.isShowLog());
            } catch (Exception var6) {
                this.log.error("Decryption failed", var6);
            }
        }

        return inputMessage;
    }

    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }
}
  1. 新建EncryptResponseBodyAdvice类
package com.rsa.advice;

import com.alibaba.fastjson.JSON;
import com.rsa.annotation.Encrypt;
import com.rsa.config.SecretKeyConfig;
import com.rsa.util.Base64Util;
import com.rsa.util.RSAUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.lang.annotation.Annotation;
import java.util.Objects;

@ControllerAdvice
public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<Object> {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private static final ThreadLocal<Boolean> encryptLocal = new ThreadLocal<>();
    private boolean encrypt;
    @Autowired
    private SecretKeyConfig secretKeyConfig;

    public EncryptResponseBodyAdvice() {
    }

    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        Annotation[] annotations = returnType.getDeclaringClass().getAnnotations();
        if (annotations.length > 0 && this.secretKeyConfig.isOpen()) {
            for (Annotation annotation : annotations) {
                if (annotation instanceof Encrypt) {
                    return this.encrypt = true;
                }
            }
        }
        return this.encrypt = Objects.requireNonNull(returnType.getMethod()).isAnnotationPresent(Encrypt.class) && this.secretKeyConfig.isOpen();
    }

    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        Boolean status = encryptLocal.get();
        if (null != status && !status) {
            encryptLocal.remove();
        } else {
            if (this.encrypt) {
                String publicKey = this.secretKeyConfig.getPublicKey();

                try {
                    String content = JSON.toJSONString(body);
                    if (!StringUtils.hasText(publicKey)) {
                        throw new NullPointerException("Please configure rsa.encrypt.publicKey parameter!");
                    }

                    byte[] data = content.getBytes();
                    byte[] encodedData = RSAUtil.encrypt(data, publicKey);
                    String result = Base64Util.encode(encodedData);
                    if (this.secretKeyConfig.isShowLog()) {
                        this.log.info("Pre-encrypted data:{},After encryption:{}", content, result);
                    }

                    return result;
                } catch (Exception var13) {
                    this.log.error("Encrypted data exception", var13);
                }
            }

        }
        return body;
    }
}
  1. 在启动类上加@EnableSecurity注解
  2. 新建测试类
package com.rsa.controller;

import com.alibaba.fastjson.JSON;
import com.rsa.annotation.Decrypt;
import com.rsa.annotation.Encrypt;
import com.rsa.entities.TestBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
//@Encrypt
//@Decrypt
@RestController
public class TestController {

    @Encrypt
    @GetMapping("/encryption")
    public TestBean encryption(){
        TestBean testBean = new TestBean();
        testBean.setUsername("小明觉得不错");
        testBean.setAge(18);
        return testBean;
    }

    @Decrypt
    @PostMapping("/decryption")
    public TestBean Decryption(@RequestBody String testBean) {
        log.info("testBean : [{}]", testBean);
        return JSON.parseObject(testBean, TestBean.class);
    }
}

启动项目,访问swagger地址进行测试

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容