当前位置: 首页 > news >正文

springboot如何获取前端请求头的值并加入ThreadLocal

依赖:

<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.7</version>
</dependency>

示例:

public class ThreadLocalUtil {private static ThreadLocal<String> aaHeaderValueThreadLocal = new ThreadLocal<>();public static void setAAHeaderValue(String aaHeaderValue) {aaHeaderValueThreadLocal.set(aaHeaderValue);}public static String getAAHeaderValue() {return aaHeaderValueThreadLocal.get();}public static void clear() {aaHeaderValueThreadLocal.remove();}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;public class HttpServletRequestUtil {public static String getAAHeaderValue() {HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();return request.getHeader("aaHeader");}
}
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect
@Component
public class RequireXXAspect {@Pointcut("@annotation(com.mySpringSecurity.mySpringSecurity.aspect.MyAnnotation)")public void myAnnotationMethod() {}@Before("myAnnotationMethod() && @annotation(requireXX)")public void beforeMethodWithRequireXX(JoinPoint joinPoint, MyAnnotation requireXX) {// 获取aaHeader头的值,这里假设通过ServletRequest获取String aaHeaderValue = HttpServletRequestUtil.getAAHeaderValue();// 将aaHeaderValue存放到ThreadLocal中ThreadLocalUtil.setAAHeaderValue(aaHeaderValue);}
}
http://www.lryc.cn/news/215844.html

相关文章:

  • 程序员想要网上接单却看花了眼?那这几个平台你可得收藏好了!
  • 前端食堂技术周刊第 102 期:Next.js 14、Yarn 4.0、State of HTML、SEO 从 0 到 1
  • GPT与人类共生:解析AI助手的兴起
  • HTML脚本、字符实体、URL
  • UOS安装Jenkins
  • 纯CSS实现卡片上绘制透明圆孔
  • 用前端框架Bootstrap的AdminLTE模板和Django实现后台首页的页面
  • Linux驱动 编译乱序和执行乱序
  • 京东大数据平台(京东数据分析):9月京东牛奶乳品排行榜
  • Hadoop RPC简介
  • 你没有见过的 git log 风格
  • 轻松搭建个人邮件服务器:实现远程发送邮件的hMailServer配置
  • 刷题笔记day08-字符串01
  • Pure-Pursuit 跟踪双移线 Gazebo 仿真
  • Selenium学习(Java + Edge)
  • 项目管理-组织战略类型和层次讲解
  • 面试算法50:向下的路径节点值之和
  • dbeaver查看表,解决证书报错current license is non-compliant for [jdbc]
  • 网络安全进阶学习第二十一课——XXE
  • 如何将 ruby 打包类似于jdk在另一台相同架构的机器上面开箱即用
  • vue封装独立组件:实现分格密码输入框/验证码输入框
  • 从2D圆形到3D椭圆
  • Linux CentOS7.9安装OpenJDK17
  • 计算机网络第4章-网络层(1)
  • 单元测试学习
  • python编写接口测试文档(以豆瓣搜索为例)
  • C++查看Class类结构
  • appium如何连接多台设备
  • VUE el-form组件不绑定model时进行校验
  • 计算机视觉的监督学习与无监督学习