尽管我似乎有它们的“确切”副本,但我已经阅读了许多示例,因此我无法弄清楚为什么当我导航到/login
而不是/restricted/*
. 我已经尝试过注释(如下例所示)和 XML 来定义 WebFilter。
网络过滤器
@WebFilter(filterName = "AuthenticationFilter", servletNames = { "Faces Servlet" }, urlPatterns = { "/restricted/*" } )
public class AuthenticationFilter implements Filter {
@Inject
private SessionManager sessionManager;
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("Active AuthenticationFilter");
if (sessionManager.getUser() == null) {
((HttpServletResponse) response).sendRedirect("/login");
}
else {
chain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
web.xml
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<filter>
<filter-name>Pretty Filter</filter-name>
<filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>Pretty Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
<filter>
<filter-name>NoCacheFilter</filter-name>
<filter-class>filter.NoCacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>NoCacheFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>filter.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/restricted/*</url-pattern>
</filter-mapping>
笔记
即使在两个文件中都定义了过滤器,我可以保证我不会同时使用这两种表示法。但是,为了测试和审查,我确实将它们都发布了。
我是使用网络过滤器的新手,并且找不到太多关于使用多个过滤器的信息。但是我读过的内容,您可以使用多个,它们将按照您在web.xml
.
问题
有没有人知道为什么我的过滤器也会在页面上执行/login
?该应用程序在 Glassfish 3.1.2 上运行。