フィルタの実装SpringCloud Zuul実際のログイン認証(XI)

カスタムzuulフィルタ実装のログイン認証戦闘

図1に示すように、新たなフィルタバッグ2、新しいクラスがメソッド書き換え、ZuulFilterを継承スプリングスキャン注釈が@commentを追加するように図3に示すように、クラスのトップを

1  / ** 
2  * @author WGR
 3。 * @Create 2019年10月20日- 21:19
 。4   * / 
5  / ** 
6  *フィルタログ
 7。  * / 
8。 @Component
 9つの。 パブリック クラス LoginFilter   延びZuulFilter {
 10  11 / ** 12      *型フィルタ、プレフィルタ
 13である     * @return 14 * / 15     @Override
 16 パブリック文字列のfilterType(){
 17。復帰 "前" 18です    }
     
  
      
                19  20 / ** 21      *のフィルタ次数、小さい最初の実装
 22は     * @return 23である* / 24     @Override
 25 公衆INT filterOrder(){
 26は、27 リターン 4。;
 28     }
 29 30 31れる/ ** 32      *フィルタ効果である
 33である     * @return 34がある* / 35     @Override
 36 パブリックブール){shouldFilter(
 37 [ 38れます     
  
      
                         
  
      
                 RequestContextのRequestContextの= RequestContext.getCurrentContext()。
39          HttpServletRequestのリクエスト= requestContext.getRequest()。
40  41 // するSystem.out.println(request.getRequestURI())。// / apigateway /製品/ API / V1 /製品/リスト
 42 // するSystem.out.println(request.getRequestURL())。// のhttp:// localhostを:9000 / apigateway /製品/ API / V1 /製品/リスト43 44 // ACL 45 46 もし( "/ apigateway / API / V1 /オーダー/保存" .equalsIgnoreCase(request.getRequestURI ())){
 47                  
          
                       返す ;
48          } そう であれば( "/ apigateway /順序/ API / V1 /順序/リスト" .equalsIgnoreCase(request.getRequestURI())){
 49              リターン 50          } そう であれば( "/ apigateway /順序/ API / V1 /順序/検索" .equalsIgnoreCase(request.getRequestURI())){
 51              リターン 52          }
 53  54 リターン55     }
 56 57 / ** 58      *业务逻辑
 59      *                 
  @return 
60       * @throws ZuulException
 61       * / 
62      @Override
 63      パブリックオブジェクトラン()がスローZuulException {
 64  65 // JWT 66          RequestContextのRequestContextの=   RequestContext.getCurrentContext()。
67          HttpServletRequestのリクエスト= requestContext.getRequest()。
68 69 // トークン对象70          列トークン= request.getHeader( "トークン" )。
71 72 もし         
          
          (StringUtils.isBlank((トークン))){
 73は              トークンrequest.getParameter =( "トークン" );
 74          }
 75  76 77 // ログイン検証ロジックカスタムケース会社JWTに従って78 IF (StringUtils.isBlank(トークン)){
 79              requestContext.setSendZuulResponse(偽の);
 80             requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
 81         }
 82 83 戻りNULL ;
 84     }
 85 86 87          
                          88 }

 

テスト:

  • トークンなし

  • 带token

おすすめ

転載: www.cnblogs.com/dalianpai/p/11710142.html