博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot干货系列:(六)静态资源和拦截器处理 | 掘金技术征文
阅读量:6968 次
发布时间:2019-06-27

本文共 7585 字,大约阅读时间需要 25 分钟。

原本地址:

博客地址:

前言

本章我们来介绍下SpringBoot对静态资源的支持以及很重要的一个类WebMvcConfigurerAdapter。

正文

前面章节我们也有简单介绍过SpringBoot中对静态资源的默认支持,今天详细的来介绍下默认的支持,以及自定义扩展如何实现。

默认资源映射

Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性。

建议大家使用Spring Boot的默认配置方式,提供的静态资源映射如下:

  • classpath:/META-INF/resources
  • classpath:/resources
  • classpath:/static
  • classpath:/public

在工程里面路径是这样:

sprngboot6-1.jpg
上面这几个都是静态资源的映射路径,优先级顺序为:META-INF/resources > resources > static > public
大家可以自己在上面4个路径下都放一张同名的图片,访问一下即可验证。
还有,你可以随机在上面一个路径下面放上index.html,当我们访问应用根目录 时,会直接映射到index.html页面。

对应的配置文件配置如下:

# 默认值为 /**spring.mvc.static-path-pattern=# 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/spring.resources.static-locations=这里设置要指向的路径,多个使用英文逗号隔开复制代码

我们可以通过修改spring.mvc.static-path-pattern来修改默认的映射,例如我改成/dudu/**,那运行的时候访问 才对应到index.html页面。

接管Spring Boot的Web配置

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

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

这里我们提到这个WebMvcConfigurerAdapter这个类,重写这个类中的方法可以让我们增加额外的配置,这里我们就介绍几个常用的。

自定义资源映射addResourceHandlers

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

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

通过addResourceHandler添加映射路径,然后通过addResourceLocations来指定路径。我们访问自定义my文件夹中的elephant.jpg 图片的地址为

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

@Override    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.htm页面了     * @param registry     */    @Override    public void addViewControllers(ViewControllerRegistry registry) {        registry.addViewController("/toLogin").setViewName("login");        super.addViewControllers(registry);    }复制代码

值的指出的是,在这里重写addViewControllers方法,并不会覆盖WebMvcAutoConfiguration中的addViewControllers(在此方法中,Spring Boot将“/”映射至index.html),这也就意味着我们自己的配置和Spring Boot的自动配置同时有效,这也是我们推荐添加自己的MVC配置的方式。

拦截器addInterceptors

拦截器在我们项目中经常使用的,这里就来介绍下最简单的判断是否登录的使用。

要实现拦截器功能需要完成以下2个步骤:

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

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

