2
votes

( this is regarding Java EE / Servlets WebFilters)

I am currently trying to write a WebFilter which catches every request made to the web application.

However, I noticed that the WebFilter does not see requests made within <script> tags.

The HTML-Page which is being served contains a stylesheet...

<link rel="stylesheet" href="/webjars/bootstrap/4.1.0/css/bootstrap.min.css">

...and three Javascript-includes...

<script src="/webjars/jquery/3.0.0/jquery.min.js"></script>
<script src="/webjars/popper.js/1.14.1/popper.min.js"></script>
<script src="/webjars/bootstrap/4.1.0/js/bootstrap.min.js"></script>

The @WebFilter, however, only sees requests to the root page (GET /) and to the stylesheet, shown in the logs created by the filter below.

12:04:51,909 INFO Init
12:04:51,909 INFO doFilter
12:04:51,909 INFO ServletRequest: HttpServletRequestImpl [ GET / ]
12:04:51,959 INFO doFilter
12:04:51,959 INFO ServletRequest: HttpServletRequestImpl [ GET /webjars/bootstrap/4.1.0/css/bootstrap.min.css ]

The Implementation of the WebFilter looks like this:

@WebFilter(filterName = "webjarFilter", urlPatterns = "/*")
public class WebJarFilter implements Filter {
    private Logger logger = LoggerFactory.getLogger(getClass());

    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("Init");
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        logger.info("doFilter");
        logger.info("ServletRequest: {}", servletRequest);
        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {
        logger.info("destroy");
    }
}

The web.xml file contains following mapping:

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

My question now is: Can somebody tell me why the filter does not catch the requests which should be generated from the <script> tags?

Observations and additional Info:

  • I am testing on Wildfly 12 with the Java EE 7
  • Directly opening the Javascript-Files will trigger the filter
  • When Javascript files with a wrong path are referenced, the filter sees the requests. Only inclusions of Javascript-files with the correct path are not seen by the filter
1

1 Answers

0
votes

Looks like I figured it out myself.

This is actually a result of Firefox behavior. When calling the same server from Chrome, the filter sees all requests. Apparently there is some caching mechanism involved within Firefox which applies to scripts.