xuhy
2024-08-09 d7f599824d4ab79c35e6aacdc59d2581c28504b3
Merge remote-tracking branch 'origin/master'
8个文件已修改
80 ■■■■■ 已修改文件
ruoyi-api/ruoyi-api-other/src/main/java/com/ruoyi/other/api/factory/OtherFallbackFactory.java 7 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/SignFilter.java 67 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-account/src/main/resources/bootstrap.yml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-chargingPile/src/main/resources/bootstrap.yml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-integration/src/main/resources/bootstrap.yml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-order/src/main/resources/bootstrap.yml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-other/src/main/resources/bootstrap.yml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-payment/src/main/resources/bootstrap.yml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-api/ruoyi-api-other/src/main/java/com/ruoyi/other/api/factory/OtherFallbackFactory.java
@@ -44,7 +44,12 @@
            public R unitDelete(Integer id) {
                return R.fail("删除单位失败:" + throwable.getMessage());
            }
            @Override
            public R<TCompany> unitDetail(Integer id) {
                return R.fail("查询单位失败:" + throwable.getMessage());
            }
            @Override
            public R<Page<TUserTag>> queryTagPage(TagListQueryDto unitListQueryDto) {
                return R.fail("查询标签:" + throwable.getMessage());
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/SignFilter.java
@@ -1,5 +1,6 @@
package com.ruoyi.gateway.filter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.HttpStatus;
@@ -20,6 +21,7 @@
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.ServerHttpRequest;
@@ -29,6 +31,7 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -66,14 +69,14 @@
        }
        String sing = request.getHeaders().getFirst(TokenConstants.SING);
        String nonce_str = request.getHeaders().getFirst(TokenConstants.NONCE_STR);
        if (parameter_signature && StringUtils.isEmpty(sing)) {
            return unauthorizedResponse(exchange, "签名不能为空!");
        }
        if (parameter_signature && StringUtils.isEmpty(nonce_str)) {
            return unauthorizedResponse(exchange, "签名不能为空!");
        }
        if(parameter_signature && !authSign(exchange)){
            return unauthorizedResponse(exchange, "签名不通过!");
//        if (parameter_signature && StringUtils.isEmpty(sing)) {
//            return unauthorizedResponse(exchange, "签名不能为空!");
//        }
//        if (parameter_signature && StringUtils.isEmpty(nonce_str)) {
//            return unauthorizedResponse(exchange, "签名不能为空!");
//        }
        if(parameter_signature){
            return authSign(exchange, chain, sing, nonce_str);
        }
        return chain.filter(exchange.mutate().request(mutate.build()).build());
    }
@@ -88,36 +91,43 @@
    
    /**
     * 校验签名
     * @param exchange
     * @return
     */
    private boolean authSign(ServerWebExchange exchange){
        return false;
    private Mono<Void> authSign(ServerWebExchange exchange, GatewayFilterChain chain, String sing, String nonce_str){
        return DataBufferUtils.join(exchange.getRequest().getBody())
                .flatMap(dataBuffer -> {
                    byte[] bytes = new byte[dataBuffer.readableByteCount()];
                    dataBuffer.read(bytes);
                    String bodyString = new String(bytes, StandardCharsets.UTF_8);
                    log.info("请求参数:{}", bodyString);
                    if(!authSign(JSON.parseObject(bodyString), sing, nonce_str)){
                        return unauthorizedResponse(exchange, "签名验证失败!");
                    }
                    DataBufferUtils.release(dataBuffer);
                    Flux<DataBuffer> cachedFlux = Flux.defer(() -> {
                        DataBuffer buffer = exchange.getResponse().bufferFactory()
                                .wrap(bytes);
                        return Mono.just(buffer);
                    });
                    ServerHttpRequest mutatedRequest = new ServerHttpRequestDecorator(exchange.getRequest()) {
                        @Override
                        public Flux<DataBuffer> getBody() {
                            return cachedFlux;
                        }
                    };
                    return chain.filter(exchange.mutate().request(mutatedRequest)
                            .build());
        });
    }
    
    
    
    /**
     * 签名校验
     * @param httpRequestDecorator
     * @return
     */
    private boolean authSign(ServerHttpRequestDecorator httpRequestDecorator) {
        HttpHeaders headers = httpRequestDecorator.getHeaders();
        Flux<DataBuffer> body = httpRequestDecorator.getBody();
        AtomicReference<JSONObject> jsonObject = new AtomicReference<>(new JSONObject());
        httpRequestDecorator.getBody().map(dataBuffers -> dataBuffers.toString());
        JSONObject params = jsonObject.get();
        String sign = headers.getFirst(TokenConstants.SING);
        if(StringUtils.isEmpty(sign)){
            return false;
        }
        String nonce_str = headers.getFirst(TokenConstants.NONCE_STR);
        if(StringUtils.isEmpty(nonce_str)){
            return false;
        }
        String signUrlEncode = localSignUrl(params, nonce_str);
    private boolean authSign(JSONObject jsonStr, String sign, String nonce_str) {
        String signUrlEncode = localSignUrl(jsonStr, nonce_str);
        signUrlEncode = signUrlEncode.replaceAll("& #40;", "\\(")
                .replaceAll("& #41;", "\\)")
                .replaceAll("\\+", " ");
@@ -166,6 +176,7 @@
        try {
            signByte = HMACSHA1.HmacSHA1Encrypt(signUrl, encryptKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        String localSign = Base64.encodeBase64String(signByte);
ruoyi-service/ruoyi-account/src/main/resources/bootstrap.yml
@@ -5,6 +5,7 @@
    name: ruoyi-account
  main:
    allow-bean-definition-overriding: true
    allow-circular-references: true
  profiles:
    # 环境配置
    active: dev
ruoyi-service/ruoyi-chargingPile/src/main/resources/bootstrap.yml
@@ -5,6 +5,7 @@
    name: ruoyi-chargingPile
  main:
    allow-bean-definition-overriding: true
    allow-circular-references: true
  profiles:
    # 环境配置
    active: dev
ruoyi-service/ruoyi-integration/src/main/resources/bootstrap.yml
@@ -5,6 +5,7 @@
    name: ruoyi-integration
  main:
    allow-bean-definition-overriding: true
    allow-circular-references: true
  profiles:
    # 环境配置
    active: dev
ruoyi-service/ruoyi-order/src/main/resources/bootstrap.yml
@@ -5,6 +5,7 @@
    name: ruoyi-order
  main:
    allow-bean-definition-overriding: true
    allow-circular-references: true
  profiles:
    # 环境配置
    active: dev
ruoyi-service/ruoyi-other/src/main/resources/bootstrap.yml
@@ -5,6 +5,7 @@
    name: ruoyi-other
  main:
    allow-bean-definition-overriding: true
    allow-circular-references: true
  profiles:
    # 环境配置
    active: dev
ruoyi-service/ruoyi-payment/src/main/resources/bootstrap.yml
@@ -5,6 +5,7 @@
    name: ruoyi-payment
  main:
    allow-bean-definition-overriding: true
    allow-circular-references: true
  profiles:
    # 环境配置
    active: dev