package interceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @description: 全局拦截器
* @author: lyk
* @create: 2021/6/23 14:21
*/
@Slf4j
@Component
public class GlobalInterceptor implements HandlerInterceptor {
// 在业务处理器处理请求之前被调用
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//定义开始时间
long start = System.currentTimeMillis();
//2.将其存到请求域当中
request.setAttribute("start",start);
//记录请求日志
log.info(request.getRequestURI()+",请求到达");
//3,返回true,才会找下一个拦截器,如果没有下一个拦截器,则去Controller
return true;
}
// 在业务处理器处理请求完成之后,生成视图之前执行
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//1,取出start
long start = (long) request.getAttribute("start");
//2,得到end
long end =System.currentTimeMillis();
//3,记录耗时
long spendTime = end-start;
if (spendTime>2000){
log.warn("方法耗时严重,请及时处理,耗时:"+spendTime);
}else {
log.info("方法耗时"+spendTime+"毫秒,正常");
}
}
// 在DispatcherServlet完全处理完请求之后被调用,可用于清理资源
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.info("88888888888888");
}
}
package interceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @description: 用户登陆拦截器
* @author: lyk
* @create: 2021/6/23 16:26
*/
@Slf4j
@Component
public class UserLoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String uri = request.getRequestURI();
/* if(uri.contains("wechat")||uri.contains("api")||uri.contains("wxuser")){
return true;
}*/
Object user = request.getSession().getAttribute("user");
if(!uri.contains("login")){
if(user==null){
// response.sendRedirect("http://xx.xx.xxx.xxx:7001/test/TestDemoServlet?userName=zhangsan");
response.sendRedirect("https://www.baidu.com/");
return false;
}
}else{
if(user!=null){
response.sendRedirect("/main/index");
return false;
}
}
return true;
// HttpSession session = request.getSession(true);
// Object username=session.getAttribute("userguid");
// if(null!=username) {//已登录
// return true;
// }else {//未登录
// //直接重定向到登录页面
// //response.sendRedirect(request.getContextPath()+"/wechatplatformuser/loginRBAC.html");
// return false;
// }
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
package interceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @description: 拦截器配置添加
* @author: lvyk
* @create: 2021/6/23 14:37
*/
@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private GlobalInterceptor globalInterceptor;
@Autowired
private UserLoginInterceptor userLoginInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//这里添加拦截器的顺序就是拦截器执行的顺序
registry.addInterceptor(userLoginInterceptor)
.addPathPatterns("/**")
.addPathPatterns("/api/**")
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");;
registry.addInterceptor(globalInterceptor).addPathPatterns("/api/**");
log.info("添加拦截器成功...");
}
// @Override
// public void addInterceptors(InterceptorRegistry registry) {
// // 添加拦截器,配置拦截地址
// registry.addInterceptor(interceptor).addPathPatterns("/api/**");
// }
}
|