zuul:解决com.netflix.zuul.FilterProcessor : null

报错:com.netflix.zuul.FilterProcessor : null

ZuulFilter过滤器的filterType方法返回一个字符串代表过滤器的类型,

如果 return null 就报上述错误. 在Zuul中定义了四种不同生命周期的过滤器类型, 具体如下:

(1) pre —> 路由之前

(2) routing —> 路由之时

(3) post —> 路由之后

(4) error —> 发送错误调

如下:

@Component
public class FilterTest extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        System.out.println(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
        Object accessToken = request.getParameter("token");
        if(accessToken == null) {
            System.out.println("token is empty");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            try {
                ctx.getResponse().getWriter().write("token is empty");
            }catch (Exception e){}

            return null;
        }
        System.out.println("ok");
        return null;
    }
}

你可能感兴趣的:(Spring,Cloud)