zuul 网关 filter
给网关做一个拦截
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
/**
* 请求拦截
*
* @author H
* @version $id:LoginFilter.java, v 1.0 2022/4/4 15:51:37 $
*/
@Component
public class RequestFilter extends ZuulFilter {
@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
System.out.println("进入zuul拦截-login拦截");
//获取上下文
RequestContext ctx = RequestContext.getCurrentContext();
//获取Request
HttpServletRequest request = ctx.getRequest();
//获取请求参数
String token = request.getHeader("head_userName");
System.out.println("参数token=" + token);
return null;
}
}
FilterConstants 拦截的类型有几种
String ERROR_TYPE = "error";
String POST_TYPE = "post";
String PRE_TYPE = "pre";
String ROUTE_TYPE = "route";
pre: 在进入请求之前进入这个filter
post: 请求完成之后再进入这个filter