Spring云 - Zuul - 自定义过滤器 [英] Spring cloud - Zuul - custom Filters

查看:344
本文介绍了Spring云 - Zuul - 自定义过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring云Netflix文档没有关于现有Zuul过滤器的任何信息。是否有任何其他资源解释现有的zuul过滤器,创建一个新的过滤器指南?另外,我还需要知道在自定义过滤器中应该设置什么优先级,以及是否需要像使用servlet过滤器一样使用ResponseWappers?

解决方案

<要创建一个自定义过滤器,你可以使用ZuulFilter来扩展这个类,并且你将不得不添加@Bean配置。

  public class MyFilter扩展ZuulFilter {
@Override
public String filterType(){
returnpre;
}

@Override
public int filterOrder(){
return 1;


@Override
public boolean shouldFilter(){
return true;


@Override
public Object run(){
return null;






在springbootapplication注释的类上写下这个定义, ();

  @Bean 
public MyFilter myFilter(){
return new MyFilter();



$ b $ p
$ b

有4种类型的过滤器PRE,ROUTING,POST,ERROR解释了您可以在上面的FilterType()方法中定义过滤器类型的目的,也可以选择优先级。



您可以使用RequestContext获取请求和响应。



这些文档可能对获取更多详细信息: -


  1. https://spring.io/guides/gs/routing-and-filtering/

  2. https://github.com/Netflix/zuul/wiki/How-it-Works


The Spring cloud Netflix documentation does not have any information about existing Zuul filters. Are there any other resources which explains existing zuul filters, guides on creating a new filter? Also I need to know what priority should I set in my custom filter and whether I need to to use ResponseWappers like in servlet filters?

解决方案

To create a customfilter you can extend the class with ZuulFilter and you will have to add @Bean configuration.

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

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

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

  @Override
  public Object run() {
    return null;
  }
    }

Write this definition on the class annotated with springbootapplication

@Bean
  public MyFilter myFilter() {
    return new MyFilter();
  }

There are 4 types of filter PRE, ROUTING, POST, ERROR I think name explains the purpose you can define the type of filter in FilterType() method above and can also choose the priority.

You can use RequestContext to get the request and response.

These doc might be helpful to get more details:-

  1. https://spring.io/guides/gs/routing-and-filtering/
  2. https://github.com/Netflix/zuul/wiki/How-it-Works

这篇关于Spring云 - Zuul - 自定义过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