springboot配置静态资源访问

重写WebMvcConfigurer中的addResourceHandles

1.配置静态资源地址

 

2.重写

 

完整代码

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Value("${file.path}")
    private String path; // D:/ndedu/image/

    @Value("${file.static-path}")
    private String staticPath; // /upload/image/**
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new JWTInterceptor())
                .excludePathPatterns("/api/user/login")   //登陆接口放行
                .excludePathPatterns("/mini/api/user/login")   //登陆接口放行
                .excludePathPatterns("/api/user/getCaptcha/*")   //获取验证码放行
                .excludePathPatterns("/api/user/changePwd")   //修改密码放行
                .excludePathPatterns("/api/upgrade")   //升级数据
                .excludePathPatterns("/api/file/upload")   // 文件上传接口放行
                .excludePathPatterns("/api/fyPayOrder/payResultNotify")   // 富友回调
                .excludePathPatterns("/mini/api/file/upload")
                .excludePathPatterns("/server/**")      //网站所有接口不需要token验证
                .excludePathPatterns("/error")
                .excludePathPatterns("/upload/image/**","/swagger-ui/**","/swagger/**",
                        "/swagger-resources/**", "/webjars/**", "/v3/**", "/v2/**", "/swagger-ui.html/**") // 静态资源不需要拦截
                .addPathPatterns("/**");     //其他接口token验证
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 外界访问映射好的: http://127.0.0.1:8081/upload/image/
        // 就相当于自己访问: D:/ndedu/image/
        registry.addResourceHandler(staticPath).addResourceLocations("file:"+path);
    }

}

 

posted @ 2023-11-03 10:47  fnasklf  阅读(170)  评论(0编辑  收藏  举报