在springbooot中拦截.do请求的方法

在springbooot中拦截.do请求的方法

package com.pekcbec.whm.web.config;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * ServletConfig
 *
 *
 */
@Configuration
public class ServletConfig implements WebMvcConfigurer {

    /**
     * 配置url资源访问到指定目录
     * 如此处不限制访问,可直接通过 spring.mvc.static-path-pattern=/** 即可允许所有请求到资源目录
     *
     * @param registry 资源处理器注册
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
//        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
//        registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/images/");
//        registry.addResourceHandler("/plugins/**").addResourceLocations("classpath:/static/plugins/");
//        registry.addResourceHandler("/pageJs/**").addResourceLocations("classpath:/static/pageJs/");
    }

    /**
     * springboot2默认将localhost/test和localhost/test.do认为是2个url
     * 先将localhost/test.*和localhost/test映射到统一方法
     * 再指定拦截.do
     *
     * @param configurer 路径匹配配置
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseRegisteredSuffixPatternMatch(true);
    }

    /**
     * 指定拦截.do
     *
     * @param dispatcherServlet servlet调度器
     * @return ServletRegistrationBean
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean servletServletRegistrationBean = new ServletRegistrationBean<>(dispatcherServlet);
        servletServletRegistrationBean.addUrlMappings("*.do");
        // name默认为"dispatcherServlet"而spring boot提供的DispatcherServlet的name就是"dispatcherServlet"
        // 如果不指定,则覆盖"dispatcherServlet",则默认策略全部失效
        servletServletRegistrationBean.setName("action");
        return servletServletRegistrationBean;
    }
}

你可能感兴趣的:(springboot)