Springboot配置圖片虛擬映射示例代碼

1.新建一個MyWebAppConfigurer 配置靜態資源需要映射的位置、建議把addResourceLocations的路徑寫在application.yml中、方面以後部署的打包修改配置文件的時候做外部配置文件加載、方面修改。這邊為瞭節約時間就在代碼中寫瞭。

package io.renren.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * 
 * @author lyy
 * 2021.11.21
 */
//springboot 2.x配置
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/**").addResourceLocations("file:C:\Users\Administrator\Desktop\11月畢設\");
    }
}

2.沒有加權限控制的話直接通過IP+端口+項目名以及addResourceHandler中的路徑就可以訪問瞭、有權限控制的話需要權限放心或放在static靜態資源文件夾下。個人用的shiro權限、所以需要放行。

 @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);

        //oauth過濾
        Map<String, Filter> filters = new HashMap<>();
        filters.put("oauth2", new OAuth2Filter());
        shiroFilter.setFilters(filters);

        Map<String, String> filterMap = new LinkedHashMap<>();
        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);

        return shiroFilter;
    }

3.映射成功、訪問http://localhost:8080/renren-fast/image/v2-4f45411c72eb128a6085fc8173286ffc_1440w.jpg

總結

到此這篇關於Springboot配置圖片虛擬映射的文章就介紹到這瞭,更多相關Springboot配置圖片虛擬映射內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: