@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
/**
* 解决跨域问题
*
* @param registry 注册器
*/
@Override
public void addCorsMappings(@NotNull CorsRegistry registry) {
registry.addMapping("/**")
//是否发送Cookie
.allowCredentials(true)
//放行哪些原始域
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*")
.exposedHeaders("*");
}
/**
* 添加静态资源
*
* @param registry 注册
*/
@Override
public void addResourceHandlers(@NotNull ResourceHandlerRegistry registry) {
/*静态资源的位置*/
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
}
/**
* 添加拦截器 注入到容器中
*
* @param registry 注册
*/
@Override
public void addInterceptors(@NotNull InterceptorRegistry registry) {
//创建拦截器对象
HandlerInterceptor interceptor = new LoginInterceptor();
//指定拦截的地址
String[] path = {"/**"};
//指定不拦截的地址:对登录、首页、静态资源等进行放行
String[] excludePath = {
"/user/login", "/css/**", "/fonts/**", "/images/**", "/js/**", "/lib/**"
};
registry.addInterceptor(interceptor)
.addPathPatterns(path)
.excludePathPatterns(excludePath);
}
}
Spring Boot解决跨域问题以及解决添加拦截配置(对静态资源放行)
最新推荐文章于 2024-04-16 08:14:39 发布