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();
		}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章