- 本文介绍spring boot拦截器
- 创建拦截器类LogInterceptor.java
- 创建拦截器类OldLoginInterceptor.java
- 拦截器配置类WebMvcConfig.java
- 路由InterceptorController.java
本文介绍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";
}
}
// 运行即可
原文地址:https://www.cnblogs.com/ye-hcj/p/9614487.html
时间: 2024-10-08 05:57:05