Spring Cloud uses Zuui to filter and intercept

Preface

Zuul can realize many advanced functions, such as current limiting, authentication and so on. To achieve these functions, we must be based on the core component "filter" provided to us by zuul. Let's take a look at zuul's filter.

Filter type

1 pre It can be called before the request is routed. It is applicable to the scenario of identity authentication. After the authentication is passed, continue to perform the following process. 2 route Called when a request is routed. It is suitable for gray-scale publishing scenarios. You can make some custom logic when you are about to route. 3 post Called after route and error filters. This filter routes requests to specific services before execution. It is applicable to application scenarios such as adding response headers and recording response logs. 4 error Called when an error occurred while processing the request. When an error is sent during execution, it will enter the error filter, which can be used to record the error information uniformly.

code implementation

We can simulate token. Please verify the correct token. Illegal token is intercepted.

Inherit ZuulFilter

package com.example.zuui.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.ZuulFilterInitializer;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: zuui Filter
 * @author: Administrator
 * @create: 2019-12-02 22:31
 **/
@Component
public class ZuuiFilter extends ZuulFilter {

    /**
     * Filter type, optional values are pre, route, post and error.
     * @return
     */
    @Override
    public String filterType() {
        System.out.println("Prefix request");
        return "pre";
    }

    /**
     * The lower the value, the higher the priority.
     * @return
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * Whether to execute the filter, true means to execute and false means not to execute. This can also be realized by using the configuration center to dynamically turn on and off the filter.
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }


    /**
     * Execute your own business logic. In this code, you decide whether to intercept the requested IP by judging whether it is in the blacklist. blackIpList field is the blacklist of IP. After the judgment conditions are met,
     * By setting ctx.setsendzulresponse (false), Zuul is told that the current request does not need to be forwarded to the back-end service.
     * Return data to the client through setResponseBody.
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {

        RequestContext ctx = RequestContext.getCurrentContext();

        HttpServletRequest request = ctx.getRequest();
        HttpServletResponse response = ctx.getResponse();
        String token = request.getParameter("token");

        if(!"123".equals(token)){
            System.out.println("Illegal request");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            ctx.setResponseBody("Illegal request!");
            ctx.getResponse().setContentType("application/json; charset=utf-8");
            return null;
        }

        System.out.println("Verifying and passing");
        return null;
    }
}

Results show

False interception

Verifying and passing

Code address Portal

Tags: Programming JSON

Posted on Wed, 18 Mar 2020 07:11:00 -0700 by driver