gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:09:30 33 4
gpt4 key购买 nike

通过创建包装过滤器将外部过滤器合并到 servlet Web 应用程序中。

第三方过滤器需要包含在我的应用程序中,并且需要根据某些逻辑动态调用/绕过。我已经为 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)
}
}
}

我面临的问题是从包装器实例化 ThridPartyFilter。

实例化此过滤器的正确方法是什么?

最佳答案

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..

FilterConfig只是让您访问您在 <filter> 中定义的配置设置web.xml 中的部分对于该过滤器,以便您可以在容器初始化该过滤器后立即访问这些设置。

但现在您正在以编程方式实例化 ThirdPartyFilter这意味着您可以完全控制如何创建它。如果您想外部化 ThirdPartyFilter 的配置,这只是一个问题至MyWrapperFilter<filter> web.xml 中的部分。如果没有,您始终可以通过 ThirdPartyFilter 的构造函数传递配置参数。创建它时。

所以,只要您初始化ThirdPartyFilter,我就看不到您的代码有任何问题。正确。只是您是否要将其配置(如果有)外部化为 web.xml 的问题

关于java - 在另一个过滤器中调用 servlet 过滤器包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57702858/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com