Skip to content

Commit

Permalink
build: 发布1.1.0版本
Browse files Browse the repository at this point in the history
  • Loading branch information
Licoy committed Mar 29, 2022
1 parent d031dbd commit 3c5d8ac
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 37 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<dependency>
<groupId>cn.licoy</groupId>
<artifactId>encrypt-body-spring-boot-starter</artifactId>
<version>1.0.4.RELEASE</version>
<version>1.1.0</version>
</dependency>
```
- 在工程对应的`Application`类中增加@EnableEncryptBody注解,例如:
Expand Down
6 changes: 3 additions & 3 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<dependency>
<groupId>cn.licoy</groupId>
<artifactId>encrypt-body-spring-boot-starter</artifactId>
<version>1.0.3.RELEASE</version>
<version>1.1.0</version>
</dependency>
```
- Add the @EnableEncryptBody annotation to the `Application` class corresponding to the project, for example:
Expand Down
9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cn.licoy</groupId>
<artifactId>encrypt-body-spring-boot-starter</artifactId>
<version>1.0.3.RELEASE</version>
<version>1.1.0</version>

<name>encrypt-body-spring-boot-starter</name>
<description>encrypt-body-spring-boot-starter是SpringBoot控制器统一的响应体加密与请求体解密的注解处理方式,支持MD5/SHA/AES/DES/RSA</description>
Expand All @@ -24,9 +24,9 @@
<java.version>1.8</java.version>
<hutool.version>5.7.22</hutool.version>
<lombok.version>1.18.22</lombok.version>
<nexus-staging-maven-plugin.version>1.6.12</nexus-staging-maven-plugin.version>
<maven-gpg-plugin.version>3.0.1</maven-gpg-plugin.version>
<maven-javadoc-plugin.version>3.3.2</maven-javadoc-plugin.version>
<nexus-staging-maven-plugin.version>1.6.3</nexus-staging-maven-plugin.version>
<maven-gpg-plugin.version>1.5</maven-gpg-plugin.version>
<maven-javadoc-plugin.version>2.9.1</maven-javadoc-plugin.version>
</properties>

<licenses>
Expand Down Expand Up @@ -162,7 +162,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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();
}
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
@Order(1)
@ControllerAdvice
@Slf4j
public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<String> {
public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<Object> {

private final ObjectMapper objectMapper;

Expand Down Expand Up @@ -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<? extends HttpMessageConverter<?>> 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);
Expand Down Expand Up @@ -257,4 +262,5 @@ private String switchEncrypt(String formatStringBody, EncryptAnnotationInfoBean
}
throw new EncryptBodyFailException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
@Documented
public @interface DESDecryptBody {

String value() default "";
String key() default "";

}
22 changes: 13 additions & 9 deletions src/main/java/cn/licoy/encryptbody/config/HttpConverterConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <p>响应体数据处理,防止数据类型为String时再进行JSON数据转换,那么产生最终的结果可能被双引号包含...</p>
*
* @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);
}
}
Expand All @@ -44,10 +46,12 @@ protected void writeInternal(Object object, Type type, HttpOutputMessage outputM
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter converter = mappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(new LinkedList<MediaType>() {{
add(MediaType.TEXT_PLAIN);
converter.setSupportedMediaTypes(new LinkedList<MediaType>(){{
add(MediaType.TEXT_HTML);
add(MediaType.APPLICATION_JSON_UTF8);
}});
converters.add(new StringHttpMessageConverter());
converters.add(converter);
}
}

5 changes: 3 additions & 2 deletions src/main/java/cn/licoy/encryptbody/util/CommonUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3c5d8ac

Please sign in to comment.