2017-08-07 27 views
0

認証メソッドは、APIのすべてのREST呼び出しと統合されています。私は、SpringのAOPを介して認証メソッドを実装しようとしています。そのため、エンドポイントからすべての重複コードを削除し、Controllers内のすべてのパブリックメソッドを探すアドバイスが1つあります。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は、シンボルのHttpServletRequestを解決できないと言って getCategory(HttpServletRequest)) && args(request)の近くにエラーを与えました、私のコードの下に確認してください。私は、各RESTのエンドポイントを区別するように要求する必要があります。メソッドのHttpServletRequest変数よりも多くの変数がありますが、その変数だけが必要です。

アドバイスに到達していないことがわかった機能をテストすると、コードがコンパイルされています。誰でもこの問題を解決するのに手伝ってもらえますか? は私が Spring doc

は、任意の時点 つのパラメータを取ります(メソッドのみ春AOPで実行)、および実行時に渡された引数がある 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) { 
+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が既に追加されていて、私はコントローラで使用しています。 argsを削除すると、IDEはメッセージを削除します。今すぐこのアドバイスにはメソッドがないとアドバイスします – newday

+0

次に、インテルがgetCategory(HttpServletRequest)の近くでエラーを出すのを見て、&Hのargs(要求)が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); 
} 
関連する問題