package com.dudu.interceptor;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*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {    // addPathPatterns 用于添加拦截规则    // excludePathPatterns 用户排除拦截    registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/toLogin","/login");    super.addInterceptors(registry);}复制代码

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

页面登录关键代码

//简单登录操作$("#doLogin").click(function (e) {    $.ajax({    type : "POST",    url : "/login",    data : {        "userName" : $("#userName").val(),        "password" : $("#password").val()    },    dataType : "json",    success : function(data) {        if (data.result == "1") {        window.location.href ="/learn";        } else {        alert("账号密码不能为空!");        }    }    });});复制代码

控制器代码:

package com.dudu.controller;@Controllerpublic class LearnController {    /**    *登录操作    **/    @RequestMapping(value = "/login",method = RequestMethod.POST)    @ResponseBody    public Map
login(HttpServletRequest request, HttpServletResponse response){ Map
map =new HashMap
(); String userName=request.getParameter("userName"); String password=request.getParameter("password"); if(!userName.equals("") && password!=""){ User user =new User(userName,password); request.getSession().setAttribute("user",user); map.put("result","1"); }else{ map.put("result","0"); } return map; } @RequestMapping("/learn") public ModelAndView index(){ List
learnList =new ArrayList
(); LearnResouce bean =new LearnResouce("官方参考文档","Spring Boot Reference Guide","http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/#getting-started-first-application"); learnList.add(bean); bean =new LearnResouce("官方SpriongBoot例子","官方SpriongBoot例子","https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples"); learnList.add(bean); bean =new LearnResouce("龙国学院","Spring Boot 教程系列学习","http://www.roncoo.com/article/detail/125488"); learnList.add(bean); bean =new LearnResouce("嘟嘟MD独立博客","Spring Boot干货系列 ","http://tengj.top/"); learnList.add(bean); bean =new LearnResouce("后端编程嘟","Spring Boot教程和视频 ","http://www.toutiao.com/m1559096720023553/"); learnList.add(bean); bean =new LearnResouce("程序猿DD","Spring Boot系列","http://www.roncoo.com/article/detail/125488"); learnList.add(bean); bean =new LearnResouce("纯洁的微笑","Sping Boot系列文章","http://www.ityouknow.com/spring-boot"); learnList.add(bean); bean =new LearnResouce("CSDN——小当博客专栏","Sping Boot学习","http://blog.csdn.net/column/details/spring-boot.html"); learnList.add(bean); bean =new LearnResouce("梁桂钊的博客","Spring Boot 揭秘与实战","http://blog.csdn.net/column/details/spring-boot.html"); learnList.add(bean); bean =new LearnResouce("林祥纤博客系列","从零开始学Spring Boot ","http://412887952-qq-com.iteye.com/category/356333"); learnList.add(bean); ModelAndView modelAndView = new ModelAndView("/template"); modelAndView.addObject("learnList", learnList); return modelAndView; }}复制代码

这样访问的时候,如果未登录就会跳转到login.html页面,而访问 和 不会被拦截。

springboot6.gif
更多配置可以查看WebMvcConfigurerAdapter的类的API。因其是WebMvcConfigurer接口的实现,所以WebMvcConfigurer的API方法也可以用来配置MVC。
只是实现这个接口的话,要实现所有的方法,这个就尴尬了。
所以还是推荐使用继承WebMvcConfigurerAdapter类来处理。

总结

静态资源跟拦截器在平时项目中经常用到,弄懂如何处理是很有用的。今天就到此为止,下一篇将来介绍一下项目中如何使用日志。

想要查看更多Spring Boot干货教程,可前往:


一直觉得自己写的不是技术,而是情怀,一篇篇文章是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你少走弯路,希望我能帮你抹去知识的蒙尘,希望我能帮你理清知识的脉络,希望未来技术之巅上有你也有我,希望大爷你看完打赏点零花钱给我。

订阅博主微信公众号:嘟爷java超神学堂(javaLearn)三大好处:

  • 获取最新博主博客更新信息,首发公众号
  • 获取大量视频,电子书,精品破解软件资源
  • 可以跟博主聊天,欢迎程序媛妹妹来撩我

你可能感兴趣的文章
Ubuntu 17.10 +Nginx +Mysql +PHP 环境搭建
查看>>
如何让自己的Asp.Net程序运行在免费的云计算空间OpenShift上面
查看>>
OC基础第一天
查看>>
Git clone远程分支
查看>>
PHP5.3.20配置后发生未知 FastCGI 错误 错误代码 0x800736b1解决办法
查看>>
SELECT可输入可选的实现方法
查看>>
隐藏列tableoid
查看>>
Spring Boot with MySQL
查看>>
互联网时代: 从Uber的供需匹配看开发需求
查看>>
Python中字符串和datetime
查看>>
ng-Cordova插件之fileTransfer的使用
查看>>
基于struts1.框架的异常处理方案
查看>>
【Qt】Qt之密码框不可选中、复制、粘贴、无右键菜单等【转】
查看>>
/usr/lib目录属性更改引发的蝴蝶效应
查看>>
OpenGL超级宝典笔记——颜色
查看>>
shell 命令学习
查看>>
DataURL与File,Blob,canvas对象之间的互相转换的Javascript
查看>>
布置阿里云
查看>>
【草稿】.net 中已经引用了dll,但是编译报错找不到类型或者命名空间
查看>>
Sublime Text 3注册码
查看>>