过滤链循环问题 - 超过100个线程在后面工作?

时间:2014-05-26 09:22:50

标签: java java-ee servlet-filters jcifs

我的过滤器有一个奇怪的情况。我想捕获从JCIFS Ntlm Http过滤器抛出的异常。过滤器链从我的自定义过滤器开始,我已经使用try-catch块包围了chain.doFilter()部分,以便在发生任何异常时能够重定向响应。这是web.xml的代码:

    <filter>
        <filter-name>NTLM_FILTER_CHAIN_START_POINT</filter-name>
        <filter-class>filters.JcifsNtlmWrapperFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>NTLM_FILTER_CHAIN_START_POINT</filter-name>
        <url-pattern>/ntlm/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>NTLM_HTTP_AUTHENTICATION_FILTER</filter-name>
        <filter-class>filters.JcifsNtlmFilterImpl</filter-class>
        <!-- init params goes here -->
    </filter>
    <filter-mapping>
        <filter-name>NTLM_HTTP_AUTHENTICATION_FILTER</filter-name>
        <url-pattern>/ntlm/*</url-pattern>
    </filter-mapping>

JcifsNtlmWrapperFilter.java:

public class JcifsNtlmWrapperFilter implements Filter {

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

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession(true);

        try{
            filterChain.doFilter(servletRequest, servletResponse);
        }catch (Exception e){
            System.out.println("--------------- Catching Exception ------------");
            session.setAttribute("error", "NTLMFailedToAuthenticate");
            response.sendRedirect("/");
        }
    }

    @Override
    public void destroy() {}
}

在JcifsNtlmFilterImpl.java中,我只是抛出异常(仅用于测试):

public void doFilter( ServletRequest request,
                          ServletResponse response,
                          FilterChain chain ) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
        NtlmPasswordAuthentication ntlm;

        System.out.println("--------------- NTLM Filtering ------------");
        if(1==1){
            System.out.println("--------------- Throwing Exception ------------");
            throw new IOException();
        }


        if ((ntlm = negotiate( req, resp, false )) == null) {
            return;
        }

        chain.doFilter( new NtlmHttpServletRequest( req, ntlm ), response );
    }

一切都按预期工作。用户没有看到NTLM过滤器内部抛出的任何错误消息(但是看到了明确的auth登录页面),但是当我查看glassfish日志时,我看到超过100个Threads正在经历这个过滤周期(来自日志的一个小的复制粘贴) ):

[#|2014-05-26T14:16:02.892+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=109;_ThreadName=Thread-2;|USER IP:127.0.0.1|#]

[#|2014-05-26T14:16:02.909+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=108;_ThreadName=Thread-2;|--------------- NTLM Filtering ------------|#]

[#|2014-05-26T14:16:02.910+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=108;_ThreadName=Thread-2;|--------------- Throwing Exception ------------|#]

[#|2014-05-26T14:16:02.910+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=108;_ThreadName=Thread-2;|--------------- Catching Exception ------------|#]

[#|2014-05-26T14:16:02.927+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=110;_ThreadName=Thread-2;|USER IP:127.0.0.1|#]

[#|2014-05-26T14:16:02.942+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=111;_ThreadName=Thread-2;|--------------- NTLM Filtering ------------|#]

[#|2014-05-26T14:16:02.942+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=111;_ThreadName=Thread-2;|--------------- Throwing Exception ------------|#]

[#|2014-05-26T14:16:02.942+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=111;_ThreadName=Thread-2;|--------------- Catching Exception ------------|#]

[#|2014-05-26T14:16:02.959+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=107;_ThreadName=Thread-2;|USER IP:127.0.0.1|#]

[#|2014-05-26T14:16:02.978+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=109;_ThreadName=Thread-2;|--------------- NTLM Filtering ------------|#]

[#|2014-05-26T14:16:02.978+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=109;_ThreadName=Thread-2;|--------------- Throwing Exception ------------|#]

[#|2014-05-26T14:16:02.978+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=109;_ThreadName=Thread-2;|--------------- Catching Exception ------------|#]

[#|2014-05-26T14:16:03.004+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=108;_ThreadName=Thread-2;|USER IP:127.0.0.1|#]

[#|2014-05-26T14:16:03.019+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=110;_ThreadName=Thread-2;|--------------- NTLM Filtering ------------|#]

[#|2014-05-26T14:16:03.019+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=110;_ThreadName=Thread-2;|--------------- Throwing Exception ------------|#]

[#|2014-05-26T14:16:03.019+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=110;_ThreadName=Thread-2;|--------------- Catching Exception ------------|#]

[#|2014-05-26T14:16:03.034+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=111;_ThreadName=Thread-2;|USER IP:127.0.0.1|#]

[#|2014-05-26T14:16:03.051+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=107;_ThreadName=Thread-2;|--------------- NTLM Filtering ------------|#]

[#|2014-05-26T14:16:03.051+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=107;_ThreadName=Thread-2;|--------------- Throwing Exception ------------|#]

[#|2014-05-26T14:16:03.052+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=107;_ThreadName=Thread-2;|--------------- Catching Exception ------------|#]

[#|2014-05-26T14:16:03.067+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=109;_ThreadName=Thread-2;|USER IP:127.0.0.1|#]

[#|2014-05-26T14:16:03.081+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=108;_ThreadName=Thread-2;|--------------- NTLM Filtering ------------|#]

[#|2014-05-26T14:16:03.082+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=108;_ThreadName=Thread-2;|--------------- Throwing Exception ------------|#]

[#|2014-05-26T14:16:03.082+0500|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=108;_ThreadName=Thread-2;|--------------- Catching Exception ------------|#]

2 个答案:

答案 0 :(得分:0)

只是一个猜测,但也许

response.sendRedirect("/");

是否会将您发送到您的欢迎页面,这恰好会再次触发过滤器链,冲洗并重复...您是否尝试重定向到/ntlm/*模式之外的某些特定资源?例如,重定向到http://www.google.com,然后查看是否在日志中显示相同的结果。

答案 1 :(得分:0)

如果你想在捕获一些异常时返回,而不是调用左过滤器。

您可能需要从ContainerRequestContext类调用以下代码: requestContext.abortWith(*,*)

可能需要检查Filter类中的相关方法。