web配置类 相当于 web.xml配置文件。 配置类要实现WebMvcConfigurer接口。
以下代码为 :SpringMVC可以拦截.html为结尾的请求
package com.jt.config; import com.jt.interceptor.UserInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration //配置web.xml配置文件 public class MvcConfigurer implements WebMvcConfigurer{ //开启匹配后缀型配置,为了将来程序实现静态页面的跳转而准备的 @Override public void configurePathMatch(PathMatchConfigurer configurer) { configurer.setUseSuffixPatternMatch(true); } //添加拦截器配置 @Autowired private UserInterceptor userInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(userInterceptor).addPathPatterns("/cart/**","/order/**"); } }以下代码为 :SpringMVC可以实现CORS跨域
package com.jt.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class CORSConfig implements WebMvcConfigurer { /** * 实现跨域的方式 * 需要配置服务端程序 * 方法说明: * 1.addMapping(/**) 允许什么样的请求可以跨域 所有的请求 * 2.allowedOrigins("*")可以允许任意的域名 * 3.allowCredentials(true) 跨域时是否允许携带cookie等参数 */ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowCredentials(true); } }