Search in sources :

Example 1 with UrlCleaner

use of com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner in project Sentinel by alibaba.

the class SentinelWebInterceptor method getResourceName.

@Override
protected String getResourceName(HttpServletRequest request) {
    // Resolve the Spring Web URL pattern from the request attribute.
    Object resourceNameObject = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
    if (resourceNameObject == null || !(resourceNameObject instanceof String)) {
        return null;
    }
    String resourceName = (String) resourceNameObject;
    UrlCleaner urlCleaner = config.getUrlCleaner();
    if (urlCleaner != null) {
        resourceName = urlCleaner.clean(resourceName);
    }
    // Add method specification if necessary
    if (StringUtil.isNotEmpty(resourceName) && config.isHttpMethodSpecify()) {
        resourceName = request.getMethod().toUpperCase() + ":" + resourceName;
    }
    return resourceName;
}
Also used : UrlCleaner(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner)

Example 2 with UrlCleaner

use of com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner in project blade-tool by chillzhuang.

the class BladeSentinelFilterConfiguration method sentinelWebMvcConfig.

@Bean
public SentinelWebMvcConfig sentinelWebMvcConfig(SentinelProperties properties, Optional<UrlCleaner> urlCleanerOptional, Optional<BlockExceptionHandler> blockExceptionHandlerOptional, Optional<RequestOriginParser> requestOriginParserOptional) {
    SentinelWebMvcConfig sentinelWebMvcConfig = new SentinelWebMvcConfig();
    sentinelWebMvcConfig.setHttpMethodSpecify(properties.getHttpMethodSpecify());
    sentinelWebMvcConfig.setWebContextUnify(properties.getWebContextUnify());
    if (blockExceptionHandlerOptional.isPresent()) {
        blockExceptionHandlerOptional.ifPresent(sentinelWebMvcConfig::setBlockExceptionHandler);
    } else {
        if (StringUtils.hasText(properties.getBlockPage())) {
            sentinelWebMvcConfig.setBlockExceptionHandler(((request, response, e) -> response.sendRedirect(properties.getBlockPage())));
        } else {
            sentinelWebMvcConfig.setBlockExceptionHandler(new DefaultBlockExceptionHandler());
        }
    }
    urlCleanerOptional.ifPresent(sentinelWebMvcConfig::setUrlCleaner);
    requestOriginParserOptional.ifPresent(sentinelWebMvcConfig::setOriginParser);
    return sentinelWebMvcConfig;
}
Also used : DefaultBlockExceptionHandler(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler) SentinelWebMvcConfig(com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig) SentinelWebInterceptor(com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor) UrlCleaner(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner) RequiredArgsConstructor(lombok.RequiredArgsConstructor) SentinelProperties(com.alibaba.cloud.sentinel.SentinelProperties) Optional(java.util.Optional) RequestOriginParser(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser) Bean(org.springframework.context.annotation.Bean) BlockExceptionHandler(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler) ConditionalOnWebApplication(org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication) StringUtils(org.springframework.util.StringUtils) DefaultBlockExceptionHandler(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler) SentinelWebMvcConfig(com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig) Bean(org.springframework.context.annotation.Bean)

Aggregations

UrlCleaner (com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner)2 SentinelProperties (com.alibaba.cloud.sentinel.SentinelProperties)1 SentinelWebInterceptor (com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor)1 BlockExceptionHandler (com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler)1 DefaultBlockExceptionHandler (com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler)1 RequestOriginParser (com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser)1 SentinelWebMvcConfig (com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig)1 Optional (java.util.Optional)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 ConditionalOnWebApplication (org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication)1 Bean (org.springframework.context.annotation.Bean)1 StringUtils (org.springframework.util.StringUtils)1