前言:
好了废话不多说,直接说说拦截器是干嘛的,拦截器是一种aop的体现,可以实现权限等功能,例如常用的认证,也就是所谓的登录。好了直接上代码吧,我这里使用的是springboot2.0.5的版本,众所周知springboot2.x多了很多新特性,刚好拦截器配置这里就做了些许改变,为了避免大家采坑,小编特意写下这篇文章。
1.x和2.x的静态资源访问区别(注意事项)
- 1.x的resources/static目录下的静态资源可以直接访问,并且访问路径上不用带static,当有配置自定义HandlerInterceptor拦截器时,请求静态资源路径不会被拦截。
- 2.x的如果自定义HandlerInterceptor拦截器时访问静态资源就会被同步拦截,这样为了实现session过期而跳转登录页面功能就会受影响.
2,自定义拦截器实现
package com.renrengou.interceptor;
import com.renrengou.common.CookieUtils;
import com.renrengou.mange.modle.User;
import com.renrengou.service.LoginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 彭伟
* @Date 2018/9/27 15:52
*/
public class NeedLoginInterceptor implements HandlerInterceptor {
@Value("${mangeuser.cookiename}")
private String MANGE_TOKEN;
@Autowired
private LoginService loginService;
//方法执行前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println(request.getContextPath());
String token = CookieUtils.getCookieValue(request, MANGE_TOKEN, true);
if (StringUtils.isBlank(token)) {//未登录
response.sendRedirect(request.getContextPath() + "/index.html");
return false;
}
//2,取出用户数据
User user = (User) loginService.findUserByToken(token);
if (user == null) {
//登录过期
response.sendRedirect(request.getContextPath() + "/index.html");
return false;
}
//3,登录成功,重置过期时间
loginService.resetKeyTime(token);
return true;
}
//方法执行后
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
}
//页面渲染前
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
}
}
3,自定义拦截器配置
package com.renrengou.config;
import com.renrengou.interceptor.NeedLoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author 彭伟
* @Date 2018/9/27 15:56
* mvc配置类
*/
@Configuration
public class WebMvcConfg implements WebMvcConfigurer {
/**
* @return 登录验证拦截器
* 自定义登录验证拦截器
*/
@Bean
public NeedLoginInterceptor needLoginInterceptor() {
return new NeedLoginInterceptor();
}
/**
* @param registry 配置静态资源放行
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
/**
* @param registry 添加拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//添加登录处理拦截器,拦截所有请求,登录请求除外
InterceptorRegistration interceptorRegistration = registry.addInterceptor(needLoginInterceptor());
//排除配置
interceptorRegistration.excludePathPatterns("/sys/login.json");
interceptorRegistration.excludePathPatterns("/charts/**");
interceptorRegistration.excludePathPatterns("/css/**");
interceptorRegistration.excludePathPatterns("/easyUi/**");
interceptorRegistration.excludePathPatterns("/flashPlayer/**");
interceptorRegistration.excludePathPatterns("/font/**");
interceptorRegistration.excludePathPatterns("/images/**");
interceptorRegistration.excludePathPatterns("/js/**");
interceptorRegistration.excludePathPatterns("/pages/**");
interceptorRegistration.excludePathPatterns("/plugin/**");
interceptorRegistration.excludePathPatterns("/index.html");
interceptorRegistration.excludePathPatterns("/show.html");
//配置拦截策略
interceptorRegistration.addPathPatterns("/**");
}
}
4,项目目录结构
5,到此,自定义拦截器就完成了