From 3c5d8ac0405c756d6ce3f5f1e2279b764d7c6388 Mon Sep 17 00:00:00 2001 From: Licoy Date: Tue, 29 Mar 2022 18:21:42 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E5=8F=91=E5=B8=831.1.0=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++--- README_EN.md | 6 ++--- pom.xml | 9 ++++---- .../advice/DecryptRequestBodyAdvice.java | 8 +++---- .../advice/EncryptResponseBodyAdvice.java | 22 ++++++++++++------- .../annotation/EnableEncryptBody.java | 3 +-- .../annotation/decrypt/DESDecryptBody.java | 2 +- .../config/HttpConverterConfig.java | 22 +++++++++++-------- .../licoy/encryptbody/util/CommonUtils.java | 5 +++-- 9 files changed, 46 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index c6a27b2..940249d 100644 --- a/README.md +++ b/README.md @@ -12,18 +12,18 @@ - - [x] SHA-224 / 256 / 384 / 512 - - [x] AES - - [x] DES - - - [ ] RSA + - - [x] RSA - 可进行解密的方式有: - - [x] AES - - [x] DES - - - [ ] RSA + - - [x] RSA ## 使用方法 - 在`pom.xml`中引入依赖: ```xml cn.licoy encrypt-body-spring-boot-starter - 1.0.4.RELEASE + 1.1.0 ``` - 在工程对应的`Application`类中增加@EnableEncryptBody注解,例如: diff --git a/README_EN.md b/README_EN.md index 7c993b5..b5194f2 100644 --- a/README_EN.md +++ b/README_EN.md @@ -13,18 +13,18 @@ - - [x] SHA-224 / 256 / 384 / 512 - - [x] AES - - [x] DES - - - [ ] RSA + - - [x] RSA - There are ways to decrypt: - - [x] AES - - [x] DES - - - [ ] RSA + - - [x] RSA ## Usage method - Introducing dependencies in `pom.xml`: ```xml cn.licoy encrypt-body-spring-boot-starter - 1.0.3.RELEASE + 1.1.0 ``` - Add the @EnableEncryptBody annotation to the `Application` class corresponding to the project, for example: diff --git a/pom.xml b/pom.xml index 31fcb0c..e463fcc 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cn.licoy encrypt-body-spring-boot-starter - 1.0.3.RELEASE + 1.1.0 encrypt-body-spring-boot-starter encrypt-body-spring-boot-starter是SpringBoot控制器统一的响应体加密与请求体解密的注解处理方式,支持MD5/SHA/AES/DES/RSA @@ -24,9 +24,9 @@ 1.8 5.7.22 1.18.22 - 1.6.12 - 3.0.1 - 3.3.2 + 1.6.3 + 1.5 + 2.9.1 @@ -162,7 +162,6 @@ org.apache.maven.plugins maven-gpg-plugin - 1.5 sign-artifacts diff --git a/src/main/java/cn/licoy/encryptbody/advice/DecryptRequestBodyAdvice.java b/src/main/java/cn/licoy/encryptbody/advice/DecryptRequestBodyAdvice.java index 1682911..38cc43d 100644 --- a/src/main/java/cn/licoy/encryptbody/advice/DecryptRequestBodyAdvice.java +++ b/src/main/java/cn/licoy/encryptbody/advice/DecryptRequestBodyAdvice.java @@ -126,7 +126,7 @@ private DecryptAnnotationInfoBean getMethodAnnotation(MethodParameter methodPara if (method.isAnnotationPresent(DESDecryptBody.class)) { DESDecryptBody decryptBody = methodParameter.getMethodAnnotation(DESDecryptBody.class); if (decryptBody != null) { - return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.DES).key(decryptBody.value()).build(); + return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.DES).key(decryptBody.key()).build(); } } if (method.isAnnotationPresent(AESDecryptBody.class)) { @@ -135,8 +135,8 @@ private DecryptAnnotationInfoBean getMethodAnnotation(MethodParameter methodPara return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.AES).key(decryptBody.key()).build(); } } - if (method.isAnnotationPresent(RSAEncryptBody.class)) { - RSAEncryptBody decryptBody = methodParameter.getMethodAnnotation(RSAEncryptBody.class); + if (method.isAnnotationPresent(RSADecryptBody.class)) { + RSADecryptBody decryptBody = methodParameter.getMethodAnnotation(RSADecryptBody.class); if (decryptBody != null) { return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.RSA).key(decryptBody.key()).rsaKeyType(decryptBody.type()).build(); } @@ -159,7 +159,7 @@ private DecryptAnnotationInfoBean getClassAnnotation(Class clazz) { return DecryptAnnotationInfoBean.builder().decryptBodyMethod(decryptBody.value()).key(decryptBody.otherKey()).build(); } if (annotation instanceof DESDecryptBody) { - return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.DES).key(((DESDecryptBody) annotation).value()).build(); + return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.DES).key(((DESDecryptBody) annotation).key()).build(); } if (annotation instanceof AESDecryptBody) { return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.AES).key(((AESDecryptBody) annotation).key()).build(); diff --git a/src/main/java/cn/licoy/encryptbody/advice/EncryptResponseBodyAdvice.java b/src/main/java/cn/licoy/encryptbody/advice/EncryptResponseBodyAdvice.java index e1b4c06..6a91446 100644 --- a/src/main/java/cn/licoy/encryptbody/advice/EncryptResponseBodyAdvice.java +++ b/src/main/java/cn/licoy/encryptbody/advice/EncryptResponseBodyAdvice.java @@ -41,7 +41,7 @@ @Order(1) @ControllerAdvice @Slf4j -public class EncryptResponseBodyAdvice implements ResponseBodyAdvice { +public class EncryptResponseBodyAdvice implements ResponseBodyAdvice { private final ObjectMapper objectMapper; @@ -82,19 +82,24 @@ public boolean supports(MethodParameter returnType, Class converterType) { } @Override - public String beforeBodyWrite(String body, MethodParameter returnType, MediaType selectedContentType, + public String beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { if (body == null) { return null; } - response.getHeaders().setContentType(MediaType.TEXT_PLAIN); - String str = null; - try { - str = objectMapper.writeValueAsString(body); - } catch (JsonProcessingException e) { - e.printStackTrace(); + String str; + if (body instanceof String || body instanceof Number || body instanceof Boolean) { + str = String.valueOf(body); + } else { + try { + str = objectMapper.writeValueAsString(body); + } catch (JsonProcessingException e) { + e.printStackTrace(); + throw new EncryptBodyFailException(e.getMessage()); + } } + response.getHeaders().setContentType(MediaType.TEXT_PLAIN); EncryptAnnotationInfoBean classAnnotation = getClassAnnotation(returnType.getDeclaringClass()); if (classAnnotation != null) { return switchEncrypt(str, classAnnotation); @@ -257,4 +262,5 @@ private String switchEncrypt(String formatStringBody, EncryptAnnotationInfoBean } throw new EncryptBodyFailException(); } + } diff --git a/src/main/java/cn/licoy/encryptbody/annotation/EnableEncryptBody.java b/src/main/java/cn/licoy/encryptbody/annotation/EnableEncryptBody.java index c00666f..d5b6370 100644 --- a/src/main/java/cn/licoy/encryptbody/annotation/EnableEncryptBody.java +++ b/src/main/java/cn/licoy/encryptbody/annotation/EnableEncryptBody.java @@ -226,7 +226,6 @@ file or class name and description of purpose be included on the @Import({EncryptBodyConfig.class, HttpConverterConfig.class, EncryptResponseBodyAdvice.class, - DecryptRequestBodyAdvice.class, - HttpConverterConfig.class}) + DecryptRequestBodyAdvice.class,}) public @interface EnableEncryptBody { } diff --git a/src/main/java/cn/licoy/encryptbody/annotation/decrypt/DESDecryptBody.java b/src/main/java/cn/licoy/encryptbody/annotation/decrypt/DESDecryptBody.java index 184a6a7..9b4966c 100644 --- a/src/main/java/cn/licoy/encryptbody/annotation/decrypt/DESDecryptBody.java +++ b/src/main/java/cn/licoy/encryptbody/annotation/decrypt/DESDecryptBody.java @@ -12,6 +12,6 @@ @Documented public @interface DESDecryptBody { - String value() default ""; + String key() default ""; } diff --git a/src/main/java/cn/licoy/encryptbody/config/HttpConverterConfig.java b/src/main/java/cn/licoy/encryptbody/config/HttpConverterConfig.java index 45107b4..1852de4 100644 --- a/src/main/java/cn/licoy/encryptbody/config/HttpConverterConfig.java +++ b/src/main/java/cn/licoy/encryptbody/config/HttpConverterConfig.java @@ -14,27 +14,29 @@ import java.io.IOException; import java.lang.reflect.Type; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.LinkedList; import java.util.List; /** *

