SpringBoot 过滤器和拦截器(三十八)

news/2024/11/24 7:07:49/

我喜欢你,可是你却并不知道.

上一章简单介绍了SpringBoot参数验证(三十七) ,如果没有看过,请观看上一章

关于过滤器和拦截器已经讲很多了, 这里老蝴蝶只说一下 SpringBoot 的用法。

可以看之前的文章:

https://blog.csdn.net/yjltx1234csdn/article/details/100667082

https://blog.csdn.net/yjltx1234csdn/article/details/105484464

一.过滤器

一.一 过滤器实现 Filter 接口

@Slf4j
public class LogFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {Filter.super.init(filterConfig);}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, 		ServletException {// 主体方法long start = System.currentTimeMillis();chain.doFilter(request, response);log.info("Execute cost= {}" ,(System.currentTimeMillis() - start));}@Overridepublic void destroy() {Filter.super.destroy();}
}

一.二 配置过滤器

@Configuration
public class WebFilterConfig {@Beanpublic FilterRegistrationBean registFilter() {FilterRegistrationBean registration = new FilterRegistrationBean();registration.setFilter(new LogFilter());registration.addUrlPatterns("/*");registration.setName("LogCostFilter");registration.setOrder(1);return registration;}}

一.三 过滤器代码

  @GetMapping("/hello")public String hello() {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}return "你好啊,岳泽霖";}
2023-04-04 17:54:20.769  INFO 29068 --- [nio-8081-exec-4] top.yueshushu.learn.config.LogFilter     : Execute cost= 2012

二. 拦截器

设置一个登录拦截器

用户对象

@Data
public class User {private static final long serialVersionUID = 1L;private Integer id;private String account;private String name;private String token;// ... 其他属性信息
}

二.一 @AuthToken 注解

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthToken {}

二.二 拦截器 AuthorizationInterceptor

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import top.yueshushu.learn.annotation.AuthToken;
import top.yueshushu.learn.model.User;
import top.yueshushu.learn.util.ThreadLocalUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.lang.reflect.Method;@Slf4j
public class AuthorizationInterceptor implements HandlerInterceptor {private String httpHeaderName = "Authorization";public static final String X_REAL_IP = "x-real-ip";public static final String REQUEST_CURRENT_KEY = "REQUEST_CURRENT_KEY";@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {if (!(handler instanceof HandlerMethod)) {return true;}HandlerMethod handlerMethod = (HandlerMethod) handler;Method method = handlerMethod.getMethod();boolean isForAllTimeUser = false;if (method.getAnnotation(AuthToken.class) != null ||handlerMethod.getBeanType().getAnnotation(AuthToken.class) != null) {String token = request.getHeader(httpHeaderName);String ip = request.getHeader(X_REAL_IP);User loginUser = null;if (StringUtils.hasText(token)) {// 根据 token 获取用户 信息// 验证 token 是否 jwt 过期, 过期的话, isFalse}if (!StringUtils.hasText(token) || loginUser == null) {isFalse(request, response);return false;}String userDoAccount = loginUser.getAccount();ThreadLocalUtils.put("userDoAccount", userDoAccount);ThreadLocalUtils.put("ip", ip == null ? request.getRemoteAddr() : ip);ThreadLocalUtils.put("userName", loginUser.getAccount());ThreadLocalUtils.put("user", loginUser);ThreadLocalUtils.put("token", token);ThreadLocalUtils.put("userId", loginUser.getId());// 可以进行续命, 将时间延长.return true;}request.setAttribute(REQUEST_CURRENT_KEY, null);return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {ThreadLocalUtils.release();}private void isFalse(HttpServletRequest httpServletRequest, HttpServletResponse response) {JSONObject jsonObject = new JSONObject();PrintWriter out = null;try {response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);response.setContentType(MediaType.APPLICATION_JSON_VALUE);response.setCharacterEncoding("utf-8");jsonObject.put("code", "10010001");jsonObject.put("message", "登录已过期,请重新登录");//添加其余的两个属性  success和 datajsonObject.put("data", "");jsonObject.put("success", false);out = response.getWriter();out.println(jsonObject);} catch (Exception e) {log.error("发生异常{}", e);} finally {if (out != null) {out.flush();out.close();}}}private void isUnValid(HttpServletRequest httpServletRequest, HttpServletResponse response) {JSONObject jsonObject = new JSONObject();PrintWriter out = null;try {response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);response.setContentType(MediaType.APPLICATION_JSON_VALUE);response.setCharacterEncoding("utf-8");jsonObject.put("code", "10010002");jsonObject.put("message", "您的账号已经在另一处登录了,您被迫下线!");//添加其余的两个属性  success和 data, 解决 PDA端无法翻译 退出的问题。 @zk_yjljsonObject.put("data", "");jsonObject.put("success", false);out = response.getWriter();out.println(jsonObject);} catch (Exception e) {log.error("发生异常", e);} finally {if (out != null) {out.flush();out.close();}}}
}

二.三 线程内工具类

public class ThreadLocalUtils {private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL = new ThreadLocal<>();/*** 存储*/public static void put(String key, Object value) {Map<String, Object> map = THREAD_LOCAL.get();if (map == null) {map = new HashMap<>(1,1);}map.put(key, value);THREAD_LOCAL.set(map);}/*** 取值*/public static <T> T get(String key) {Map<String, Object> map = THREAD_LOCAL.get();if (map != null) {return (T) map.get(key);}return null;}/*** 获取当前用户*/public static User getUser() {Map<String, Object> map = THREAD_LOCAL.get();return (User) map.get("user");}public static void release() {THREAD_LOCAL.remove();}public static String getToken() {return get("token");}
}

二.四 添加拦截器


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.yueshushu.learn.interceptor.AuthorizationInterceptor;/*** @ClassName:MvcConfig* @Description Web端配置* @Author zk_yjl* @Date 2021/6/29 16:31* @Version 1.0* @Since 1.0**/
@Configuration
public class MvcConfig implements WebMvcConfigurer {@Beanpublic HandlerInterceptor getAuthInterceptor() {//返回自定义的拦截类return new AuthorizationInterceptor();}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(getAuthInterceptor())// 拦截所有请求.addPathPatterns("/**")// 静态资源过滤.excludePathPatterns("/usr/login","/static/**","/v2/**", "/swagger-resources/configuration/**","/swagger-resources/**", "/swagger-ui.html#/**", "/webjars/**");}/*** 配置静态的资源信息** @param registry*/@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");}
}

二.五 处理数据

