spring boot之spring mvc路径匹配


第一种方式

  • 第一步,application.yml添加如下配置,use-suffix-pattern的作用是将http://www/ww.xx的链接同http://www/ww等价,配置完后会在匹配servlet时将.xx自动忽略
spring:
  mvc:
    pathmatch:
      use-suffix-pattern: false #
      use-registered-suffix-pattern: true #默认为false,同use-suffix-pattern作用一致,但仅支持第二步注册过的后缀,这样更安全
    contentnegotiation:
      favor-path-extension: false # 不知道什么意思
  • 第二步,添加过滤器,过滤指定后缀链接,如下只允许.do的链接通过
@SpringBootApplication
@ServletComponentScan
public class JtiismUwpWebApplication {

	public static void main(String[] args) {
		SpringApplication.run(JtiismUwpWebApplication.class, args);
	}

	@Bean
	public ServletRegistrationBean servletRegistrationBean(DispatcherServlet dispatcherServlet) {
		ServletRegistrationBean<DispatcherServlet> servletServletRegistrationBean = new ServletRegistrationBean<>(dispatcherServlet);
		servletServletRegistrationBean.addUrlMappings("*.do");
		return servletServletRegistrationBean;
	}
}

第二种方式

  • 第一步,添加配置类
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
	 @Override 
	 public void configurePathMatch(PathMatchConfigurer pathMatchConfigurer) { 
	 		pathMatchConfigurer.setUseSuffixPatternMatch(false); 
	 		 //将http://www/ww.xx的链接同http://www/ww等价,配置完后会在匹配servlet时将.xx自动忽略
	 		pathMatchConfigurer.setUseRegisteredSuffixPatternMatch(true); 
	 }
	  @Override 
	  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
	  	configurer.favorPathExtension(false); 
	  }

}
  • 第二步,同第一种方式

猜你喜欢

转载自blog.csdn.net/u014296316/article/details/87469365