1.新建一个MyWebAppConfigurer 配置静态资源需要映射的位置、建议把addResourceLocations的路径写在application.yml中、方面以后部署的打包修改配置文件的时候做外部配置文件加载、方面修改。这边为了节约时间就在代码中写了。
packageio.renren.config;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;/****@authorlyy*2021.11.21*///springboot2.x配置@ConfigurationpublicclassMyWebAppConfigurerimplementsWebMvcConfigurer{@OverridepublicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){registry.addResourceHandler("/image/**").addResourceLocations("file:C:\Users\Administrator\Desktop\11月毕设\");}}
2.没有加权限控制的话直接通过IP+端口+项目名以及addResourceHandler中的路径就可以访问了、有权限控制的话需要权限放心或放在static静态资源文件夹下。个人用的shiro权限、所以需要放行。
@Bean("shiroFilter")publicShiroFilterFactoryBeanshiroFilter(SecurityManagersecurityManager){ShiroFilterFactoryBeanshiroFilter=newShiroFilterFactoryBean();shiroFilter.setSecurityManager(securityManager);//oauth过滤Map<String,Filter>filters=newHashMap<>();filters.put("oauth2",newOAuth2Filter());shiroFilter.setFilters(filters);Map<String,String>filterMap=newLinkedHashMap<>();filterMap.put("/webjars/**","anon");filterMap.put("/druid/**","anon");filterMap.put("/app/**","anon");filterMap.put("/sys/login","anon");filterMap.put("/swagger/**","anon");filterMap.put("/v2/api-docs","anon");filterMap.put("/swagger-ui.html","anon");filterMap.put("/swagger-resources/**","anon");filterMap.put("/captcha.jpg","anon");filterMap.put("/image/**","anon");filterMap.put("/**","oauth2");shiroFilter.setFilterChainDefinitionMap(filterMap);returnshiroFilter;}
3.映射成功、访问http://localhost:8080/renren-fast/image/v2-4f45411c72eb128a6085fc8173286ffc_1440w.jpg
作者:Java李杨勇