在典型的 Spring Boot Web 应用中,我们通常将系统划分为多个逻辑层:Controller(Web 层)、Service(业务层)、Repository(数据访问层)等。其中,Controller 属于 Web 层 ,可以直接访问 HttpServletRequest、HttpSession 等 Servlet API;而 Service、AOP 切面等属于非 Web 层,按理不应直接依赖 HTTP 上下文。
然而,在某些实际场景中(例如日志记录、权限校验、操作审计等),我们可能需要在 AOP 切面或 Service 方法中获取当前用户的 Session 信息。本文将详细介绍如何安全、可靠地在非 Web 层中获取 Session,并探讨其适用边界与最佳实践。
一、为什么非 Web 层默认无法获取 Session?
Spring 的设计哲学强调"关注点分离"和"无状态服务"。
- Service 层应保持与协议无关,以便支持多种调用方式(如 REST API、定时任务、消息队列消费者、RPC 调用等)。
HttpSession是 Servlet 规范的一部分,仅在处理 HTTP 请求的线程中存在。
因此,在非 Web 线程(如 @Scheduled 定时任务、异步方法、MQ 消费者)中,HttpSession 根本不存在。
二、解决方案:通过 RequestContextHolder 间接获取
尽管非 Web 层不直接持有 Session,但 Spring 提供了 RequestContextHolder 工具类,它利用 ThreadLocal 将当前请求的上下文绑定到处理线程上。只要你的方法调用链起源于一个 HTTP 请求(如 Controller → Service),就可以通过它获取 Session。
✅ 步骤详解
1. 确保项目是 Web 应用
你的 Spring Boot 项目必须引入 spring-boot-starter-web,这样才能启用请求上下文绑定。
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2. 在 AOP 切面中使用 RequestContextHolder
java
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpSession;
@Aspect
@Component
public class SessionAwareAspect {
@Around("execution(* com.example.service..*(..))")
public Object captureSessionInfo(ProceedingJoinPoint joinPoint) throws Throwable {
// 1. 获取当前线程绑定的请求上下文
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
String userId = null;
if (requestAttributes instanceof ServletRequestAttributes) {
// 2. 转换为 Servlet 请求属性
ServletRequestAttributes servletAttrs = (ServletRequestAttributes) requestAttributes;
// 3. 获取 HttpSession(false 表示不自动创建)
HttpSession session = servletAttrs.getRequest().getSession(false);
if (session != null) {
userId = (String) session.getAttribute("userId");
System.out.println("【AOP】检测到用户: " + userId);
} else {
System.out.println("【AOP】当前无有效 Session");
}
} else {
System.out.println("【AOP】当前不在 Web 请求上下文中");
}
// 4. 继续执行原方法
try {
return joinPoint.proceed();
} finally {
// 可选:清理资源(通常不需要)
}
}
}
三、关键注意事项
⚠️ 1. 仅适用于 Web 请求线程
该方法仅在由 HTTP 请求触发的调用链中有效。以下场景将无法获取 Session:
@Scheduled定时任务@Async异步方法(除非手动传递上下文)- 消息队列监听器(如 RabbitMQ、Kafka)
- 单元测试(未模拟 Web 环境)
⚠️ 2. 异步场景需特殊处理
如果你在异步方法中需要 Session,必须手动传递或使用 DelegatingSecurityContextAsyncTaskExecutor(结合 Spring Security)等方式传播上下文。
核心原则 :非 Web 层应尽量保持"无状态"和"协议无关"。只有在明确知道当前处于 Web 请求上下文,且没有更好替代方案时,才考虑通过
RequestContextHolder获取 Session。