如何在 Spring AOP advise 中提取变量值

How to extract a variable value in Spring AOP advise

身份验证方法已集成到 API 中的每个 REST 调用中。我一直在尝试通过 Spring AOP 实现一种身份验证方法,以便我可以从端点删除所有重复代码,并有一个建议在控制器中查找所有 public 方法。

请检查下面我的代码,

@Aspect
public class EndpointAccessAspect {
/**
 * All the request mappings in controllers need to authenticate and validate end-point access
 */

@Before("execution(public * com.xxxx.webapi.controllers.MenuController.getCategory(HttpServletRequest)) && args(request)")
public void checkTokenAccess(HttpServletRequest request){
    String re =(String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
 System.out.println(" %%%%%%%%%%%%%%%%%%% checkTokenAccess %%%%%%%%%%%%%%%" + re);
}

public void checkEndPointPermission(){
    System.out.println(" $$$$$$$$$$$$$$$$$$ checkEndPointPermission &&&&&&&&&&&&&");
}

}

但是,我看到 Intelij 在 getCategory(HttpServletRequest)) && args(request) 附近给出了错误,说无法解析符号 HttpServletRequest。我需要区分每个 REST 端点的请求。方法中的变量多于 HttpServletRequest 变量,但只需要该变量。

代码正在编译,当我测试功能时我注意到它没有达到建议。有人可以帮我解决这个问题吗? 我从 Spring 文档中找到了这个 Spring doc

any join point (method execution only in Spring AOP) which takes a single parameter, and where the argument passed at runtime is Serializable

这是否意味着我不能使用具有多个参数的方法?

控制器端点

@RequestMapping(value = "{menuId}/categories/{categoryId}", method = RequestMethod.GET)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successful retrieval of a category requested", response = ProductGroupModel.class),
            @ApiResponse(code = 500, message = "Internal server error") })
    public ProductGroupModel getCategory(
            @ApiParam(name = "menuId", value = "Numeric value for menuId", required = true) @PathVariable(value = "menuId") final String menuId,
            @ApiParam(name = "categoryId", value = "Numeric value for categoryId", required = true) @PathVariable(value = "categoryId") final String categoryId,
            final HttpServletRequest request) {

下面的语法,解决了上面的问题。基本上,我不得不修改代码来处理建议中的多个参数。

@Before("execution(public * com.xxxx.webapi.controllers.MenuController.getCategory( HttpServletRequest,..)) && args(request, ..)")
public void checkTokenAccess(HttpServletRequest request){
    String re =(String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
 System.out.println(" %%%%%%%%%%%%%%%%%%% checkTokenAccess %%%%%%%%%%%%%%%" + re);
}