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) {
			...
		}
	}
相关推荐
mldong6 小时前
工作流引擎的灵魂:状态机与 submitType
后端
朦胧之9 小时前
Go 基础
后端·go
lun9 小时前
Agent 工具调用成长史:理清楚 Function Calling、MCP、CLI 的关系
后端
Python私教9 小时前
Codex 自动写 Commit 够安全吗?一套证据化 Git 提交流程
人工智能·git·后端
Python私教9 小时前
AI Agent 上生产要不要开写权限?我把执行链拆成 4 道闸门
人工智能·后端·python
lun9 小时前
Claude Code 多 Agent 实现:subAgent & Agent Teams
后端
SomeB1oody11 小时前
【RustyML入门】2.9. MeanShift
开发语言·后端·机器学习·rust·教程
weixin_BYSJ198712 小时前
springboot酒店管理系统--附源码27436
java·spring boot·python·随机森林·贪心算法·eclipse·django
2401_8949155312 小时前
GEO 源码部署如何实现精准地域分发?核心配置参数深度讲解
java·运维·服务器·后端·开源
IT_陈寒12 小时前
为什么我的Java Stream流操作会吃掉内存?
前端·人工智能·后端