springboot成神之——拦截器
2021-07-04 05:03
标签:handle dmi import servlet exclude body registry admin attribute springboot成神之——拦截器 标签:handle dmi import servlet exclude body registry admin attribute 原文地址:https://www.cnblogs.com/ye-hcj/p/9614487.html
本文介绍spring boot拦截器
创建拦截器类LogInterceptor.java
package com.springlearn.learn.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class LogInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
long startTime = System.currentTimeMillis();
System.out.println("拦截前"+"Start Time: " + startTime);
request.setAttribute("startTime", startTime);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,Object handler, ModelAndView modelAndView) throws Exception{
System.out.println("拦截中"+"Request URL: " + request.getRequestURL());
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("拦截后"+(System.currentTimeMillis()-(Long) request.getAttribute("startTime"))+"");
}
}
创建拦截器类OldLoginInterceptor.java
package com.springlearn.learn.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class OldLoginInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截前"+"重定向到 /admin/login");
response.sendRedirect(request.getContextPath() + "/admin/login");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("拦截中");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("拦截后");
}
}
拦截器配置类WebMvcConfig.java
package com.springlearn.learn.config;
import com.springlearn.learn.interceptor.LogInterceptor;
import com.springlearn.learn.interceptor.OldLoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LogInterceptor());
registry.addInterceptor(new LogInterceptor()).addPathPatterns("/admin/*").excludePathPatterns("/admin/oldLogin");
registry.addInterceptor(new OldLoginInterceptor()).addPathPatterns("/admin/oldLogin");
}
}
路由InterceptorController.java
package com.springlearn.learn.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class InterceptorController {
@ResponseBody
@RequestMapping(value = { "/", "/test" })
public String test(Model model) {
return "test";
}
@ResponseBody
@Deprecated
@RequestMapping(value = { "/admin/oldLogin" })
public String oldLogin(Model model) {
return "oldLogin";
}
@ResponseBody
@RequestMapping(value = { "/admin/login" })
public String login(Model model) {
return "login";
}
}
// 运行即可
下一篇:数组的扩容问题
文章标题:springboot成神之——拦截器
文章链接:http://soscw.com/index.php/essay/101577.html