From 6186fdaeda7ac975b7f636ab6935f065ae734cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Fri, 23 Feb 2024 12:58:34 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20gateway=20sentinel?= =?UTF-8?q?=20=E9=99=90=E6=B5=81=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98(?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=96=B9=E6=A1=88)=20https://github.com/alib?= =?UTF-8?q?aba/Sentinel/issues/3298?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../callback/DefaultBlockRequestHandler.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 ruoyi-gateway/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/sc/callback/DefaultBlockRequestHandler.java diff --git a/ruoyi-gateway/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/sc/callback/DefaultBlockRequestHandler.java b/ruoyi-gateway/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/sc/callback/DefaultBlockRequestHandler.java new file mode 100644 index 00000000..2971dceb --- /dev/null +++ b/ruoyi-gateway/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/sc/callback/DefaultBlockRequestHandler.java @@ -0,0 +1,95 @@ +/* + * Copyright 1999-2019 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.adapter.gateway.sc.callback; + +import org.springframework.http.HttpStatus; +import org.springframework.http.InvalidMediaTypeException; +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.server.ServerResponse; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +import java.util.List; + +import static org.springframework.web.reactive.function.BodyInserters.fromObject; + +// https://github.com/alibaba/Sentinel/issues/3298 +// 临时解决 sentinel 限流插件 jdk17 报错问题 +/** + * The default implementation of {@link BlockRequestHandler}. + * Compatible with Spring WebFlux and Spring Cloud Gateway. + * + * @author Eric Zhao + */ +public class DefaultBlockRequestHandler implements BlockRequestHandler { + + private static final String DEFAULT_BLOCK_MSG_PREFIX = "Blocked by Sentinel: "; + + @Override + public Mono handleRequest(ServerWebExchange exchange, Throwable ex) { + if (acceptsHtml(exchange)) { + return htmlErrorResponse(ex); + } + // JSON result by default. + return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS) + .contentType(MediaType.APPLICATION_JSON_UTF8) + .body(fromObject(buildErrorResult(ex))); + } + + private Mono htmlErrorResponse(Throwable ex) { + return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS) + .contentType(MediaType.TEXT_PLAIN) + .syncBody(DEFAULT_BLOCK_MSG_PREFIX + ex.getClass().getSimpleName()); + } + + private ErrorResult buildErrorResult(Throwable ex) { + return new ErrorResult(HttpStatus.TOO_MANY_REQUESTS.value(), + DEFAULT_BLOCK_MSG_PREFIX + ex.getClass().getSimpleName()); + } + + /** + * Reference from {@code DefaultErrorWebExceptionHandler} of Spring Boot. + */ + private boolean acceptsHtml(ServerWebExchange exchange) { + try { + List acceptedMediaTypes = exchange.getRequest().getHeaders().getAccept(); + acceptedMediaTypes.remove(MediaType.ALL); + MediaType.sortBySpecificityAndQuality(acceptedMediaTypes); + return acceptedMediaTypes.stream() + .anyMatch(MediaType.TEXT_HTML::isCompatibleWith); + } catch (InvalidMediaTypeException ex) { + return false; + } + } + + private static class ErrorResult { + private final int code; + private final String message; + + ErrorResult(int code, String message) { + this.code = code; + this.message = message; + } + + public int getCode() { + return code; + } + + public String getMessage() { + return message; + } + } +}