简体   繁体   English

在另一个过滤器中调用servlet过滤器包装器

[英]Calling a servlet filter wrapper in another filter

Incorporate an external filter in the servlet web app by creating a wrapper filter. 通过创建包装过滤器,将外部过滤器并入servlet Web应用程序。

A third party filter needs to be included in my application and needs to be invoked /bypassed dynamically depending on some logic. 第三方过滤器需要包含在我的应用程序中,并且需要根据某些逻辑动态地调用/绕过。 I have created a wrapper filter for the ThirdpartyFilter , but not sure how to invoke this from the wrapper class 我已经为ThirdpartyFilter创建了包装过滤器,但是不确定如何从包装器类调用此过滤器

final class ThirdPartyFilter implements Filter{

@Override
public void init(@SuppressWarnings("hiding") FilterConfig filterConfig) throws ServletException {
   this.filterConfig = filterConfig;
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException  {
    // Some validations here and then call the next filter in the  chain
    }
}

class MyWrapperFilter implements Filter
{

    private ThirdPartyFilter thirdPartyFilter 

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        thirdPartyFilter  = new ThirdPartyFilter(); 
        // Not sure if this is the right way to instantiate the filter
    }

   @Override
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException  
   {
       if(thirdPartyfilterFlag == true) {
          thirdPartyFilter.doFilter(request, response)
       }
       else
       {
           filterChain.doFilter(request, response)
       }
   }
}

The issue I am facing is instantiating the ThridPartyFilter from the wrapper. 我面临的问题是从包装器实例化ThridPartyFilter。

What's the right way to instantiate this filter ? 实例化此过滤器的正确方法是什么?

I'm not sure if it's the right way to instantiate the ThirdPartyFilter, as the filter configs are usually taken care by the container.. 我不确定这是否是实例化ThirdPartyFilter的正确方法,因为通常容器会注意过滤器的配置。

FilterConfig just gives you to access the configuration setting you defined in the <filter> section in web.xml for that filter such that you can access these setting right after the container initialises that Filter. FilterConfig仅使您可以访问在web.xml<filter>部分中为该过滤器定义的配置设置,以便您可以在容器初始化该Filter之后立即访问这些设置。

But now you are programatically instantiating ThirdPartyFilter which means you are in total control of how to create it. 但是现在您正在以编程方式实例化ThirdPartyFilter ,这意味着您完全可以控制如何创建它。 It is just a matter if you want to externalise the configuration of ThirdPartyFilter to MyWrapperFilter 's <filter> section in web.xml . 如果要外化的配置,是早晚的事情ThirdPartyFilterMyWrapperFilter<filter>中部分web.xml If not , you can always pass the configuration parameter through the constructor of ThirdPartyFilter when creating it. 如果不是,则在创建配置参数时始终可以将其传递给ThirdPartyFilter的构造函数。

So I don't see any problems of your codes provided that you initialises ThirdPartyFilter correctly. 因此,如果您正确初始化ThirdPartyFilter ,我看不到您的代码有任何问题。 Just a matter of whether you want to externalise its configuration (if it has) to web.xml 只需确定是否要将其配置(如果有)外部化到web.xml

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

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