Springboot2.0项目加载静态资源显示404/401

文件位置:经过文件上传后保存在本地的一个upload_test文件夹中
在这里插入图片描述
配置WebConfig

实现WebMvcConfigurer类

@Configuration
public class WebConfig implements WebMvcConfigurer {}

重写addResourceHandlers方法

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置访问前缀
        registry.addResourceHandler("/home/hobo/upload_test/**")
                //配置文件真实路径
                .addResourceLocations("file:/home/hobo/upload_test/");
    }

效果图

在这里插入图片描述

如果用security的同学加载显示401 权限认证失败的话,在SecurityConfig 现将文件路径配置为公开

 @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                //token的验证方式不需要开启csrf的防护
                .csrf().disable()
                .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
                .and()
                //设置无状态的连接,即不创建session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
//                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
//                配置静态路径
				 .antMatchers("/home/hobo/upload_test/**").permitAll()
				 .anyRequest().authenticated();
		}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章