spring boot项目 直接访问图片处理(跳过token)

@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private JwtInterceptor jwtInterceptor;
/**
* 添加拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//excludePathPatterns中的路径不进行拦截。
//拦截路径可自行配置多个 可用 ,分隔开
registry.addInterceptor(jwtInterceptor).addPathPatterns("/**").excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**","/api/**");
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// /home/file/**为前端URL访问路径 后面 file:xxxx为本地磁盘映射
// registry.addResourceHandler("/home/file/**").addResourceLocations("file:C:" + uploadPath);
String os = System.getProperty("os.name");

if (os.toLowerCase().startsWith("win")) { //如果是Windows系统

registry.addResourceHandler("/topoAlarm/image/**")
// /apple/**表示在磁盘apple目录下的所有资源会被解析为以下的路径
.addResourceLocations("file:D:\\ideawork\\2021\\hcy-porttoport-service\\topoAlarm\\image/") //媒体资源
.addResourceLocations("classpath:/META-INF/resources/"); //swagger2页面
} else { //linux 和mac
registry.addResourceHandler("/topoAlarm/image/**")
.addResourceLocations("file:./topoAlarm/image/") //媒体资源
.addResourceLocations("classpath:/META-INF/resources/"); //swagger2页面;
}

}
}

/访问图片跳过验证
String requestURI = request.getRequestURI();
// if (requestURI.startsWith("/img") && FileUtils.IMG_FILTER.contains(requestURI.substring(requestURI.lastIndexOf(".") + 1))) {
if (FileUtils.IMG_FILTER.contains(requestURI.substring(requestURI.lastIndexOf(".") + 1))) {
return true;
}

posted on 2021-03-17 14:44  博客海沐沐  阅读(1541)  评论(0)    收藏  举报

导航