How do I apply a servlet filter when serving an HT

2019-05-04 18:26发布

First off, I'm using Google AppEngine and Guice, but I suspect my problem is not related to these.

When the user connect to my (GWT) webapp, the URL is a direct html page. For example, in development mode, it is: http://127.0.0.1:8888/Puzzlebazar.html?gwt.codesvr=127.0.0.1:9997. Now, I setup my web.xml in the following way:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 version="2.5">
 <display-name>PuzzleBazar</display-name>

 <!-- Default page to serve -->
 <welcome-file-list>
  <welcome-file>Puzzlebazar.html</welcome-file>
 </welcome-file-list>


 <filter>
  <filter-name>guiceFilter</filter-name>
  <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
 </filter>

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

 <!--
  This Guice listener hijacks all further filters and servlets. Extra
  filters and servlets have to be configured in your
  ServletModule#configureServlets() by calling
  serve(String).with(Class<? extends HttpServlet>) and
  filter(String).through(Class<? extends Filter)
 -->
 <listener>
  <listener-class>com.puzzlebazar.server.guice.MyGuiceServletContextListener
  </listener-class>
 </listener>

</web-app>

And my appengine-web.xml is:

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>puzzlebazaar</application>
    <version>1</version>
    <sessions-enabled>true</sessions-enabled>

    <!-- Configure java.util.logging -->
    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>     
    </system-properties>

</appengine-web-app>

Since I'm using Guice, I have to configure extra filters in my ServletModule, where I do this:

filter("*.html").through( SecurityCookieFilter.class );

But my SecurityCookieFilter.doFilter is never called. I tried things like "*.html*" or <url-pattern>*</url-pattern> but to no avail. Any idea how I should do this?

2条回答
在下西门庆
2楼-- · 2019-05-04 19:10

If your need happens to be (like mine) to make GAE add certain http response headers for static HTML files, you can do this by declaring them as static resource in appengine-web.xml like this:

<static-files>
  <include path="/my_static-files" >
    <http-header name="Access-Control-Allow-Origin" value="http://example.org" />
  </include>
</static-files>

I found this in the GAE documentation

查看更多
狗以群分
3楼-- · 2019-05-04 19:23

You've probably configured html files to be served up as static content in your appengine-web.xml. Static file serving doesn't involve your app at all, so you can't filter the output.

查看更多
登录 后发表回答