响应体数据处理,防止数据类型为String时再进行JSON数据转换,那么产生最终的结果可能被双引号包含...

- * * @author licoy.cn * @version 2018/9/5 */ +@Configuration public class HttpConverterConfig implements WebMvcConfigurer { - public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { - return new MappingJackson2HttpMessageConverter() { + public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){ + return new MappingJackson2HttpMessageConverter(){ @Override protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { - if (object instanceof String) { + if(object instanceof String){ Charset charset = this.getDefaultCharset(); - if (charset != null) { - StreamUtils.copy((String) object, charset, outputMessage.getBody()); + if(charset==null){ + charset = StandardCharsets.UTF_8; } - } else { + StreamUtils.copy((String)object, charset, outputMessage.getBody()); + }else{ super.writeInternal(object, type, outputMessage); } } @@ -44,10 +46,12 @@ protected void writeInternal(Object object, Type type, HttpOutputMessage outputM @Override public void configureMessageConverters(List> converters) { MappingJackson2HttpMessageConverter converter = mappingJackson2HttpMessageConverter(); - converter.setSupportedMediaTypes(new LinkedList() {{ - add(MediaType.TEXT_PLAIN); + converter.setSupportedMediaTypes(new LinkedList(){{ + add(MediaType.TEXT_HTML); + add(MediaType.APPLICATION_JSON_UTF8); }}); converters.add(new StringHttpMessageConverter()); converters.add(converter); } } + diff --git a/src/main/java/cn/licoy/encryptbody/util/CommonUtils.java b/src/main/java/cn/licoy/encryptbody/util/CommonUtils.java index 4be31bf..483f4ef 100644 --- a/src/main/java/cn/licoy/encryptbody/util/CommonUtils.java +++ b/src/main/java/cn/licoy/encryptbody/util/CommonUtils.java @@ -1,6 +1,7 @@ package cn.licoy.encryptbody.util; import cn.hutool.core.util.StrUtil; +import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.asymmetric.RSA; import cn.licoy.encryptbody.bean.ISecurityInfo; import cn.licoy.encryptbody.exception.IllegalSecurityTypeException; @@ -28,10 +29,10 @@ public static RSA infoBeanToRsaInstance(ISecurityInfo info) { RSA rsa; switch (info.getRsaKeyType()) { case PUBLIC: - rsa = new RSA(null, info.getKey().getBytes()); + rsa = new RSA(null, SecureUtil.decode(info.getKey())); break; case PRIVATE: - rsa = new RSA(info.getKey().getBytes(), null); + rsa = new RSA(SecureUtil.decode(info.getKey()), null); break; default: throw new IllegalSecurityTypeException();