Springboot中静态资源和拦截器处理(踩了坑)

 

背景: 在项目中我使用了自定义的Filter 这时候过滤了很多路径,当然对静态资源我是直接放过去的,但是,还是出现了静态资源没办法访问到springboot默认的文件夹中得文件

springBoot2.0 访问磁盘图片实现方式

 @Configuration
public class MyWebConfig implements WebMvcConfigurer { @Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("app/index");
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/temp/**").addResourceLocations("file:d:/temp/");
}
}

说下默认映射的文件夹有:

classpath:/META-INF/resources

  • classpath:/resources

  • classpath:/static

  • classpath:/public

上面这几个都是静态资源的映射路径,优先级顺序为:META-INF/resources > resources > static > public

我们可以通过修改spring.mvc.static-path-pattern来修改默认的映射**

*************************接管Spring Boot的Web配置 ************************这是重点中得重点********************

如果Spring Boot提供的Sping MVC不符合要求,则可以通过一个配置类(注解有@Configuration的类)加上@EnableWebMvc注解来实现完全自己控制的MVC配置。

当然,通常情况下,Spring Boot的自动配置是符合我们大多数需求的。在你既需要保留Spring Boot提供的便利,有需要增加自己的额外的配置的时候,可以定义一个配置类并继承WebMvcConfigurerAdapter,无需使用@EnableWebMvc注解。

这里我们提到这个WebMvcConfigurerAdapter这个类,重写这个类中的方法可以让我们增加额外的配置

自定义资源映射addResourceHandlers

比如,我们想自定义静态资源映射目录的话,只需重写addResourceHandlers方法即可。

通过addResourceHandler添加映射路径,然后通过addResourceLocations来指定路径。我们访问自定义my文件夹中

@Configuration
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
/**
* 配置静态访问资源
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/my/**").addResourceLocations("classpath:/my/");
super.addResourceHandlers(registry);
}
}

如果你想指定外部的目录也很简单,直接addResourceLocations指定即可,代码如下:

 public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/my/**").addResourceLocations("file:E:/my/");
super.addResourceHandlers(registry);
}

addResourceLocations指的是文件放置的目录,addResoureHandler指的是对外暴露的访问路径

页面跳转addViewControllers

以前写SpringMVC的时候,如果需要访问一个页面,必须要写Controller类,然后再写一个方法跳转到页面,感觉好麻烦,其实重写WebMvcConfigurerAdapter中的addViewControllers方法即可达到效果了

/**
* 以前要访问一个页面需要先创建个Controller控制类,再写方法跳转到页面
* 在这里配置后就不需要那么麻烦了,直接访问http://localhost:8080/toLogin就跳转到login.jsp页面了
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/toLogin").setViewName("login");
super.addViewControllers(registry);
}

拦截器在我们项目中经常使用的,这里就来介绍下最简单的判断是否登录的使用。
要实现拦截器功能需要完成以下2个步骤:

  • 创建我们自己的拦截器类并实现 HandlerInterceptor 接口
  • 其实重写WebMvcConfigurerAdapter中的addInterceptors方法把自定义的拦截器类添加进来即可

首先,自定义拦截器代码:

public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
boolean flag =true;
User user=(User)request.getSession().getAttribute("user");
if(null==user){
response.sendRedirect("toLogin");
flag = false;
}else{
flag = true;
}
return flag;
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
} @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}

这里我们简单实现了根据session中是否有User对象来判断是否登录,为空就跳转到登录页,不为空就通过。

接着,重写WebMvcConfigurerAdapter中的addInterceptors方法如下:

/**
* 拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/toLogin","/login");
super.addInterceptors(registry);
}

addPathPatterns("/**")对所有请求都拦截,但是排除了/toLogin/login请求的拦截


spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案的更多相关文章

  1. spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案(转)

    文章转自 http://blog.51cto.com/12066352/2093750 最近项目采用spring icloud,用的spring boot版本是1.5.x的,spring boot 2 ...

  2. Spring boot拦截器的实现

    Spring boot拦截器的实现 Spring boot自带HandlerInterceptor,可通过继承它来实现拦截功能,其的功能跟过滤器类似,但是提供更精细的的控制能力. 1.注册拦截器 @C ...

  3. 【spring boot】spring boot 拦截器

    今日份代码: 1.定义拦截器 import com.alibaba.fastjson.JSON; import org.apache.commons.collections.CollectionUti ...

  4. spring boot拦截器配置

    1.在spring boot配置文件application.properties中添加要拦截的链接 com.url.interceptor=/user/test 2.编写拦截器代码 ,创建UrlInt ...

  5. 13. Spring Boot 拦截器

    1.thymeleaf  页面修改后可能不会实时反馈到Web,做2步骤: 1)禁用掉tymleaf 缓存: spring.thymeleaf.cache=false 2)IDE编辑器:Ctrl + F ...

  6. spring boot拦截器

    实现自定义拦截器只需要3步: 1.创建我们自己的拦截器类并实现 HandlerInterceptor 接口. 2.创建一个Java类继承WebMvcConfigurerAdapter,并重写 addI ...

  7. (22)Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】

    上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API,与Spring 没什么关系.     Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Sprin ...

  8. spring boot 拦截器

    @SpringBootApplicationpublic class Application extends WebMvcConfigurerAdapter { public static void ...

  9. 九、 Spring Boot 拦截器

    过滤器属于Servlet范畴的API,与spring 没什么关系. Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截 ...

随机推荐

  1. Mybatis3——使用学习(一)

    目录 Mybatis Mybatis参考资源 Mybatis 使用 肯定TM要跑起来 XML映射配置文件 Mapper XML 文件 Mybatis Mybatis参考资源 Mybatis官网手册:h ...

  2. SNF快速开发平台3.0之-界面个性化配置+10种皮肤+7种菜单-Asp.net+MVC4.0+WebAPI+EasyUI+Knockout

    一.个性配置-首页:可以进行拖动保存配置,下次登录时就会按配置的进行加载 二.个人配置页面 7种菜单用户可自定义配置,和预览效果 10种皮肤自定义配置,和预览效果 皮肤和菜单可以随意组合-部分截图: ...

  3. Spring Boot 2.0 热部署指南

    Spring Boot 2.0 支持热部署,实现方法很简单 Spring Boot 2.0 有几种热重载的选项. 推荐的方法是使用spring-boot-devtools 因为它提供了额外的开发时间功 ...

  4. [svc]linux的ip命令操作接口和路由表

    参考: https://www.tecmint.com/ip-command-examples/ 学会linux的配置ip,配置网关,添加路由等命令 man ip man ip address man ...

  5. python学习之struct模块

    class struct.Struct(format) 返回一个struct对象(结构体,参考C). 该对象可以根据格式化字符串的格式来读写二进制数据. 第一个参数(格式化字符串)可以指定字节的顺序. ...

  6. Git应用实践(二)

    [时间:2017-08] [状态:Open] [关键词:Git,git diff, git apply, git format-patch, git am, git log] 0-背景 距上次总结Gi ...

  7. [转]对form:input标签中的数字进行格式化

    原文地址:https://blog.csdn.net/qq_29662201/article/details/80708373 数字进行格式化(保留2位小数) 单独使用<fmt:formatNu ...

  8. 教你一招:解决Windows 开机弹出AotuIt ERROR 错误

    AutoIt是个脚本语言,常被用于自动化安装.网络上有些系统镜像里含有AutoIt脚本,用于系统的自动配置.出现这种问题往往有两种可能的原因: 1)做系统的时候没搞好.这种情况就需要换一个镜像文件. ...

  9. rqalpha环境搭建(windows版)

    windows环境: win7 64bit rqalpha版本3.0.9 参考文档:http://rqalpha.readthedocs.io/zh_CN/latest/intro/install.h ...

  10. Excel公式中使用动态计算的地址

    例:统计A列第四行开始,到公式所在行的前一行的非空白行的个数: =COUNTA(A4:INDIRECT(ADDRESS(ROW()-,COLUMN())))