【发布时间】:2017-08-07 06:12:00
【问题描述】:
身份验证方法已与 API 中的每个 REST 调用集成。我一直在尝试通过 Spring AOP 实现一种身份验证方法,以便我可以从端点中删除所有重复的代码,并有一个建议来查找 Controller 中的所有公共方法。
请检查下面我的代码,
@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
任何连接点(方法仅在 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) {
【问题讨论】:
-
除了你试图重塑Spring Security,你的问题对我来说没有意义。 IntelliJ 不能在切入点定义中给出任何错误,因为字符串是字符串是字符串 - 它不是可编译的代码。也许您需要将servlet-api 添加到您的类路径中以解析
HttpServletRequest。 -
servelet-api 已经添加,我一直在控制器中使用。如果我删除了参数,那么 IDE 会删除该消息。现在它给出这个建议不建议任何方法
-
然后解释语句 我看到 Intelij 在 getCategory(HttpServletRequest)) && args(request) 附近给出错误说无法解析符号 HttpServletRequest 并发布实际错误。否则,将其删除。显示控制器方法(连接点)。
-
@Abhijit Sarkar 当我将参数减少为单个参数时它可以工作。现在我需要弄清楚如何使用多个参数来做到这一点
标签: java spring spring-aop