Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 所有请求的Servlet过滤器_Java_Servlets_Servlet Filters - Fatal编程技术网

Java 所有请求的Servlet过滤器

Java 所有请求的Servlet过滤器,java,servlets,servlet-filters,Java,Servlets,Servlet Filters,我想知道,如何在web.xml中设置对每个请求都调用的筛选器?添加带有“*”通配符的筛选器映射 <filter-mapping> <filter-name>TestFilter</filter-name> <url-pattern>*</url-pattern> </filter-mapping> 测试过滤器 * 只需创建一个过滤器,并将其映射到/* e、 g 我的过滤器 com.mycompany.

我想知道,如何在web.xml中设置对每个请求都调用的筛选器?

添加带有“*”通配符的筛选器映射

  <filter-mapping>
   <filter-name>TestFilter</filter-name>
   <url-pattern>*</url-pattern>
 </filter-mapping>

测试过滤器
*

只需创建一个过滤器,并将其映射到/*

e、 g


我的过滤器
com.mycompany.MyFilter
我的过滤器
/*

您确定请求正在命中控制器/servlet吗?如果它正在进行Ajax调用或运行一些JS,那么过滤器将不会命中。

Spring Security Web将所有URL定义为
/**
。因此,它应该适用于所有请求

看-
org.springframework.security.web.util.matcher.AntPathRequestMatcher

它将
MATCH\u ALL
常量定义为
/**
,最后一个变量用于
matches
方法

粘贴到
org.springframework.security.web.util.matcher.AntPathRequestMatcher
中的方法定义下方,其中决定某些请求url是否匹配。如果模式设置为
MATCH\u ALL
aka
/**
则返回
true

public boolean matches(HttpServletRequest request) {
        if (this.httpMethod != null && StringUtils.hasText(request.getMethod())
                && this.httpMethod != valueOf(request.getMethod())) {
            if (logger.isDebugEnabled()) {
                logger.debug("Request '" + request.getMethod() + " "
                        + getRequestPath(request) + "'" + " doesn't match '"
                        + this.httpMethod + " " + this.pattern + "'");
            }

            return false;
        }

        if (this.pattern.equals(MATCH_ALL)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Request '" + getRequestPath(request)
                        + "' matched by universal pattern '/**'");
            }

            return true;
        }

        String url = getRequestPath(request);

        if (logger.isDebugEnabled()) {
            logger.debug("Checking match of request : '" + url + "'; against '"
                    + this.pattern + "'");
        }

        return this.matcher.matches(url);
    }

例如,当查询字符串不是绑定到servlet而是绑定到根url时,不会调用过滤器。你知道一个解决方案吗?我有一个完全相同的问题,我的过滤器不是为来自客户端的每个请求调用的。我需要一个过滤器来捕获所有请求,无论它们是在应用程序级别还是jboss容器级别……我们可以通过servlet过滤器捕获Tomcat中的所有请求(webapp和非webapp contextx)吗?@M.S.Naidu您找到解决方案了吗?我知道这是一条老线索,但我面临着同样的问题。有什么帮助吗?例如,当查询字符串不是绑定到servlet而是绑定到根url时,不会调用过滤器。你知道解决方案吗?应该打电话给它。您能检查一下与mysite.com关联的servlet是如何映射的吗?如果mysite.com是一个直接从web服务器上呈现的静态页面,那么java应用程序可能永远不会命中该url。如果是这样,那么我如何将筛选器绑定到根url?如果根url不是通过java应用程序提供的,则您无法附加筛选器(很遗憾:-)。如果需要提供过滤器,可以使用重定向规则模拟Web服务器上的行为,也可以获取java应用程序提供的根url。
public boolean matches(HttpServletRequest request) {
        if (this.httpMethod != null && StringUtils.hasText(request.getMethod())
                && this.httpMethod != valueOf(request.getMethod())) {
            if (logger.isDebugEnabled()) {
                logger.debug("Request '" + request.getMethod() + " "
                        + getRequestPath(request) + "'" + " doesn't match '"
                        + this.httpMethod + " " + this.pattern + "'");
            }

            return false;
        }

        if (this.pattern.equals(MATCH_ALL)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Request '" + getRequestPath(request)
                        + "' matched by universal pattern '/**'");
            }

            return true;
        }

        String url = getRequestPath(request);

        if (logger.isDebugEnabled()) {
            logger.debug("Checking match of request : '" + url + "'; against '"
                    + this.pattern + "'");
        }

        return this.matcher.matches(url);
    }