前言
在旧版中,一般继承 WebMvcConfigurerAdapter类,但由于2.0后,前者已经过时,在spring boot2.x中,WebMvcConfigurerAdapter被弃用,虽然继承WebMvcConfigurerAdapter这个类虽然有此便利,但在Spring5.0里面已经弃用了。
官方文档也说了,WebMvcConfigurer接口现在已经有了默认的空白方法,所以在Springboot2.0(Spring5.0)下更好的做法还是implements WebMvcConfigurer。
拦截器
目录
拦截器
自定义拦截器必须实现HandlerInterceptor,定义一个登录拦截器,拦截需要登录的操作,若未登录则重定向至登录界面
package com.cxy.springboot.utils.Interceptor;
import com.cxy.springboot.utils.GlobalConst;
import com.cxy.springboot.utils.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Auther: cxy
* @Date: 2019/1/10
* @Description: 拦截器
*/
public class LoginInterceptor implements HandlerInterceptor {
private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
UserInfo user = (UserInfo)request.getSession().getAttribute(GlobalConst.USER_SESSION_KEY);
logger.info(request.getRequestURI().toString());
if (user == null || user.equals("")) {
response.sendRedirect("/login");
logger.info("请先登录");
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
logger.info("postHandle...");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
logger.info("afterCompletion...");
}
}
- preHandle:在业务处理器处理请求之前被调用。预处理,可以进行编码、安全控制、权限校验等处理;
- postHandle:在业务处理器处理请求执行完成后,生成视图之前执行。后处理(调用了Service并返回ModelAndView,但未进行页面渲染),有机会修改ModelAndView;
- afterCompletion:在DispatcherServlet完全处理完请求后被调用,可用于清理资源等。返回处理(已经渲染了页面);
注册拦截器
新建类 WebConfigurer.java
,addPathPatterns 用来设置拦截路径,excludePathPatterns 用来设置白名单,也就是不需要触发这个拦截器的路径。
package com.cxy.springboot.utils;
import com.cxy.springboot.utils.Interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
/**
* @Auther: cxy
* @Date: 2019/1/10
* @Description: 在web的配置文件中,实例化登陆的拦截器,并添加规则
*/
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/login").excludePathPatterns("/loginSys").excludePathPatterns("/static/**");
}
}
不管哪个版本,addResourceHandler方法是设置访问路径前缀,addResourceLocations方法设置资源路径,如果你想指定外部的目录也很简单,直接addResourceLocations指定即可,代码如下:
registry.addResourceHandler("/static/**").addResourceLocations("file:E:/cxy/");
配置静态资源
在application.properties 或 application.yml指定静态资源拦截,要不然静态资源会被拦截。
#配置静态资源
spring.mvc.static-path-pattern=/static/**
前台静态文件路径配置
<link th:href="@{/static/js/hplus/css/bootstrap.min14ed.css}" rel="stylesheet">