springboot 用request.setAttribute 传值到控制层

Interceptor

java 复制代码
@Slf4j
@Component
public class XxxxInterceptor implements HandlerInterceptor {

    @Override
    @SneakyThrows
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
			.... 
			throw new DefineException("xxxxx....");
			.... 			

            request.setAttribute("userInfo",userInfo);
        return true;
    }

Aspect

java 复制代码
@Aspect
@Component
@Slf4j
@Order(2)
public class DataLimitAspect {
    @Autowired
    private XxxxService xxxService;
    
    @Pointcut("@annotation(xxx.annotation.DataLimit)")
    public void dataLimitPointcut() {
    }

    @Before("dataLimitPointcut()")
    public void before(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        UserDTO userInfo = (UserDTO)request.getAttribute("userInfo");
        ....
         DataLimit annotation = method.getAnnotation(DataLimit.class);
        if (annotation.idOnly()) {
            request.setAttribute("id",id);
            return;
        }
        ....
        for (Object arg : joinPoint.getArgs()) {
        	if (arg instanceof String){ 
	        	functionAaa((String) arg ,id);
        	}
        }
        ....
   }

	void functionAaa(String str ,Integer id){
        ....	
		xxxService.count()...
	};
}                        

GET请求

java 复制代码
    @DataLimit
    @GetMapping("/xxx")
    @ApiOperation("...")
    public AjaxResult getXxxx(@RequestParam String str , HttpServletRequest request ) {
        Object idObj = request.getAttribute("id");
		if (null!=idObj) {
			...
		}
	}

POST请求

java 复制代码
    @DataLimit
    @PostMapping("/xxx")
    @ApiOperation("...")
    public AjaxResult postXxxx(@RequestPart("file") MultipartFile file, 
								@RequestAttribute("userInfo") UserDTO userInfo , 
								HttpServletRequest request ) {
        Object idObj = request.getAttribute("id");
		if (null!=idObj) {
			...
		}
	}
相关推荐
一只公羊几秒前
在 Ubuntu 26.04 LTS (Root) 下使用 uv 丝滑安装 Microsoft MarkItDown
后端
dd聊技术6 分钟前
一张本地消息表装两个业务:差异一点没进表里
后端
她的男孩8 分钟前
头像换了三次还是旧图:秒传 + 预签名 URL + 私有文件权限,四层缓存叠出一个 Bug
java·后端·架构
深入云栈9 分钟前
Netty 4.2.x 源码深度解析 (十五):io_uring 传输 —— RingBuffer 与零拷贝 IO
java·后端
wei_shuo9 分钟前
KES 数据保护体系构建:备份策略设计、恢复流程优化与时间点恢复实践
后端
MacroZheng12 分钟前
装上这款全能增强插件,DeepSeek Harness瞬间高大上了!
java·人工智能·后端
imDwAaY12 分钟前
6篇文章讲清楚Git:Git 实用技巧:暂存现场、挑选提交与定位 Bug (5/6)
git·后端
小卿噢13 分钟前
那个时间不存在——时区代码里六个会真的出事的坑
后端
小园子的小菜14 分钟前
深度图解 Python 进程、线程与 GIL:彻底吃透并发核心原理
后端
用户77139702070616 分钟前
Trae 深度使用指南:从配置到实战,一个 AI 原生 IDE 的完整工作流
后端