如何在春季AOP建议中提取变量值



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

请检查下面的我的代码,

@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 AOP中执行方法(,它需要 单个参数,其中运行时传递的参数为 序列 化

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

控制器端点

@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);
}

最新更新