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) {
			...
		}
	}
相关推荐
why1513 小时前
微服务商城-商品微服务
数据库·后端·golang
結城5 小时前
mybatisX的使用,简化springboot的开发,不用再写entity、mapper以及service了!
java·spring boot·后端
Bruk.Liu5 小时前
《Minio 分片上传实现(基于Spring Boot)》
前端·spring boot·minio
星辰离彬5 小时前
Java 与 MySQL 性能优化:MySQL 慢 SQL 诊断与分析方法详解
java·spring boot·后端·sql·mysql·性能优化
q_19132846955 小时前
基于Springboot+Vue的办公管理系统
java·vue.js·spring boot·后端·intellij idea
陪我一起学编程6 小时前
关于nvm与node.js
vue.js·后端·npm·node.js
舒一笑7 小时前
基于KubeSphere平台快速搭建单节点向量数据库Milvus
后端
JavaBuild7 小时前
时隔半年,拾笔分享:来自一个大龄程序员的迷茫自问
后端·程序员·创业
周全全7 小时前
基于 Vue 和 Spring Boot 实现滑块验证码的机器验证
前端·vue.js·spring boot
一只叫煤球的猫8 小时前
虚拟线程生产事故复盘:警惕高性能背后的陷阱
java·后端·性能优化