All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.sentinel.base.config.web.ServletSentinelConfigurer Maven / Gradle / Ivy

package org.zodiac.sentinel.base.config.web;

import java.util.Objects;
import java.util.Optional;

import org.slf4j.Logger;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.zodiac.commons.logging.SmartSlf4jLoggerFactory;
import org.zodiac.commons.util.lang.Strings;
import org.zodiac.sentinel.base.client.callback.servlet.ServletPlatformBlockFallbackHandler;
import org.zodiac.sentinel.base.client.callback.servlet.parser.DefaultRequestOriginParser;
import org.zodiac.sentinel.base.config.SentinelConfigInfo;
import org.zodiac.sentinel.base.config.SentinelFilterInfo;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor;
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.callback.UrlCleaner;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig;

public class ServletSentinelConfigurer implements WebMvcConfigurer {

    private static final Logger log = SmartSlf4jLoggerFactory.getLogger(ServletSentinelConfigurer.class);

    private String projectName;
    private final SentinelConfigInfo sentinelConfigInfo;

    private Optional urlCleanerOptional;

    private Optional blockExceptionHandlerOptional;

    private Optional requestOriginParserOptional;

    private Optional sentinelWebInterceptorOptional;

    public ServletSentinelConfigurer(String projectName, SentinelConfigInfo sentinelConfigInfo,
        Optional urlCleanerOptional, Optional blockExceptionHandlerOptional,
        Optional requestOriginParserOptional,
        Optional sentinelWebInterceptorOptional) {
        this.projectName = Objects.requireNonNull(projectName, "projectName");
        this.sentinelConfigInfo = sentinelConfigInfo;
        this.urlCleanerOptional = urlCleanerOptional;
        this.blockExceptionHandlerOptional = blockExceptionHandlerOptional;
        this.requestOriginParserOptional = requestOriginParserOptional;
        this.sentinelWebInterceptorOptional = sentinelWebInterceptorOptional;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (!sentinelWebInterceptorOptional.isPresent()) {
            return;
        }
        SentinelFilterInfo filterConfig = sentinelConfigInfo.getHttp().getFilter();
        registry.addInterceptor(sentinelWebInterceptorOptional.get())
                .order(filterConfig.getOrder())
                .addPathPatterns(filterConfig.getUrlPatterns());
        log.info(
                "[Sentinel Starter] register SentinelWebInterceptor with urlPatterns: {}.", filterConfig.getUrlPatterns());
    }

    public BlockExceptionHandler blockExceptionHandler() {
        //return new DefaultBlockExceptionHandler();
        return new ServletPlatformBlockFallbackHandler();
    }

    public DefaultRequestOriginParser requestOriginParser() {
        return new DefaultRequestOriginParser(projectName);
    }

    public SentinelWebMvcConfig sentinelWebMvcConfig() {
        SentinelWebMvcConfig sentinelWebMvcConfig = new SentinelWebMvcConfig();
        sentinelWebMvcConfig.setHttpMethodSpecify(sentinelConfigInfo.getHttp().isHttpMethodSpecify());
        sentinelWebMvcConfig.setWebContextUnify(sentinelConfigInfo.getHttp().isWebContextUnify());

        if (blockExceptionHandlerOptional.isPresent()) {
            blockExceptionHandlerOptional.ifPresent(sentinelWebMvcConfig::setBlockExceptionHandler);
        } else {
            if (Strings.notBlank(sentinelConfigInfo.getBlockPage())) {
                sentinelWebMvcConfig.setBlockExceptionHandler(((request, response, e) -> response.sendRedirect(sentinelConfigInfo.getBlockPage())));
            } else {
                sentinelWebMvcConfig.setBlockExceptionHandler(blockExceptionHandler());
            }
        }

        urlCleanerOptional.ifPresent(sentinelWebMvcConfig::setUrlCleaner);
        if (requestOriginParserOptional.isPresent()) {
            requestOriginParserOptional.ifPresent(sentinelWebMvcConfig::setOriginParser);
        } else {
            sentinelWebMvcConfig.setOriginParser(this.requestOriginParser());
        }

        return sentinelWebMvcConfig;
    }

    public SentinelWebInterceptor sentinelWebInterceptor(SentinelWebMvcConfig sentinelWebMvcConfig) {
        return new SentinelWebInterceptor(sentinelWebMvcConfig);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy