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 小时前
Nginx 优化与防盗链配置指南
java·后端·spring
皮皮林5518 小时前
SpringBoot 全局/局部双模式 Gzip 压缩实战:14MB GeoJSON 秒变 3MB
java·spring boot
weixin_456904278 小时前
Spring Boot 用户管理系统
java·spring boot·后端
奔跑吧邓邓子8 小时前
【Java实战㉞】从0到1:Spring Boot Web开发与接口设计实战
java·spring boot·实战·web开发·接口设计
茶本无香9 小时前
深入理解Spring Boot的EnvironmentPostProcessor:环境处理的黑科技
spring boot
奔跑吧邓邓子9 小时前
【Java实战㉝】Spring Boot实战:从入门到自动配置的进阶之路
java·spring boot·实战·自动配置
ONLYOFFICE9 小时前
【技术教程】如何将ONLYOFFICE文档集成到使用Spring Boot框架编写的Java Web应用程序中
java·spring boot·编辑器
cyforkk9 小时前
Spring 异常处理器:从混乱到有序,优雅处理所有异常
java·后端·spring·mvc
程序员爱钓鱼10 小时前
Go语言实战案例-开发一个Markdown转HTML工具
前端·后端·go