这篇文章主要介绍了springboot项目访问静态资源的配置代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 这里只是简单
这篇文章主要介绍了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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。