Gateway filter batch release url specified by properties -- white list

Posted by gijs25 on Fri, 03 Apr 2020 07:00:38 +0200

White list

2.4.1 what is a white list

  • Problem: all requests are blocked and do not meet the actual requirements. (some requests do not need to be intercepted)
  • White list: used to store the request path that does not need to be blocked

2.4.3 implementation

  • Step 1: modify the gateway yml file, and configure the path that does not need to be blocked (white list)
  • Step 2: create a configuration class to store the user-defined configuration information in yml
  • Step 3: improve the gateway filter, modify the third method, release in the white list, not intercept.
  • Step 1: modify = = gateway = = yml file, configure the path that does not need to be blocked (white list)

    • There may be multiple paths, you need to configure the collection in yml

      #Configure one
      a:
        b:
          c: abc
      #Configure a set of
      a:
        b:
          c: 
            - abc
            - aaa
            - bbb
      
      #Custom data
      sc:
        jwt:
          secret: sc@Login(Auth}*^31)&czxy% # Key for login verification
          pubKeyPath: D:/rsa/rsa.pub # Public key address
          priKeyPath: D:/rsa/rsa.pri # Private key address
          expire: 360 # Expiration time in minutes
        filter:
          allowPaths:
            - /checkusername
            - /checkmobile
            - /sms
            - /register
            - /login
            - /verifycode
            - /categorys
            - /news
            - /brands
            - /specifications
            - /search
            - /goods
            - /comments
      
  • Step 2: create a configuration class to store the user-defined configuration information in yml

package com.czxy.changgou3.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by liangtong.
 */
[@Component](https://my.oschina.net/u/3907912) / / submitted to the spring container
@ConfigurationProperties(prefix = "sc.filter")
[@Data](https://My. Oschina. Net / difrik) / / getter and setter
public class FilterProperties {

    private List<String> allowPaths;
}

//Write implementation:
~~~java
 [@Resource](https://my.oschina.net/u/929718)
    private FilterProperties filterProperties;

    @Override
    public boolean shouldFilter() {     //Whether to filter or not. The return value of true means to execute run(); the return value of false means not to execute run()
        //If the current request path matches the "white list" path, return false, otherwise return true
        //1 GET request
        // 1.1 get context object
        RequestContext currentContext = RequestContext.getCurrentContext();
        // 1.2 get request object
        HttpServletRequest request = currentContext.getRequest();
        // 1.3 access path
        // URI: uniform resource identifier (partial path), / v3/cgwebservice/user/checkusername
        //System.out.println(request.getRequestURI());
        // URL: uniform resource locator (full path), http://localhost:10010/v3/cgwebservice/user/checkusername
        //System.out.println(request.getRequestURL());
        String requestURI = request.getRequestURI();

        //2 get white list path
        List<String> allowPaths = filterProperties.getAllowPaths();

        //3. Path matching. In the request path, [include] configuration path
        for(String path : allowPaths) {
            if( requestURI.contains( path)) {
                return false;
            }
        }

        //Other path, run() must be executed
        return true;
    }

Topics: Programming Java Lombok Spring