springboot專案訪問靜態資源的配置程式碼例項
阿新 • • 發佈:2020-01-07
這篇文章主要介紹了springboot專案訪問靜態資源的配置程式碼例項,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
這裡只是簡單記錄當上傳圖片不是放在tomcat其他伺服器中時,只是放在磁碟中便可以這樣配置,在專案啟動後可以訪問到磁碟中的資源。
@Configuration public class SystemConfigurer implements WebMvcConfigurer { @Value("${jeewx.path.upload}") private String upLoadPath; //配置的檔案的儲存位置如 “E://image” @Value("${spring.resource.static-locations}") private String staticLocations; //其他靜態資源位置,如js,css檔案 /**預設攔截器排除資源*/ private List<String> EXCLUDE_PATHS= Arrays.asList("/plug-in/**","/content/**","/upload/**","/system/*.do","/error"); @Autowired private LoginInterceptor loginInterceptor; @Autowired private AccessSignInterceptor accessSignInterceptor; @Value("${jeewx.interceptor.is-open}") private boolean isOpen; @Value("${jeewx.interceptor.excludeUrls.login-interceptor}") private String loginInterceptorExcludeUrls; @Value("${jeewx.interceptor.excludeUrls.access-sign-interceptor}") private String accessSignInterceptorExcludeUrls; /** * 靜態資源的配置 - 使得可以從磁碟中讀取 Html、圖片、視訊、音訊等 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("file:" + upLoadPath + "//") .addResourceLocations(staticLocations.split(",")); } /** * 登入驗證碼 */ @Bean @SuppressWarnings({ "rawtypes","unchecked" }) public ServletRegistrationBean randCodeImageServlet() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new RandCodeImageServlet(),"/randCodeImage"); return servletRegistrationBean; } /** * 攔截器(登入 + 簽名) */ public void addInterceptors(InterceptorRegistry registry) { if(isOpen) { log.info("loginInterceptorExcludeUrls: "+loginInterceptorExcludeUrls); log.info("accessSignInterceptorExcludeUrls: "+accessSignInterceptorExcludeUrls); registry.addInterceptor(loginInterceptor).addPathPatterns("/**/back/**/*").excludePathPatterns(EXCLUDE_PATHS).excludePathPatterns(loginInterceptorExcludeUrls.split(",")); registry.addInterceptor(accessSignInterceptor).addPathPatterns("/**").excludePathPatterns("/**/back/**").excludePathPatterns(EXCLUDE_PATHS).excludePathPatterns(accessSignInterceptorExcludeUrls.split(",")); } } /** * 預設跳轉登入頁面 */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("redirect:/system/login.do"); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。