Tip
1.如果有拦截器,请开放该接口
2.项目启动时会调用该接口
3.请求的连接 http://localhost:8848/files/test.png
应用场景:
发起请求/files/**,访问文件的目录下的路径。
package com.fusdom.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @创建人: giao
* @创建时间: 2020/8/4
* @描述: 解决跨域问题
*/
//@Configuration注解
@Configuration
//实现WebMvcConfigurer
public class CrossDomainConfig implements WebMvcConfigurer {
//重写addCorsMappings方法
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedOrigins("*")
.allowedMethods("GET", "POST", "OPTIONS")
.maxAge(3600);
}
// /**
// * 这里需要先将限流拦截器入住,不然无法获取到拦截器中的redistemplate
// * @return
// */
// @Bean
// public WeChatLoginInterceptor getWeChatLoginInterceptor() {
// return new WeChatLoginInterceptor();
// }
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//获取文件的真实路径
// String path = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\asserts\\img\\wx_icon\\";
// System.out.println(path);
//static/upload/**是对应resource下工程目录
registry.addResourceHandler("/files/**").addResourceLocations("file:D:\\Git\\ht\\micro_services\\cloud_base\\src\\main\\resources\\static\\asserts\\img\\gogo\\");
WebMvcConfigurer.super.addResourceHandlers(registry);
}
}