springboot2.x以上访问外部静态资源

springboot2.x 加载静态文件

升级到springboot2.x,网上的资料都是1.x的

springboot 2.x的版本


import com.zzfeidu.interceptor.AuthenticationInterceptor;
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.WebMvcConfigurationSupport;

@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurationSupport {

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		//设置允许跨域的路径
		registry.addMapping("/**")
				//设置允许跨域请求的域名
				.allowedOrigins("*")
				//是否允许证书 不再默认开启
				.allowCredentials(true)
				//设置允许的方法
				.allowedMethods("*")
				//跨域允许时间
				.maxAge(36000);
	}

	@Bean
	public AuthenticationInterceptor setBean2(){
		return new AuthenticationInterceptor();
	}

	@Override
	protected void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(setBean2()).addPathPatterns("/**");
		super.addInterceptors(registry);
	}

	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("swagger-ui.html","doc.html")
				.addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**")
				.addResourceLocations("classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/uploadData/**").addResourceLocations("file:D://xx/uploadData/");
		super.addResourceHandlers(registry);
	}

}

 


拦截 /uploadData/ 的资源去 file:D:\\static\\ D盘下面的

D://feidu/uploadData/

里面找。这样就可以找到了

 

你可能感兴趣的:(springboot2.x以上访问外部静态资源)