  @GetMapping("/hello2")@AuthTokenpublic String hello2() {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}// 线程内获取User user = ThreadLocalUtils.getUser();return "你好啊,岳泽霖";}

本章节的代码放置在 github 上:

https://github.com/yuejianli/springboot/tree/develop/SpringBoot_Interceptor

谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!


http://www.ppmy.cn/news/37910.html

相关文章

​ 【蓝桥杯】每日四道编程题(两道真题+两道模拟)​| 第6天

专栏&#xff1a; 蓝桥杯——每日四道编程题&#xff08;两道真题两道模拟&#xff09; “蓝桥杯就要开始了&#xff0c;这些题刷到就是赚到” ₍ᐢ..ᐢ₎♡ 另一个专栏&#xff1a; 蓝桥杯——每日四道填空题&#xff08;两道真题两道模拟题&#xff09; 专题前瞻&#xff1a;…

C++面向对象高级编程(下)

C面向对象高级编程(上) C面向对象高级编程(下) 泛型编程&#xff08;Generic Programming&#xff09;(模板) 和面向对象编程&#xff08;Object-Oriented Programming&#xff09;虽然分属不同思维&#xff0c;但它们正是C的技术主流&#xff1b; 深入探索面向对象之继承关…

电影《铃芽之旅》观后感

这周看了电影《铃芽之旅》&#xff0c;整部电影是新海诚的新作。电影讲述的是女主铃芽为了关闭往门&#xff0c;在日本旅行中&#xff0c;遭遇灾难的故事。 &#xff08;1&#xff09;往昔记忆-往昔之物 电影中&#xff0c;有很多的“往门”&#xff0c;换成中国的话说&#xf…

TiDB入门篇-数据物理备份和恢复

简介 快照备份是集群全量备份的一种实现。它基于 TiDB 的多版本并发控制 (MVCC) 实现&#xff0c;将指定快照包含的所有数据备份到目标存储中。备份下来的数据大小约等于集群&#xff08;压缩后的&#xff09;单副本数据大小。备份完成之后&#xff0c;你可以在一个空集群或不…

工厂模式(简单工厂 工厂方法 抽象工厂)

简单工厂模式 简单工厂模式又叫做静态工厂方法模式&#xff08;static Factory Method pattern&#xff09;,它是通过使用静态方法接收不同的参数来返回不同的实例对象&#xff08;这些产品类继承自一个父类或接口&#xff09;。 简单工厂模式是由一个工厂对象决定创建出哪一种…

VR全景餐厅, 全景VR新颖的沉浸式就餐体验

随着科技的不断发展&#xff0c;餐饮业也在寻求各种创新以满足顾客日益增长的需求。VR全景餐厅应运而生&#xff0c;它结合了虚拟现实技术与美食&#xff0c;为顾客提供了一种前所未有的沉浸式就餐体验。本文将深入探讨VR全景餐厅的核心技术、特色菜品和服务&#xff0c;以及它…

JavaScript之DOM操作

目录一、对元素进行操作1.动态生成元素2.在尾部插入一个元素3.在指定位置插入元素4.替换指定元素5.删除子级元素6.删除自身元素7.innerHTML二、对属性进行操作1.属性的设置&#xff08;setAttribute&#xff09;2.属性的获取&#xff08;getAttribute&#xff09;3.属性的删除&…

nginx--开源免费

nginx开源免费&#xff0c;支持高性能&#xff0c;高并发的web服务和代理服务软件。 apache,nodejs nginx可以提供的服务&#xff1a; 1、web服务 2、负载均衡&#xff08;反向代理&#xff09;&#xff08;动静分离&#xff09; 3、web cache(web缓存&#xff09; nginx…