springcloud服务网关 --前置过滤器和后置过滤器(二)

一:zuul:
zuul=路由+过滤器

前置过滤器(pre)
1.限流:流量过大的时候 通过某些规则把流量挡回去 后续逻辑就不处理了
2.鉴权:如果多个服务都需要鉴权的话 那么放在网关是个不错的选择
3.参数校验

后置:

  1. 统计
    二:前置过滤器测试
    从url参数获取token 如果token为null 则提示401错误 带则通过
package com.zhu.apigateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class TokenFilter extends ZuulFilter {

    /*FilterConstants*/
    @Override
    public String filterType() {
      //PRE_TYPE标识为前置过滤器
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
    //PRE_DECORATION_FILTER_ORDER的值越小优先级越高
        return FilterConstants.PRE_DECORATION_FILTER_ORDER-1;
    }

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

    /**
     * 前置逻辑写在run方法内
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
       // 从url参数获取token 如果token为null 则提示401错误 带则通过
         RequestContext requestContext= RequestContext.getCurrentContext();
             HttpServletRequest request= requestContext.getRequest();
             //从url参数获取 或者 cookie或者header里面获取
             String token= request.getParameter("token");
             System.out.println("token:"+token);
             if (StringUtils.isEmpty(token))
             {
                 requestContext.setSendZuulResponse(false);
                 requestContext.setResponseStatusCode(HttpStatus.SC_UNAUTHORIZED);
             }
        return null;
    }
}

三:测试后置过滤器

往返结果中的头加入一个uuid

在这里插入代码片
package com.zhu.apigateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.http.HttpResponse;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Component
public class AddReseponseFileter extends ZuulFilter {
    @Override
    public String filterType() {
        //标识为后置过滤器
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        //优先级在SEND_RESPONSE_FILTER_ORDER之前
        return FilterConstants.SEND_RESPONSE_FILTER_ORDER-1;
    }

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

    @Override
    public Object run() throws ZuulException {
        //往返结果中的头加入一个uuid
        RequestContext requestContext= RequestContext.getCurrentContext();
          HttpServletResponse response= requestContext.getResponse();
          response.setHeader("zhu", UUID.randomUUID().toString());
        return null;
    }
}

你可能感兴趣的:(springcloud)