Skip to content

Commit 3c5d8ac

Browse files
committed
build: 发布1.1.0版本
1 parent d031dbd commit 3c5d8ac

File tree

9 files changed

+46
-37
lines changed

9 files changed

+46
-37
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
- - [x] SHA-224 / 256 / 384 / 512
1313
- - [x] AES
1414
- - [x] DES
15-
- - [ ] RSA
15+
- - [x] RSA
1616
- 可进行解密的方式有:
1717
- - [x] AES
1818
- - [x] DES
19-
- - [ ] RSA
19+
- - [x] RSA
2020
## 使用方法
2121
-`pom.xml`中引入依赖:
2222
```xml
2323
<dependency>
2424
<groupId>cn.licoy</groupId>
2525
<artifactId>encrypt-body-spring-boot-starter</artifactId>
26-
<version>1.0.4.RELEASE</version>
26+
<version>1.1.0</version>
2727
</dependency>
2828
```
2929
- 在工程对应的`Application`类中增加@EnableEncryptBody注解,例如:

README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
- - [x] SHA-224 / 256 / 384 / 512
1414
- - [x] AES
1515
- - [x] DES
16-
- - [ ] RSA
16+
- - [x] RSA
1717
- There are ways to decrypt:
1818
- - [x] AES
1919
- - [x] DES
20-
- - [ ] RSA
20+
- - [x] RSA
2121
## Usage method
2222
- Introducing dependencies in `pom.xml`
2323
```xml
2424
<dependency>
2525
<groupId>cn.licoy</groupId>
2626
<artifactId>encrypt-body-spring-boot-starter</artifactId>
27-
<version>1.0.3.RELEASE</version>
27+
<version>1.1.0</version>
2828
</dependency>
2929
```
3030
- Add the @EnableEncryptBody annotation to the `Application` class corresponding to the project, for example:

pom.xml

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>cn.licoy</groupId>
88
<artifactId>encrypt-body-spring-boot-starter</artifactId>
9-
<version>1.0.3.RELEASE</version>
9+
<version>1.1.0</version>
1010

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

3232
<licenses>
@@ -162,7 +162,6 @@
162162
<plugin>
163163
<groupId>org.apache.maven.plugins</groupId>
164164
<artifactId>maven-gpg-plugin</artifactId>
165-
<version>1.5</version>
166165
<executions>
167166
<execution>
168167
<id>sign-artifacts</id>

