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) {
			...
		}
	}
相关推荐
武子康1 小时前
Vercel Cache Reason:相同 MISS,怎样决定下一步查什么
人工智能·后端·agent
杨文说AI与数字化1 小时前
把模型依赖做成可替换的组件:三层适配 + 一套回归评估
后端
IT_陈寒1 小时前
Vue的这个响应式陷阱我居然现在才发现
前端·人工智能·后端
WL学习笔记2 小时前
使用string类
c++·后端·string类
飞鸟真人2 小时前
IM网关系列(九)单账号流量统计:滑动窗口桶方案设计与Go实现
后端·golang
武子康3 小时前
Embedding 替换怎么评测:把索引成本算进成功任务
人工智能·后端·agent
9527!到!5 小时前
Springboot @value读取配置参数踩坑&推荐解决方案
java·spring boot·spring
大哥43095 小时前
on-failure 不扛重启:一次 Docker restart 策略的反直觉排查
后端
十年Java程序媛5 小时前
Java 面试常问 this、super:构造方法调用顺序避坑
java·开发语言·spring boot·面试·个人开发
房西杭5 小时前
GM指令:仓库扩容、清空仓库(行会仓库 + 个人仓库,Lua + proto + GMMgr注册)
后端