spring boot mvc系列-静态资源配置与MappingHandler拦截器
静态资源配置
Spring Boot 默认将 /** 所有访问映射到以下目录:
classpath:/smetic
classpath:/public
classpath:/resources
classpath:/MEme-INF/resources
如果需要自定义映射目录,可以继承WebMvcConfigurerAdapter或WebMvcConfigurationSupport,以后者为例,如下:
@Configuration
public class WebConfig extends WebMvcConfigurationSupport { @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//将所有/smetic/** 访问都映射到classpath:/smetic/ 目录下
registry.addResourceHandler("/js/**").addResourceLocations("classpath:/smetic/js/");
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/smetic/resources/");
registry.addResourceHandler("/images/**").addResourceLocations("classpath:/smetic/images/");
registry.addResourceHandler("/css/**").addResourceLocations("classpath:/smetic/css/");
registry.addResourceHandler("/font/**").addResourceLocations("classpath:/smetic/font/");
registry.addResourceHandler("/themes/**").addResourceLocations("classpath:/smetic/themes/");
} }
如果使用了拦截器HandlerInterceptor,好像覆盖addResourceHandlers方法,似乎excludePathPatterns并没有生效,不覆盖的话前台会报404。
拦截器配置
同样在WebConfig中配置,如下:
package com.xxx.me.aop.config; import org.springframework.context.annometion.Bean;
import org.springframework.context.annometion.Configuration;
import org.springframework.web.servlet.config.annometion.InterceptorRegistry;
import org.springframework.web.servlet.config.annometion.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annometion.WebMvcConfigurationSupport; import com.xxx.me.interceptor.SecurityInteceptor; @Configuration
public class WebConfig extends WebMvcConfigurationSupport {
// 需要注意的是HandlerInteceptor必须通过@Bean配置,直接添加@new SecurityInteceptor()会导致依赖类未注入
@Bean
SecurityInteceptor securityInteceptor() {
return new SecurityInteceptor();
} @Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(securityInteceptor()).excludePathPatterns("/css/**", "/js/**", "/font/**", "/images/**", "/resources/**", "/themes/**");
super.addInterceptors(registry);
}
}
spring boot mvc系列-静态资源配置与MappingHandler拦截器的更多相关文章
- spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,guava限流,定时任务案例, 发邮件
本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...
- Spring Boot2 系列教程(十一)Spring Boot 中的静态资源配置
当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥 Spring Boot 中的静态资源加载问题:"松哥,我的 HTML 页面好像没有样 ...
- Spring Boot干货系列:(六)静态资源和拦截器处理
Spring Boot干货系列:(六)静态资源和拦截器处理 原创 2017-04-05 嘟嘟MD 嘟爷java超神学堂 前言 本章我们来介绍下SpringBoot对静态资源的支持以及很重要的一个类We ...
- Spring Boot干货系列:(四)Thymeleaf篇
Spring Boot干货系列:(四)Thymeleaf篇 原创 2017-04-05 嘟嘟MD 嘟爷java超神学堂 前言 Web开发是我们平时开发中至关重要的,这里就来介绍一下Spring Boo ...
- (转)Spring Boot干货系列:(四)开发Web应用之Thymeleaf篇
转:http://tengj.top/2017/03/13/springboot4/ 前言 Web开发是我们平时开发中至关重要的,这里就来介绍一下Spring Boot对Web开发的支持. 正文 Sp ...
- Spring Boot实战:静态资源处理
前两章我们分享了Spring boot对Restful 的支持,不过Restful的接口通常仅仅返回数据.而做web开发的时候,我们往往会有很多静态资源,如html.图片.css等.那如何向前端返回静 ...
- 程序员DD 《Spring boot教程系列》补充
最近在跟着程序员DD的Spring boot教程系列学习Spring boot,由于年代原因,Spring boot已经发生了一些变化,所以在这里进行一些补充. 补充的知识大多来自评论区,百度,Sta ...
- Spring Boot 中的静态资源到底要放在哪里?
当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥Spring Boot 中的静态资源加载问题:"松哥,我的HTML页面好像没有样式?& ...
- 【转】Spring Boot干货系列:(一)优雅的入门篇
转自Spring Boot干货系列:(一)优雅的入门篇 前言 Spring一直是很火的一个开源框架,在过去的一段时间里,Spring Boot在社区中热度一直很高,所以决定花时间来了解和学习,为自己做 ...
随机推荐
- [Git/GitHub] Tutorial 1. Git download and commit first project
1. Install at https://git-scm.com/downloads 2. Set up your name and email $ git config --global user ...
- js监听页面放大缩小
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>d ...
- model.addattribute()的作用
1.往前台传数据,可以传对象,可以传List,通过el表达式 ${}可以获取到, 类似于request.setAttribute("sts",sts)效果一样. 2.@ModelA ...
- Eclipse + Pydev问题 : pydev unresolved import
http://blog.csdn.net/qq_22765745/article/details/71054030http://blog.csdn.net/amghost/article/detail ...
- MySQL创建外键约束的报错Error : Can't create table '#sql-534_185' (errno: 150)
总得来说是因为两个表的字段类型不一致,例如: 两个字段的类型或大小不严格匹配,一个为tinyint,另一个为char:或一个为int(10)另一个为int(9)也是不行的,即使都为int(10),但一 ...
- <2>基本表达式和语句
1.基本表达式 1: =, +, -, *, /, 赋值,加减剩除; lua 没有 c/c++的缩写表达式 += -= *=, ++, --; 2: () 改变运算的优先级; 3: 字符串对象加法.. ...
- 5.无监督学习-DBSCAN聚类算法及应用
DBSCAN方法及应用 1.DBSCAN密度聚类简介 DBSCAN 算法是一种基于密度的聚类算法: 1.聚类的时候不需要预先指定簇的个数 2.最终的簇的个数不确定DBSCAN算法将数据点分为三类: 1 ...
- EXTENDED LIGHTS OUT (高斯消元)
In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each (the actual ...
- 从js中提取数据
<script language="JavaScript" type="text/javascript+gk-onload"> SKART = (S ...
- css 扩大点击范围
业务场景:比如某个按钮大小已经固定了,但是需求点击按钮周边就可以触发点击事件. 设置一下before属性里面的height,width就是设置你要点击的范围. rem是css3中新增加的一个单位属性( ...