简体   繁体   English

Spring cloud - Zuul - 自定义过滤器

[英]Spring cloud - Zuul - custom Filters

The Spring cloud Netflix documentation does not have any information about existing Zuul filters. Spring云Netflix文档没有关于现有Zuul过滤器的任何信息。 Are there any other resources which explains existing zuul filters, guides on creating a new filter? 是否还有其他资源可以解释现有的zuul过滤器,有关创建新过滤器的指南? 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? 另外我需要知道我应该在自定义过滤器中设置什么优先级以及是否需要在servlet过滤器中使用ResponseWappers?

To create a customfilter you can extend the class with ZuulFilter and you will have to add @Bean configuration. 要创建自定义过滤器,可以使用ZuulFilter扩展类,并且必须添加@Bean配置。

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 在使用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. 有4种类型的过滤器PRE,ROUTING,POST,ERROR我认为名称解释了你可以在上面的FilterType()方法中定义过滤器类型的目的,也可以选择优先级。

You can use RequestContext to get the request and response. 您可以使用RequestContext来获取请求和响应。

These doc might be helpful to get more details:- 这些文档可能有助于获取更多详细信息: -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM