diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8c2758c8..1500232a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,8 +33,13 @@ jobs: java-version: 17 distribution: 'temurin' - - name: Test with Maven + - name: Maven Test With Spring 6.x run: mvn --batch-mode test -Dsurefire.jdk-toolchain-version=${{ matrix.java }} + if: ${{ matrix.java >= 17 }} + + - name: Maven Test Without Spring 6.x + run: mvn --batch-mode test -Dsurefire.jdk-toolchain-version=${{ matrix.java }} -Dskip.spring.v6x.test=true + if: ${{ matrix.java < 17 }} - name: Build with Maven run: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V -DminimumPriority=1 diff --git a/pom.xml b/pom.xml index fb27e5e3..ef3e6381 100755 --- a/pom.xml +++ b/pom.xml @@ -159,6 +159,13 @@ sentinel-cluster-common-default ${project.version} + + + com.alibaba.csp + sentinel-web-adapter-common + ${project.version} + + com.alibaba.csp sentinel-adapter diff --git a/sentinel-adapter/pom.xml b/sentinel-adapter/pom.xml index beab90ee..2f22512e 100755 --- a/sentinel-adapter/pom.xml +++ b/sentinel-adapter/pom.xml @@ -27,8 +27,9 @@ sentinel-spring-webflux-adapter sentinel-api-gateway-adapter-common sentinel-spring-cloud-gateway-adapter + sentinel-web-adapter-common sentinel-spring-webmvc-adapter - sentinel-spring-webmvc-6x-adapter + sentinel-spring-webmvc-v6x-adapter sentinel-zuul2-adapter sentinel-okhttp-adapter sentinel-jax-rs-adapter diff --git a/sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebInterceptor.java b/sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebInterceptor.java index 0c244d68..1568bfd7 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebInterceptor.java +++ b/sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebInterceptor.java @@ -25,6 +25,8 @@ import org.springframework.web.servlet.HandlerMapping; /** * Spring Web MVC interceptor that integrates with Sentinel. + *

+ * This will record resource as `${uri}`. * * @author kaizi2009 * @since 1.7.1 diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/README.md b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/README.md old mode 100644 new mode 100755 similarity index 98% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/README.md rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/README.md index cfbeb2da..79caf263 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/README.md +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/README.md @@ -9,7 +9,7 @@ Add the following dependency in `pom.xml` (if you are using Maven): ```xml com.alibaba.csp - sentinel-spring-webmvc-adapter + sentinel-spring-webmvc-v6x-adapter x.y.z ``` @@ -104,4 +104,4 @@ config.setBlockExceptionHandler((request, response, e) -> { | name | description | type | default value | |------|------------|------|-------| | totalResourceName | The resource name in `SentinelTotalInterceptor` | `String` | `spring-mvc-total-url-request` | -| requestAttributeName | Attribute key in request used by Sentinel (internal) | `String` | `$$sentinel_spring_web_total_entry_attr` | \ No newline at end of file +| requestAttributeName | Attribute key in request used by Sentinel (internal) | `String` | `$$sentinel_spring_web_total_entry_attr` | diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/pom.xml b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/pom.xml similarity index 69% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/pom.xml rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/pom.xml index 0746c3b0..65c09272 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/pom.xml +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/pom.xml @@ -5,21 +5,21 @@ com.alibaba.csp sentinel-adapter - 2.0.0-SNAPSHOT + 1.8.7 4.0.0 - sentinel-spring-webmvc-6x-adapter + sentinel-spring-webmvc-v6x-adapter jar 6.0.2 3.0.0 6.0.0 - 17 - 17 - 2.0.4 4.0.0 + 2.0.4 + + false @@ -27,6 +27,11 @@ com.alibaba.csp sentinel-core + + com.alibaba.csp + sentinel-web-adapter-common + + jakarta.servlet jakarta.servlet-api @@ -39,7 +44,6 @@ ${spring.version} provided - org.springframework.boot spring-boot-starter-web @@ -52,6 +56,12 @@ ${spring.boot.version} test + + org.apache.commons + commons-lang3 + 3.14.0 + test + com.alibaba fastjson @@ -62,8 +72,17 @@ junit test + + org.mockito + mockito-inline + test + + + org.slf4j + slf4j-api + - + @@ -78,17 +97,22 @@ - + org.apache.maven.plugins - maven-compiler-plugin - ${maven.compiler.version} + maven-surefire-plugin + ${maven.surefire.version} + + + org.apache.maven.surefire + surefire-junit47 + 3.2.5 + + - ${java.source.version} - ${java.target.version} - ${java.encoding} + ${skip.spring.v6x.test} diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/AbstractSentinelInterceptor.java similarity index 78% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/AbstractSentinelInterceptor.java index b4bfe2da..271d1442 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/AbstractSentinelInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,44 +13,37 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; - -import com.alibaba.csp.sentinel.Entry; -import com.alibaba.csp.sentinel.EntryType; -import com.alibaba.csp.sentinel.ResourceTypeConstants; -import com.alibaba.csp.sentinel.SphU; -import com.alibaba.csp.sentinel.Tracer; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.BaseWebMvcConfig; +import com.alibaba.csp.sentinel.*; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.config.BaseWebMvcConfig; import com.alibaba.csp.sentinel.context.ContextUtil; import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.util.AssertUtil; import com.alibaba.csp.sentinel.util.StringUtil; - +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; /** * Since request may be reprocessed in flow if any forwarding or including or other action * happened (see {@link jakarta.servlet.ServletRequest#getDispatcherType()}) we will only - * deal with the initial request. So we use reference count to track in - * dispathing "onion" though which we could figure out whether we are in initial type "REQUEST". + * deal with the initial request. So we use reference count to track in + * dispatching "onion" though which we could figure out whether we are in initial type "REQUEST". * That means the sub-requests which we rarely meet in practice will NOT be recorded in Sentinel. *

* How to implement a forward sub-request in your action: *

- * initalRequest() {
+ * initialRequest() {
  *     ModelAndView mav = new ModelAndView();
  *     mav.setViewName("another");
  *     return mav;
  * }
  * 
- * - * @author kaizi2009 - * @since 1.7.1 + * + * @since 1.8.8 */ public abstract class AbstractSentinelInterceptor implements HandlerInterceptor { @@ -64,40 +57,38 @@ public abstract class AbstractSentinelInterceptor implements HandlerInterceptor AssertUtil.assertNotBlank(config.getRequestAttributeName(), "requestAttributeName should not be blank"); this.baseWebMvcConfig = config; } - + /** * @param request * @param rcKey * @param step - * @return reference count after increasing (initial value as zero to be increased) + * @return reference count after increasing (initial value as zero to be increased) */ - private Integer increaseReferece(HttpServletRequest request, String rcKey, int step) { + private Integer increaseReference(HttpServletRequest request, String rcKey, int step) { Object obj = request.getAttribute(rcKey); - + if (obj == null) { // initial obj = Integer.valueOf(0); } - - Integer newRc = (Integer)obj + step; + + Integer newRc = (Integer) obj + step; request.setAttribute(rcKey, newRc); return newRc; } - + @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) - throws Exception { + throws Exception { + String resourceName = ""; try { - String resourceName = getResourceName(request); - + resourceName = getResourceName(request); if (StringUtil.isEmpty(resourceName)) { return true; } - - if (increaseReferece(request, this.baseWebMvcConfig.getRequestRefName(), 1) != 1) { + if (increaseReference(request, this.baseWebMvcConfig.getRequestRefName(), 1) != 1) { return true; } - // Parse the request origin using registered origin parser. String origin = parseOrigin(request); String contextName = getContextName(request); @@ -107,7 +98,7 @@ public abstract class AbstractSentinelInterceptor implements HandlerInterceptor return true; } catch (BlockException e) { try { - handleBlockException(request, response, e); + handleBlockException(request, response, resourceName, e); } finally { ContextUtil.exit(); } @@ -136,10 +127,10 @@ public abstract class AbstractSentinelInterceptor implements HandlerInterceptor @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { - if (increaseReferece(request, this.baseWebMvcConfig.getRequestRefName(), -1) != 0) { + if (increaseReference(request, this.baseWebMvcConfig.getRequestRefName(), -1) != 0) { return; } - + Entry entry = getEntryInRequest(request, baseWebMvcConfig.getRequestAttributeName()); if (entry == null) { // should not happen @@ -147,7 +138,12 @@ public abstract class AbstractSentinelInterceptor implements HandlerInterceptor getClass().getSimpleName(), baseWebMvcConfig.getRequestAttributeName()); return; } - + + // Record the status code here. +// String resourceName = entry.getResourceWrapper().getName(); +// int status = response.getStatus(); +// StatusCodeMetricManager.getInstance().recordStatusCode(resourceName, status); + traceExceptionAndExit(entry, ex); removeEntryInRequest(request); ContextUtil.exit(); @@ -160,7 +156,7 @@ public abstract class AbstractSentinelInterceptor implements HandlerInterceptor protected Entry getEntryInRequest(HttpServletRequest request, String attrKey) { Object entryObject = request.getAttribute(attrKey); - return entryObject == null ? null : (Entry)entryObject; + return entryObject == null ? null : (Entry) entryObject; } protected void removeEntryInRequest(HttpServletRequest request) { @@ -176,12 +172,18 @@ public abstract class AbstractSentinelInterceptor implements HandlerInterceptor } } - protected void handleBlockException(HttpServletRequest request, HttpServletResponse response, BlockException e) - throws Exception { + protected void handleBlockException(HttpServletRequest request, HttpServletResponse response, String resourceName, + BlockException e) + throws Exception { if (baseWebMvcConfig.getBlockExceptionHandler() != null) { - baseWebMvcConfig.getBlockExceptionHandler().handle(request, response, e); + baseWebMvcConfig.getBlockExceptionHandler().handle(request, response, resourceName, e); + + // Record status when blocked +// int status = response.getStatus(); +// StatusCodeMetricManager.getInstance().recordStatusCode(resourceName, status); } else { // Throw BlockException directly. Users need to handle it in Spring global exception handler. + // NOTE: the status code statistics will be lost here! throw e; } } diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebInterceptor.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelWebInterceptor.java similarity index 78% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebInterceptor.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelWebInterceptor.java index 37ffd703..524bd040 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebInterceptor.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelWebInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,21 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.config.SentinelWebMvcConfig; +import com.alibaba.csp.sentinel.adapter.web.common.UrlCleaner; import jakarta.servlet.http.HttpServletRequest; -import com.alibaba.csp.sentinel.util.StringUtil; import org.springframework.web.servlet.HandlerMapping; /** * Spring Web MVC interceptor that integrates with Sentinel. + *

+ * This will record resource as `${uri}`. * - * @author kaizi2009 - * @since 1.7.1 + * @since 1.8.8 */ public class SentinelWebInterceptor extends AbstractSentinelInterceptor { @@ -59,9 +59,8 @@ public class SentinelWebInterceptor extends AbstractSentinelInterceptor { if (urlCleaner != null) { resourceName = urlCleaner.clean(resourceName); } - // Add method specification if necessary - if (StringUtil.isNotEmpty(resourceName) && config.isHttpMethodSpecify()) { - resourceName = request.getMethod().toUpperCase() + ":" + resourceName; + if (config.isContextPathSpecify() && request.getContextPath() != null) { + resourceName = request.getContextPath() + resourceName; } return resourceName; } diff --git a/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelWebPrefixInterceptor.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelWebPrefixInterceptor.java new file mode 100644 index 00000000..021f2d90 --- /dev/null +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelWebPrefixInterceptor.java @@ -0,0 +1,39 @@ +/* + * Copyright 1999-2024 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 + * + * https://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.spring.webmvc_v6x; + +import com.alibaba.csp.sentinel.util.StringUtil; +import jakarta.servlet.http.HttpServletRequest; + +/** + * Spring Web MVC interceptor that integrates with Sentinel. + *

+ * This will record resource as `${httpMethod}:${uri}`. + * + * @since 1.8.8 + */ +public class SentinelWebPrefixInterceptor extends SentinelWebInterceptor { + + @Override + protected String getResourceName(HttpServletRequest request) { + String resourceName = super.getResourceName(request); + // Add method specification + if (StringUtil.isNotEmpty(resourceName)) { + resourceName = request.getMethod().toUpperCase() + ":" + resourceName; + } + return resourceName; + } +} \ No newline at end of file diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebTotalInterceptor.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelWebTotalInterceptor.java similarity index 85% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebTotalInterceptor.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelWebTotalInterceptor.java index e1751c50..9cbc654d 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebTotalInterceptor.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelWebTotalInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcTotalConfig; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.config.SentinelWebMvcTotalConfig; import jakarta.servlet.http.HttpServletRequest; @@ -23,8 +23,7 @@ import jakarta.servlet.http.HttpServletRequest; * The web interceptor for all requests, which will unify all URL as * a single resource name (configured in {@link SentinelWebMvcTotalConfig}). * - * @author kaizi2009 - * @since 1.7.1 + * @since 1.8.8 */ public class SentinelWebTotalInterceptor extends AbstractSentinelInterceptor { diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/callback/BlockExceptionHandler.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/callback/BlockExceptionHandler.java similarity index 82% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/callback/BlockExceptionHandler.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/callback/BlockExceptionHandler.java index 7e9c87f6..c6335ad7 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/callback/BlockExceptionHandler.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/callback/BlockExceptionHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc.callback; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback; import com.alibaba.csp.sentinel.slots.block.BlockException; @@ -23,7 +23,7 @@ import jakarta.servlet.http.HttpServletResponse; /** * Handler for the blocked request. * - * @author kaizi2009 + * @since 1.8.8 */ public interface BlockExceptionHandler { @@ -32,9 +32,11 @@ public interface BlockExceptionHandler { * * @param request Servlet request * @param response Servlet response + * @param resourceName resource name * @param e the block exception * @throws Exception users may throw out the BlockException or other error occurs */ - void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception; + void handle(HttpServletRequest request, HttpServletResponse response, String resourceName, BlockException e) + throws Exception; } diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/callback/DefaultBlockExceptionHandler.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/callback/DefaultBlockExceptionHandler.java similarity index 84% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/callback/DefaultBlockExceptionHandler.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/callback/DefaultBlockExceptionHandler.java index 9b54974d..5181da74 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/callback/DefaultBlockExceptionHandler.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/callback/DefaultBlockExceptionHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,23 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc.callback; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback; import com.alibaba.csp.sentinel.slots.block.BlockException; - import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; + import java.io.PrintWriter; /** * Default handler for the blocked request. * - * @author kaizi2009 + * @since 1.8.8 */ public class DefaultBlockExceptionHandler implements BlockExceptionHandler { @Override - public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception { + public void handle(HttpServletRequest request, HttpServletResponse response, String resourceName, BlockException ex) + throws Exception { // Return 429 (Too Many Requests) by default. response.setStatus(429); @@ -38,5 +39,4 @@ public class DefaultBlockExceptionHandler implements BlockExceptionHandler { out.flush(); out.close(); } - } diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/callback/RequestOriginParser.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/callback/RequestOriginParser.java similarity index 87% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/callback/RequestOriginParser.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/callback/RequestOriginParser.java index 9bb34270..72816e45 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/callback/RequestOriginParser.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/callback/RequestOriginParser.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,14 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc.callback; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback; import jakarta.servlet.http.HttpServletRequest; /** * The origin parser parses request origin (e.g. IP, user, appName) from HTTP request. * - * @author kaizi2009 + * @since 1.8.8 */ public interface RequestOriginParser { diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/BaseWebMvcConfig.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/BaseWebMvcConfig.java similarity index 77% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/BaseWebMvcConfig.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/BaseWebMvcConfig.java index e1bd1542..84d64b6a 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/BaseWebMvcConfig.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/BaseWebMvcConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,22 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc.config; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.config; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.BlockExceptionHandler; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.DefaultBlockExceptionHandler; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.RequestOriginParser; /** * Common base configuration for Spring Web MVC adapter. * - * @author kaizi2009 - * @since 1.7.1 + * @since 1.8.8 */ public abstract class BaseWebMvcConfig { protected String requestAttributeName; protected String requestRefName; - protected BlockExceptionHandler blockExceptionHandler; + protected BlockExceptionHandler blockExceptionHandler = new DefaultBlockExceptionHandler(); protected RequestOriginParser originParser; public String getRequestAttributeName() { diff --git a/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/SentinelPreWebMvcConfig.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/SentinelPreWebMvcConfig.java new file mode 100644 index 00000000..80373fc6 --- /dev/null +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/SentinelPreWebMvcConfig.java @@ -0,0 +1,57 @@ +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.config; + +import com.alibaba.csp.sentinel.adapter.web.common.UrlCleaner; + +/** + * @since 1.8.8 + */ +public class SentinelPreWebMvcConfig extends BaseWebMvcConfig { + + public static final String DEFAULT_REQUEST_ATTRIBUTE_NAME = "$$sentinel_pre_spring_web_entry_attr"; + + private UrlCleaner urlCleaner; + + /** + * Specify whether the URL resource name should contain the HTTP method prefix (e.g. {@code POST:}). + */ + private boolean httpMethodSpecify; + + /** + * Specify whether unify web context(i.e. use the default context name), and is true by default. + * + * @since 1.7.2 + */ + private boolean webContextUnify = true; + + public SentinelPreWebMvcConfig() { + super(); + setRequestAttributeName(DEFAULT_REQUEST_ATTRIBUTE_NAME); + } + + public boolean isHttpMethodSpecify() { + return httpMethodSpecify; + } + + public SentinelPreWebMvcConfig setHttpMethodSpecify(boolean httpMethodSpecify) { + this.httpMethodSpecify = httpMethodSpecify; + return this; + } + + public boolean isWebContextUnify() { + return webContextUnify; + } + + public SentinelPreWebMvcConfig setWebContextUnify(boolean webContextUnify) { + this.webContextUnify = webContextUnify; + return this; + } + + public UrlCleaner getUrlCleaner() { + return urlCleaner; + } + + public SentinelPreWebMvcConfig setUrlCleaner(UrlCleaner urlCleaner) { + this.urlCleaner = urlCleaner; + return this; + } +} diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/SentinelWebMvcConfig.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/SentinelWebMvcConfig.java old mode 100644 new mode 100755 similarity index 62% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/SentinelWebMvcConfig.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/SentinelWebMvcConfig.java index c94ff8c2..97ffc5cd --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/SentinelWebMvcConfig.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/SentinelWebMvcConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,13 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc.config; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.config; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner; +import com.alibaba.csp.sentinel.adapter.web.common.UrlCleaner; /** - * @author kaizi2009 - * @since 1.7.1 + * @since 1.8.8 */ public class SentinelWebMvcConfig extends BaseWebMvcConfig { @@ -35,6 +34,12 @@ public class SentinelWebMvcConfig extends BaseWebMvcConfig { */ private boolean httpMethodSpecify; + /** + * Specify whether the URL resource name should contain the HTTP method prefix in MSE (e.g. {@code GET:}). + */ + private static boolean mseHttpMethodSpecify; + + /** * Specify whether unify web context(i.e. use the default context name), and is true by default. * @@ -42,9 +47,25 @@ public class SentinelWebMvcConfig extends BaseWebMvcConfig { */ private boolean webContextUnify = true; + /** + * Specify whether the URL resource name should contain the context-path + */ + private boolean contextPathSpecify = true; + public SentinelWebMvcConfig() { super(); setRequestAttributeName(DEFAULT_REQUEST_ATTRIBUTE_NAME); + try { + String enable = System.getProperty("spring.cloud.mse.sentinel.web.http-method-prefix","true"); + if(enable != null){ + mseHttpMethodSpecify = Boolean.parseBoolean(enable); + } + String enableContextPath = System.getProperty("spring.cloud.ahas.sentinel.web.context-path", "true"); + if (enableContextPath != null) { + contextPathSpecify = Boolean.parseBoolean(enableContextPath); + } + } catch (Exception ignore) { + } } public UrlCleaner getUrlCleaner() { @@ -65,6 +86,10 @@ public class SentinelWebMvcConfig extends BaseWebMvcConfig { return this; } + public static boolean isMseHttpMethodSpecify() { + return mseHttpMethodSpecify; + } + public boolean isWebContextUnify() { return webContextUnify; } @@ -74,12 +99,22 @@ public class SentinelWebMvcConfig extends BaseWebMvcConfig { return this; } + public boolean isContextPathSpecify() { + return contextPathSpecify; + } + + public SentinelWebMvcConfig setContextPathSpecify(boolean contextPathSpecify) { + this.contextPathSpecify = contextPathSpecify; + return this; + } + @Override public String toString() { return "SentinelWebMvcConfig{" + "urlCleaner=" + urlCleaner + ", httpMethodSpecify=" + httpMethodSpecify + ", webContextUnify=" + webContextUnify + + ", contextPathSpecify=" + contextPathSpecify + ", requestAttributeName='" + requestAttributeName + '\'' + ", blockExceptionHandler=" + blockExceptionHandler + ", originParser=" + originParser + diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/SentinelWebMvcTotalConfig.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/SentinelWebMvcTotalConfig.java similarity index 91% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/SentinelWebMvcTotalConfig.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/SentinelWebMvcTotalConfig.java index dc8b4af0..7b4f4361 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/SentinelWebMvcTotalConfig.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/SentinelWebMvcTotalConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,11 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc.config; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.config; /** - * @author kaizi2009 - * @since 1.7.1 + * @since 1.8.8 */ public class SentinelWebMvcTotalConfig extends BaseWebMvcConfig { diff --git a/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/WebServletLocalConfig.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/WebServletLocalConfig.java new file mode 100755 index 00000000..58120ba4 --- /dev/null +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/WebServletLocalConfig.java @@ -0,0 +1,87 @@ +/* + * Copyright 1999-2024 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.spring.webmvc_v6x.config; + +import com.alibaba.csp.sentinel.config.SentinelConfig; +import com.alibaba.csp.sentinel.log.RecordLog; +import com.alibaba.csp.sentinel.util.StringUtil; + +/** + * The configuration center for Web Servlet adapter (ported to Spring Web adapter). + * + * @since 1.8.8 + */ +public final class WebServletLocalConfig { + + public static final String BLOCK_PAGE_URL_CONF_KEY = "csp.sentinel.web.servlet.block.page"; + public static final String BLOCK_PAGE_HTTP_STATUS_CONF_KEY = "csp.sentinel.web.servlet.block.status"; + public static final String BLOCK_PAGE_ALLOW_ORIGINS_CONF_KEY = "csp.sentinel.web.servlet.block.cors-allow-origins"; + + private static final int HTTP_STATUS_TOO_MANY_REQUESTS = 429; + + /** + * Get redirecting page when blocked by Sentinel. + * + * @return the block page URL, maybe null if not configured. + */ + public static String getBlockPage() { + return SentinelConfig.getConfig(BLOCK_PAGE_URL_CONF_KEY); + } + + public static void setBlockPage(String blockPage) { + SentinelConfig.setConfig(BLOCK_PAGE_URL_CONF_KEY, blockPage); + } + + /** + *

Get the HTTP status when using the default block page.

+ *

You can set the status code with the {@code -Dcsp.sentinel.web.servlet.block.status} + * property. When the property is empty or invalid, Sentinel will use 429 (Too Many Requests) + * as the default status code.

+ * + * @return the HTTP status of the default block page + */ + public static int getBlockPageHttpStatus() { + String value = SentinelConfig.getConfig(BLOCK_PAGE_HTTP_STATUS_CONF_KEY); + if (StringUtil.isEmpty(value)) { + return HTTP_STATUS_TOO_MANY_REQUESTS; + } + try { + int s = Integer.parseInt(value); + if (s <= 0) { + throw new IllegalArgumentException("Invalid status code: " + s); + } + return s; + } catch (Exception e) { + RecordLog.warn("[WebServletConfig] Invalid block HTTP status (" + value + "), using default 429"); + setBlockPageHttpStatus(HTTP_STATUS_TOO_MANY_REQUESTS); + } + return HTTP_STATUS_TOO_MANY_REQUESTS; + } + + /** + * Set the HTTP status of the default block page. + * + * @param httpStatus the HTTP status of the default block page + */ + public static void setBlockPageHttpStatus(int httpStatus) { + if (httpStatus <= 0) { + throw new IllegalArgumentException("Invalid HTTP status code: " + httpStatus); + } + SentinelConfig.setConfig(BLOCK_PAGE_HTTP_STATUS_CONF_KEY, String.valueOf(httpStatus)); + } + + private WebServletLocalConfig() {} +} diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/ResultWrapper.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/ResultWrapper.java similarity index 92% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/ResultWrapper.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/ResultWrapper.java index 66121fcd..c54e81b5 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/ResultWrapper.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/ResultWrapper.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x; import com.alibaba.fastjson.JSONObject; diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelSpringMvcIntegrationTest.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelSpringMvcIntegrationTest.java similarity index 98% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelSpringMvcIntegrationTest.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelSpringMvcIntegrationTest.java index f1ddb937..69d21a32 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelSpringMvcIntegrationTest.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelSpringMvcIntegrationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebInterceptorTest.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelWebInterceptorTest.java similarity index 80% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebInterceptorTest.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelWebInterceptorTest.java index 506d9b2d..1af28e27 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/SentinelWebInterceptorTest.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/SentinelWebInterceptorTest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,14 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.config.SentinelWebMvcConfig; import org.junit.Test; -import static org.junit.Assert.*; - /** * @author Eric Zhao */ diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/TestApplication.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/TestApplication.java similarity index 78% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/TestApplication.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/TestApplication.java index da9b6bba..bf2fab3c 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/TestApplication.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/TestApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,12 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.EnableAspectJAutoProxy; /** * @author kaizi2009 diff --git a/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/callback/DefaultBlockExceptionHandlerTest.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/callback/DefaultBlockExceptionHandlerTest.java new file mode 100644 index 00000000..fca59ffe --- /dev/null +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/callback/DefaultBlockExceptionHandlerTest.java @@ -0,0 +1,25 @@ +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback; + +import com.alibaba.csp.sentinel.slots.block.BlockException; +import com.alibaba.csp.sentinel.slots.block.flow.FlowException; +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +import static org.junit.Assert.assertEquals; + +public class DefaultBlockExceptionHandlerTest { + + @Test + public void handle_writeBlockPage() throws Exception { + DefaultBlockExceptionHandler h = new DefaultBlockExceptionHandler(); + MockHttpServletRequest req = new MockHttpServletRequest("GET", "/a/b/c"); + req.setQueryString("a=1&b=2"); + MockHttpServletResponse resp = new MockHttpServletResponse(); + String resourceName = "/a/b/c"; + BlockException ex = new FlowException("msg"); + h.handle(req, resp, resourceName, ex); + assertEquals(429, resp.getStatus()); + } + +} \ No newline at end of file diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/InterceptorConfig.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/InterceptorConfig.java similarity index 83% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/InterceptorConfig.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/InterceptorConfig.java index b0c4112b..363fea2e 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/InterceptorConfig.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/InterceptorConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc.config; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.config; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebTotalInterceptor; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.SentinelWebInterceptor; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.SentinelWebTotalInterceptor; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.BlockExceptionHandler; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.RequestOriginParser; import com.alibaba.csp.sentinel.slots.block.BlockException; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; @@ -50,8 +50,8 @@ public class InterceptorConfig implements WebMvcConfigurer { config.setBlockExceptionHandler(new BlockExceptionHandler() { @Override - public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception { - String resourceName = e.getRule().getResource(); + public void handle(HttpServletRequest request, HttpServletResponse response, String resourceName, BlockException e) throws Exception { + resourceName = e.getRule().getResource(); //Depending on your situation, you can choose to process or throw if ("/hello".equals(resourceName)) { //Do something ...... diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/SentinelSpringMvcBlockHandlerConfig.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/SentinelSpringMvcBlockHandlerConfig.java similarity index 90% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/SentinelSpringMvcBlockHandlerConfig.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/SentinelSpringMvcBlockHandlerConfig.java index b8e46603..6fab6530 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/config/SentinelSpringMvcBlockHandlerConfig.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/config/SentinelSpringMvcBlockHandlerConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc.config; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.config; -import com.alibaba.csp.sentinel.adapter.spring.webmvc.ResultWrapper; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.ResultWrapper; import com.alibaba.csp.sentinel.slots.block.AbstractRule; import com.alibaba.csp.sentinel.slots.block.BlockException; import org.slf4j.Logger; diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/controller/TestController.java b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/controller/TestController.java similarity index 92% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/controller/TestController.java rename to sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/controller/TestController.java index d8a42ab8..9b9ecfe2 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/controller/TestController.java +++ b/sentinel-adapter/sentinel-spring-webmvc-v6x-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webmvc_v6x/controller/TestController.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc.controller; +package com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.controller; import org.springframework.web.bind.annotation.GetMapping; diff --git a/sentinel-adapter/sentinel-web-adapter-common/pom.xml b/sentinel-adapter/sentinel-web-adapter-common/pom.xml new file mode 100644 index 00000000..fe4da301 --- /dev/null +++ b/sentinel-adapter/sentinel-web-adapter-common/pom.xml @@ -0,0 +1,34 @@ + + + + + sentinel-adapter + com.alibaba.csp + 1.8.7 + + 4.0.0 + sentinel-web-adapter-common + jar + + 8 + 8 + + + + junit + junit + test + + + org.mockito + mockito-core + test + + + org.assertj + assertj-core + test + + + diff --git a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/callback/UrlCleaner.java b/sentinel-adapter/sentinel-web-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/web/common/UrlCleaner.java similarity index 85% rename from sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/callback/UrlCleaner.java rename to sentinel-adapter/sentinel-web-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/web/common/UrlCleaner.java index 8541d097..5c6e89d5 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-6x-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/callback/UrlCleaner.java +++ b/sentinel-adapter/sentinel-web-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/web/common/UrlCleaner.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2024 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. @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.spring.webmvc.callback; +package com.alibaba.csp.sentinel.adapter.web.common; /** * Unify the resource target. * - * @author kaizi2009 + * @since 1.8.8 */ public interface UrlCleaner {