数据跨域

This commit is contained in:
haozq 2024-09-13 14:52:40 +08:00
parent 74363954f6
commit 2f8f238941
5 changed files with 234 additions and 1 deletions

View File

@ -93,7 +93,7 @@ public class SM4Utils {
} }
public static void main(String[] args) { public static void main(String[] args) {
System.err.println( decryptData_CBC("VlWYLrghArikg+SQKS4obg==")); System.err.println( decryptData_CBC("hz6mXadxdNrq2iE8vG9X+rcDwInsjg1EA7PMY1/mOsw="));
} }
private static String decryptData(String type, String cipherText, String secretKey, String iv) { private static String decryptData(String type, String cipherText, String secretKey, String iv) {
try { try {

View File

@ -0,0 +1,65 @@
package com.securitycontrol.common.core.utils.globals;
/**
* @author 黑子
* 全局变量
*/
public class SystemGlobal {
/**
* true
*/
public static String undefined="undefined";
/**
* true
*/
public static String TRUE_STR="true";
/**
* formData
*/
public static String FORM_DATA="formData";
/**
*
* 解密头
*/
public final static String KEY_DECRYPT="decrypt";
/**
* 加密头
*/
public final static String KEY_ENCRYPT ="encryption";
/**
* 成功的200 字符串
*/
public final static String SUCCESS_CODE_STR="200";
/**
* POST 请求
*/
public final static String POST="POST";
/**
* PUT 请求
*/
public final static String PUT="PUT";
/**
* 文件存储类型-local
*/
public final static String LOCAL="local";
/**
* 文件存储类型-oss
*/
public final static String OSS="oss";
/**
* 文件存储类型-mongodb
*/
public final static String MONGODB="mongodb";
/**
* 文件存储类型-obs
*/
public final static String OBS="obs";
public final static int SUCCESS_MIN_NUM = 0;
}

View File

@ -0,0 +1,163 @@
package com.securitycontrol.gateway.filter;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import com.securitycontrol.common.core.utils.aes.SM4Utils;
import com.securitycontrol.common.core.utils.globals.SystemGlobal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Charsets;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
/**
* 对返回的 data数据进行加密
* @author 黑子
*/
@Configuration
@Slf4j
public class ResponseEncryptFilter implements GlobalFilter, Ordered {
@Value("${system.res}")
public boolean jaData;
/**
* 返回的数据 是否加密
*/
public final static String KEY_HEAD="decrypt";
/**忽略加密的参数的请求*/
public static List<String> ignoreUrls = new ArrayList<>();
{
ignoreUrls.add("/tcp/ball/xmlAnalysis");
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("============================ResponseEncryptFilter start===================================");
ServerHttpRequest request = exchange.getRequest();
URI uri = request.getURI();
String reqPath = request.getURI().getPath();
boolean sf = ignoreUrls.contains(reqPath);
if(sf){
return chain.filter(exchange);
}
HttpHeaders headers=request.getHeaders();
if(headers!=null){
Object object=headers.getFirst("Content-Type");
Object head=headers.getFirst(SystemGlobal.KEY_ENCRYPT);
if (head!=null){
String keyHead=head.toString();
if (SystemGlobal.KEY_ENCRYPT.equals(keyHead)){
return chain.filter(exchange);
}
}
if(object!=null){
String contentType=object.toString();
if (contentType.contains(MULTIPART_FORM_DATA_VALUE)){
return chain.filter(exchange);
}
}
}
HttpStatus statusCode = exchange.getResponse().getStatusCode();
if(Objects.equals(statusCode, HttpStatus.BAD_REQUEST) || Objects.equals(statusCode, HttpStatus.TOO_MANY_REQUESTS)){
// 如果是特殊的请求已处理响应内容这里不再处理
return chain.filter(exchange);
}
//是否加密
if(!jaData){
return chain.filter(exchange);
}
// 根据具体业务内容修改响应体
return modifyResponseBody(exchange, chain);
}
/**
* 修改响应体
* @param exchange
* @param chain
* @return
*/
private Mono<Void> modifyResponseBody(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpResponse originalResponse = exchange.getResponse();
originalResponse.getHeaders().setContentType(MediaType.APPLICATION_JSON);
DataBufferFactory bufferFactory = originalResponse.bufferFactory();
ServerHttpResponseDecorator response = buildResponse(originalResponse, bufferFactory);
return chain.filter(exchange.mutate().response(response).build());
}
@Override
public int getOrder() {
return -5;
}
private ServerHttpResponseDecorator buildResponse(ServerHttpResponse originalResponse, DataBufferFactory bufferFactory) {
return new ServerHttpResponseDecorator(originalResponse) {
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
if (getStatusCode().equals(HttpStatus.OK) && body instanceof Flux) {
Flux<? extends DataBuffer> fluxBody = Flux.from(body);
return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
DataBuffer join = dataBufferFactory.join(dataBuffers);
byte[] content = new byte[join.readableByteCount()];
join.read(content);
DataBufferUtils.release(join);
// 流转为字符串
String responseData = new String(content, Charsets.UTF_8);
System.out.println(responseData);
Map map = JSON.parseObject(responseData);
Map maps= Maps.newHashMap();
//加密则数据 进行加密
if(jaData){
responseData = SM4Utils.encryptData_CBC(responseData.trim());
maps.put("data",responseData);
maps.put(SystemGlobal.KEY_DECRYPT,true);
responseData=JSON.toJSONString(maps);
}else{
maps.put("data",responseData);
maps.put(SystemGlobal.KEY_DECRYPT,false);
}
byte[] uppedContent = responseData.getBytes(Charsets.UTF_8);
originalResponse.getHeaders().setContentLength(uppedContent.length);
return bufferFactory.wrap(uppedContent);
}));
} else {
log.error("获取响应体数据 "+getStatusCode());
}
return super.writeWith(body);
}
@Override
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
return writeWith(Flux.from(body).flatMapSequential(p -> p));
}
};
}
}

View File

@ -49,6 +49,7 @@ endpoints:
enable: false enable: false
system: system:
jm: true jm: true
res: true
#加密组件 #加密组件
jasypt: jasypt:
encryptor: encryptor:

View File

@ -43,6 +43,10 @@ public class TodayTaskServiceImpl implements ITodayTaskService {
List<TodayTaskVo> list = new ArrayList<>(); List<TodayTaskVo> list = new ArrayList<>();
List<Future> futureList = new ArrayList<>(); List<Future> futureList = new ArrayList<>();
List<TodayTaskVo> newList = new ArrayList<>(); List<TodayTaskVo> newList = new ArrayList<>();
if(StringHelper.isNotEmpty(dto.getWorkDay())){
dto.setStartTime(dto.getWorkDay().split(" - ")[0]);
dto.setEndTime(dto.getWorkDay().split(" - ")[0]);
}
try { try {
dto = handleParams(dto); dto = handleParams(dto);
list = mapper.getToDayTaskLists(dto); list = mapper.getToDayTaskLists(dto);