The filter implementation SpringCloud Zuul actual login authentication (XI)

Custom zuul filter implementation login authentication combat

1, the new filter bag 2, the new class inherits ZuulFilter, rewriting method 3, the top of the class so that spring scanned annotations added @Comment

. 1  / ** 
2  * @author the WGR
 . 3  * @Create 2019/10/20 - 21:19
 . 4   * / 
. 5  / ** 
. 6  * Log filter
 . 7   * / 
. 8  @Component
 . 9  public  class LoginFilter   the extends ZuulFilter {
 10  11 / ** 12      * type filter, pre-filter
 13 is      * @return 14 * / 15     @Override
 16 public String filterType () {
 . 17 return "pre" ;
 18 is     }
     
  
      
                . 19  20 is / ** 21      * filter order, the smaller the first implementation
 22 is      * @return 23 is * / 24     @Override
 25 public int filterOrder () {
 26 is 27 return . 4 ;
 28     }
 29 30 31 is / ** 32      * is in effect the filter
 33 is      * @return 34 is * / 35     @Override
 36 public Boolean shouldFilter () {
 37 [ 38 is     
  
      
                         
  
      
                 RequestContext requestContext = RequestContext.getCurrentContext();
39         HttpServletRequest  request = requestContext.getRequest();
40 41         //System.out.println(request.getRequestURI()); ///apigateway/product/api/v1/product/list
42         //System.out.println(request.getRequestURL()); //http://localhost:9000/apigateway/product/api/v1/product/list
43 44         //ACL
45 46         if ("/apigateway/api/v1/order/save".equalsIgnoreCase(request.getRequestURI())){
47             return true;
48         }else if ("/apigateway/order/api/v1/order/list".equalsIgnoreCase(request.getRequestURI())){
49             return true;
50         }else if ("/apigateway/order/api/v1/order/find".equalsIgnoreCase(request.getRequestURI())){
51             return true;
52         }
53 54         return false;
55     }
56 57     /**
58      * 业务逻辑
59      * @return
60      * @throws ZuulException
61      */
62     @Override
63     public Object run() throws ZuulException {
64 65         //JWT
66         RequestContext requestContext =  RequestContext.getCurrentContext();
67         HttpServletRequest request = requestContext.getRequest();
68 69         //token对象
70         String token = request.getHeader("token");
71 72         if(StringUtils.isBlank ((token))) {
 73 is              token request.getParameter = ( "token" );
 74          }
 75  76 77 // login validation logic custom case according to the company the JWT 78 IF (StringUtils.isBlank (token )) {
 79              requestContext.setSendZuulResponse ( to false );
 80             requestContext.setResponseStatusCode (HttpStatus.UNAUTHORIZED.value ());
 81         }
 82 83 return null ;
 84     }
 85 86 87          
                          88 }

 

test:

  • Without token

  • 带 token

Guess you like

Origin www.cnblogs.com/dalianpai/p/11710142.html