src/main/java/cn/licoy/encryptbody/advice/DecryptRequestBodyAdvice.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private DecryptAnnotationInfoBean getMethodAnnotation(MethodParameter methodPara
126126
if (method.isAnnotationPresent(DESDecryptBody.class)) {
127127
DESDecryptBody decryptBody = methodParameter.getMethodAnnotation(DESDecryptBody.class);
128128
if (decryptBody != null) {
129-
return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.DES).key(decryptBody.value()).build();
129+
return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.DES).key(decryptBody.key()).build();
130130
}
131131
}
132132
if (method.isAnnotationPresent(AESDecryptBody.class)) {
@@ -135,8 +135,8 @@ private DecryptAnnotationInfoBean getMethodAnnotation(MethodParameter methodPara
135135
return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.AES).key(decryptBody.key()).build();
136136
}
137137
}
138-
if (method.isAnnotationPresent(RSAEncryptBody.class)) {
139-
RSAEncryptBody decryptBody = methodParameter.getMethodAnnotation(RSAEncryptBody.class);
138+
if (method.isAnnotationPresent(RSADecryptBody.class)) {
139+
RSADecryptBody decryptBody = methodParameter.getMethodAnnotation(RSADecryptBody.class);
140140
if (decryptBody != null) {
141141
return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.RSA).key(decryptBody.key()).rsaKeyType(decryptBody.type()).build();
142142
}
@@ -159,7 +159,7 @@ private DecryptAnnotationInfoBean getClassAnnotation(Class<?> clazz) {
159159
return DecryptAnnotationInfoBean.builder().decryptBodyMethod(decryptBody.value()).key(decryptBody.otherKey()).build();
160160
}
161161
if (annotation instanceof DESDecryptBody) {
162-
return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.DES).key(((DESDecryptBody) annotation).value()).build();
162+
return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.DES).key(((DESDecryptBody) annotation).key()).build();
163163
}
164164
if (annotation instanceof AESDecryptBody) {
165165
return DecryptAnnotationInfoBean.builder().decryptBodyMethod(DecryptBodyMethod.AES).key(((AESDecryptBody) annotation).key()).build();

src/main/java/cn/licoy/encryptbody/advice/EncryptResponseBodyAdvice.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
@Order(1)
4242
@ControllerAdvice
4343
@Slf4j
44-
public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<String> {
44+
public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<Object> {
4545

4646
private final ObjectMapper objectMapper;
4747

@@ -82,19 +82,24 @@ public boolean supports(MethodParameter returnType, Class converterType) {
8282
}
8383

8484
@Override
85-
public String beforeBodyWrite(String body, MethodParameter returnType, MediaType selectedContentType,
85+
public String beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
8686
Class<? extends HttpMessageConverter<?>> selectedConverterType,
8787
ServerHttpRequest request, ServerHttpResponse response) {
8888
if (body == null) {
8989
return null;
9090
}
91-
response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
92-
String str = null;
93-
try {
94-
str = objectMapper.writeValueAsString(body);
95-
} catch (JsonProcessingException e) {
96-
e.printStackTrace();
91+
String str;
92+
if (body instanceof String || body instanceof Number || body instanceof Boolean) {
93+
str = String.valueOf(body);
94+
} else {
95+
try {
96+
str = objectMapper.writeValueAsString(body);
97+
} catch (JsonProcessingException e) {
98+
e.printStackTrace();
99+
throw new EncryptBodyFailException(e.getMessage());
100+
}
97101
}
102+
response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
98103
EncryptAnnotationInfoBean classAnnotation = getClassAnnotation(returnType.getDeclaringClass());
99104
if (classAnnotation != null) {
100105
return switchEncrypt(str, classAnnotation);
@@ -257,4 +262,5 @@ private String switchEncrypt(String formatStringBody, EncryptAnnotationInfoBean
257262
}
258263
throw new EncryptBodyFailException();
259264
}
265+
260266
}

src/main/java/cn/licoy/encryptbody/annotation/EnableEncryptBody.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ file or class name and description of purpose be included on the
226226
@Import({EncryptBodyConfig.class,
227227
HttpConverterConfig.class,
228228
EncryptResponseBodyAdvice.class,
229-
DecryptRequestBodyAdvice.class,
230-
HttpConverterConfig.class})
229+
DecryptRequestBodyAdvice.class,})
231230
public @interface EnableEncryptBody {
232231
}

src/main/java/cn/licoy/encryptbody/annotation/decrypt/DESDecryptBody.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
@Documented
1313
public @interface DESDecryptBody {
1414

15-
String value() default "";
15+
String key() default "";
1616

1717
}

src/main/java/cn/licoy/encryptbody/config/HttpConverterConfig.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,29 @@
1414
import java.io.IOException;
1515
import java.lang.reflect.Type;
1616
import java.nio.charset.Charset;
17+
import java.nio.charset.StandardCharsets;
1718
import java.util.LinkedList;
1819
import java.util.List;
1920

2021
/**
2122
* <p>响应体数据处理,防止数据类型为String时再进行JSON数据转换,那么产生最终的结果可能被双引号包含...</p>
22-
*
2323
* @author licoy.cn
2424
* @version 2018/9/5
2525
*/
26+
@Configuration
2627
public class HttpConverterConfig implements WebMvcConfigurer {
2728

28-
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
29-
return new MappingJackson2HttpMessageConverter() {
29+
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
30+
return new MappingJackson2HttpMessageConverter(){
3031
@Override
3132
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
32-
if (object instanceof String) {
33+
if(object instanceof String){
3334
Charset charset = this.getDefaultCharset();
34-
if (charset != null) {
35-
StreamUtils.copy((String) object, charset, outputMessage.getBody());
35+
if(charset==null){
36+
charset = StandardCharsets.UTF_8;
3637
}
37-
} else {
38+
StreamUtils.copy((String)object, charset, outputMessage.getBody());
39+
}else{
3840
super.writeInternal(object, type, outputMessage);
3941
}
4042
}
@@ -44,10 +46,12 @@ protected void writeInternal(Object object, Type type, HttpOutputMessage outputM
4446
@Override
4547
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
4648
MappingJackson2HttpMessageConverter converter = mappingJackson2HttpMessageConverter();
47-
converter.setSupportedMediaTypes(new LinkedList<MediaType>() {{
48-
add(MediaType.TEXT_PLAIN);
49+
converter.setSupportedMediaTypes(new LinkedList<MediaType>(){{
50+
add(MediaType.TEXT_HTML);
51+
add(MediaType.APPLICATION_JSON_UTF8);
4952
}});
5053
converters.add(new StringHttpMessageConverter());
5154
converters.add(converter);
5255
}
5356
}
57+

src/main/java/cn/licoy/encryptbody/util/CommonUtils.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.licoy.encryptbody.util;
22

33
import cn.hutool.core.util.StrUtil;
4+
import cn.hutool.crypto.SecureUtil;
45
import cn.hutool.crypto.asymmetric.RSA;
56
import cn.licoy.encryptbody.bean.ISecurityInfo;
67
import cn.licoy.encryptbody.exception.IllegalSecurityTypeException;
@@ -28,10 +29,10 @@ public static RSA infoBeanToRsaInstance(ISecurityInfo info) {
2829
RSA rsa;
2930
switch (info.getRsaKeyType()) {
3031
case PUBLIC:
31-
rsa = new RSA(null, info.getKey().getBytes());
32+
rsa = new RSA(null, SecureUtil.decode(info.getKey()));
3233
break;
3334
case PRIVATE:
34-
rsa = new RSA(info.getKey().getBytes(), null);
35+
rsa = new RSA(SecureUtil.decode(info.getKey()), null);
3536
break;
3637
default:
3738
throw new IllegalSecurityTypeException();

0 commit comments

Comments
 (0)