2017-08-07 16 views
0

身份驗證方法已與API中的每個REST調用集成在一起。我一直試圖通過Spring AOP實現一種認證方法,這樣我就可以從端點刪除所有重複的代碼,並且有一個單獨的建議來查找控制器中的所有公共方法。如何在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 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) { 
+0

除了你試圖重塑[Spring Security](https://projects.spring.io/spring-security/)這個事實之外,你的問題對我沒有意義。 IntelliJ不能在切入點定義中給出任何錯誤,因爲字符串是一個字符串,它是一個字符串 - 它不是可編譯的代碼。也許你需要添加[servlet-api](https://search.maven.org/remotecontent?filepath=javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar )到你的類路徑來解析'HttpServletRequest'。 –

+0

servelet-api已經添加了,我一直在控制器中使用。如果我刪除了參數,IDE將刪除該消息。現在它給這個意見建議沒有方法 – newday

+0

然後解釋聲明_I看到Intelij給getCategory(HttpServletRequest)附近的錯誤&&參數(請求)說無法解析符號HttpServletRequest_併發布實際的錯誤。否則,請將其刪除。顯示一個控制器方法(連接點)。 –

回答

0

以下語法,解決了上述問題。基本上,我不得不修改代碼來處理建議中的多個參數。

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