Skip to content

Commit 311880f

Browse files
committed
feat: 加解密、签名
1 parent 16c0fb2 commit 311880f

3 files changed

Lines changed: 33 additions & 28 deletions

File tree

application-module/support-module/support-service-gateway/src/main/java/org/smartframework/cloud/examples/support/gateway/filter/access/core/datasecurity/DataSecurityFilter.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
import org.smartframework.cloud.examples.support.gateway.cache.ApiAccessMetaCache;
2323
import org.smartframework.cloud.examples.support.gateway.constants.GatewayReturnCodes;
2424
import org.smartframework.cloud.examples.support.gateway.constants.Order;
25-
import org.smartframework.cloud.examples.support.gateway.exception.UnsupportedFunctionException;
2625
import org.smartframework.cloud.examples.support.gateway.filter.FilterContext;
2726
import org.smartframework.cloud.examples.support.gateway.filter.access.AbstractFilter;
28-
import org.smartframework.cloud.examples.support.gateway.util.RewriteHttpUtil;
2927
import org.springframework.stereotype.Component;
3028
import org.springframework.web.server.ServerWebExchange;
3129
import org.springframework.web.server.WebFilterChain;
@@ -55,10 +53,6 @@ protected Mono<Void> innerFilter(ServerWebExchange exchange, WebFilterChain chai
5553
return chain.filter(exchange);
5654
}
5755

