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) {
			...
		}
	}
相关推荐
bcbnb2 分钟前
Fiddler配置方法与使用教程:HTTP/HTTPS抓包分析、代理设置与调试技巧详解(开发者实战指南)
后端
Mos_x2 分钟前
服务器公网IP、私网IP、弹性IP是什么?区别与应
java·后端
JavaArchJourney4 分钟前
分布式锁方案详解
分布式·后端
用户990450177800922 分钟前
程序员只懂技术还远远不够!不懂这点,你可能永远在敲代码
后端·面试
青梅主码25 分钟前
Artificial Analysis 刚刚重磅发布《2025 年第三季度人工智能亮点》报告:中国仅落后美国几个月(附下载)
后端
格格步入31 分钟前
🤔一次 OOM 排查(dump文件分析)
java·后端
nppe636 分钟前
NestJs 从入门到实战项目笔记
前端·后端
蓝-萧38 分钟前
Spring Security安全框架原理与实战
java·后端
SimonKing1 小时前
聊聊Spring里那个不打扰Controller就能统一改响应的“神器”
java·后端·程序员
Moment1 小时前
Soul 发布超强端侧语音模型,没错,就是你想的那个 Soul 😍😍😍
前端·后端·github