创建 servlet 过滤器 websphere 自由配置文件? [英] Create a servlet-filter websphere liberty profile?

查看:67
本文介绍了创建 servlet 过滤器 websphere 自由配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个 websphere 自由配置文件网络服务器,我已经在其中部署了几个应用程序.这些应用程序正在发送请求消息,我想在不更改应用程序的情况下创建一个 servlet 过滤器所以我可以看到应用程序正在发送和接收什么.我还想添加新的请求标头.

I'm working with an websphere liberty profile webserver where i have deployed a couple of applications. These applications are sending request message's, I want to create a servlet-filter without changing the applications so i can see what the application is sending and receiving. Also i want to add new request headers.

推荐答案

您可以使用 ServletContainerInitializer 来注册新的 ServletFilter.添加响应标头的示例实现可能如下所示:

You can use a ServletContainerInitializer to register new ServletFilters. An example implementation that adds a response header might look like this:

public class SCI implements ServletContainerInitializer {
  @Override
  public void onStartup(Set<Class<?>> arg0, ServletContext arg1)
    throws ServletException {
    arg1.addFilter("myFilter", MyFilter.class).addMappingForUrlPatterns(null, false, "/*");
  }
}

MyFilter 类看起来像这样:

The MyFilter class would look like this:

public static class MyFilter implements Filter {

  @Override
  public void destroy() { }

  @Override
  public void doFilter(ServletRequest arg0, ServletResponse arg1,
      FilterChain arg2) throws IOException, ServletException {
    if (arg1 instanceof HttpServletResponse) {
      ((HttpServletResponse) arg1).addHeader("Test", "Test");
    }
    arg2.doFilter(arg0, arg1);
  }

  @Override
  public void init(FilterConfig arg0) throws ServletException { }
}

然后您需要使用名为 META-INF/services/ServletContainerInitializer 的文件注册它,该文件应包含 Servlet Container Initializer 的完全限定类名,例如:

You then need to register this using a file in called META-INF/services/ServletContainerInitializer which should contain the fully qualified class name of the Servlet Container Initializer, for example:

test.SCI

通常您将这些打包在应用程序的 jar 中,但由于您不想更新应用程序,您可以像这样配置服务器:

Normally you package these in a jar in the application, but since you don't want to update the application you instead configure the server like this:

<featureManager>
   <feature>bells-1.0</feature>
</featureManager>

<library id="init">
  <file name="path/to/jar"/>
</library>

<bell libraryRef="init"/>

将为所有启动的 Web 应用程序调用 ServletContainerInitializer,允许您添加过滤器.请注意,这将针对所有已启动的 Web 应用程序调用,包括集成到 Liberty 运行时的应用程序,例如管理中心和 REST 连接器.

The ServletContainerInitializer will be called for all started Web applications allowing you to add the filter. Note this will be called for all started Web applications including ones integrated into the Liberty runtime, such as the Admin Center and the REST connector.

这篇关于创建 servlet 过滤器 websphere 自由配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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