58-
if (!RewriteHttpUtil.isSupported(exchange.getRequest().getHeaders().getContentType())) {
59-
throw new UnsupportedFunctionException(GatewayReturnCodes.NOT_SUPPORT_DATA_SECURITY);
60-
}
61-
6256
String token = filterContext.getToken();
6357
if (StringUtils.isBlank(token)) {
6458
throw new ParamValidateException(GatewayReturnCodes.TOKEN_MISSING);

application-module/support-module/support-service-gateway/src/main/java/org/smartframework/cloud/examples/support/gateway/filter/access/core/datasecurity/DataSecurityServerHttpRequestDecorator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
import org.smartframework.cloud.examples.support.gateway.constants.GatewayReturnCodes;
3131
import org.smartframework.cloud.examples.support.gateway.exception.AesKeyNotFoundException;
3232
import org.smartframework.cloud.examples.support.gateway.exception.RequestSignFailException;
33+
import org.smartframework.cloud.examples.support.gateway.exception.UnsupportedFunctionException;
3334
import org.smartframework.cloud.examples.support.gateway.filter.rewrite.RewriteServerHttpRequestDecorator;
3435
import org.smartframework.cloud.examples.support.gateway.util.RedisKeyHelper;
36+
import org.smartframework.cloud.examples.support.gateway.util.RewriteHttpUtil;
3537
import org.smartframework.cloud.examples.support.gateway.util.WebUtil;
3638
import org.springframework.core.io.buffer.DataBuffer;
3739
import org.springframework.core.io.buffer.DataBufferFactory;
@@ -66,6 +68,12 @@ public class DataSecurityServerHttpRequestDecorator extends ServerHttpRequestDec
6668

6769
DataSecurityServerHttpRequestDecorator(ServerHttpRequest request, DataBufferFactory dataBufferFactory, String token, boolean requestDecrypt, byte signType, IRedisAdapter redisAdapter) {
6870
super(request);
71+
72+
if ((requestDecrypt || signType == SignType.REQUEST.getType() || signType == SignType.ALL.getType())
73+
&& !RewriteHttpUtil.isSupported(super.getHeaders().getContentType())) {
74+
throw new UnsupportedFunctionException(GatewayReturnCodes.NOT_SUPPORT_DATA_SECURITY);
75+
}
76+
6977
Flux<DataBuffer> flux = super.getBody();
7078
this.redisAdapter = redisAdapter;
7179

application-module/support-module/support-service-gateway/src/main/java/org/smartframework/cloud/examples/support/gateway/filter/access/core/datasecurity/DataSecurityServerHttpResponseDecorator.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
*/
1616
package org.smartframework.cloud.examples.support.gateway.filter.access.core.datasecurity;
1717

18+
import io.github.smart.cloud.api.core.annotation.enums.SignType;
1819
import org.reactivestreams.Publisher;
20+
import org.smartframework.cloud.examples.support.gateway.constants.GatewayReturnCodes;
21+
import org.smartframework.cloud.examples.support.gateway.exception.UnsupportedFunctionException;
1922
import org.smartframework.cloud.examples.support.gateway.util.RewriteHttpUtil;
2023
import org.springframework.core.io.buffer.DataBuffer;
2124
import org.springframework.core.io.buffer.DataBufferFactory;
22-
import org.springframework.http.MediaType;
2325
import org.springframework.http.server.reactive.ServerHttpResponse;
2426
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
2527
import reactor.core.publisher.Flux;
@@ -37,8 +39,13 @@ public class DataSecurityServerHttpResponseDecorator extends ServerHttpResponseD
3739

3840
private transient Publisher<? extends DataBuffer> body;
3941

40-
DataSecurityServerHttpResponseDecorator(ServerHttpResponse delegate, boolean responseEncrypt, byte signType) {
41-
super(delegate);
42+
DataSecurityServerHttpResponseDecorator(ServerHttpResponse response, boolean responseEncrypt, byte signType) {
43+
super(response);
44+
45+
if ((responseEncrypt || signType == SignType.RESPONSE.getType() || signType == SignType.ALL.getType())
46+
&& !RewriteHttpUtil.isSupported(super.getHeaders().getContentType())) {
47+
throw new UnsupportedFunctionException(GatewayReturnCodes.NOT_SUPPORT_DATA_SECURITY);
48+
}
4249
}
4350

4451
@Override
@@ -48,26 +55,22 @@ public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends Data
4855

4956
@Override
5057
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
51-
// TODO:
52-
final MediaType contentType = super.getHeaders().getContentType();
53-
if (RewriteHttpUtil.isSupported(contentType)) {
54-
DataBufferFactory dataBufferFactory = super.bufferFactory();
55-
if (body instanceof Mono) {
56-
((Mono<DataBuffer>) body).subscribe(buffer -> {
57-
byte[] bytes = RewriteHttpUtil.convert(buffer);
58-
String bodyStr = new String(bytes, StandardCharsets.UTF_8);
59-
this.body = Mono.just(dataBufferFactory.wrap(bytes));
60-
});
58+
DataBufferFactory dataBufferFactory = super.bufferFactory();
59+
if (body instanceof Mono) {
60+
((Mono<DataBuffer>) body).subscribe(buffer -> {
61+
byte[] bytes = RewriteHttpUtil.convert(buffer);
62+
String bodyStr = new String(bytes, StandardCharsets.UTF_8);
63+
this.body = Mono.just(dataBufferFactory.wrap(bytes));
64+
});
6165

62-
return super.writeWith(this.body);
63-
} else if (body instanceof Flux) {
64-
((Flux<DataBuffer>) body).subscribe(buffer -> {
65-
byte[] bytes = RewriteHttpUtil.convert(buffer);
66-
String bodyStr = new String(bytes, StandardCharsets.UTF_8);
67-
this.body = Flux.just(dataBufferFactory.wrap(bytes));
68-
});
69-
return super.writeWith(this.body);
70-
}
66+
return super.writeWith(this.body);
67+
} else if (body instanceof Flux) {
68+
((Flux<DataBuffer>) body).subscribe(buffer -> {
69+
byte[] bytes = RewriteHttpUtil.convert(buffer);
70+
String bodyStr = new String(bytes, StandardCharsets.UTF_8);
71+
this.body = Flux.just(dataBufferFactory.wrap(bytes));
72+
});
73+
return super.writeWith(this.body);
7174
}
7275
return super.writeWith(body);
7376
}

0 commit comments

Comments
 (0)