【发布时间】:2014-10-20 16:11:53
【问题描述】:
我有一个 JSF Web 应用程序,其中驻留在目录 web 下的所有页面都需要受到保护以防止未经身份验证的使用,即用户应该在会话中访问这些页面。我正在使用过滤器来验证这些页面的会话。这些页面通过以下网址访问:/contextRoot/web/download.xhtml 或 /contextRoot/web/sign/upload.xhtml。而位于 web 目录之外或其他目录中的其他页面不需要通过会话验证过滤器。我的过滤器是这样的:
@WebFilter(filterName = "AuthenticationFilter", urlPatterns={"/web/*"}, dispatcherTypes = {DispatcherType.REQUEST})
public class AuthenticationFilter implements Filter {
private static final boolean debug = true;
private FilterConfig filterConfig = null;
public AuthenticationFilter() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
if (debug) {
log("AuthenticationFilter:doFilter()");
}
HttpSession session = ((HttpServletRequest) request).getSession(false);
if (session == null || session.getAttribute("username") == null) {
System.out.println("Your Session Not Active. You are redirected.");
//((HttpServletResponse) response).sendRedirect("home.xhtml");
} else {
System.out.println("Your Session is active. username : " + session.getAttribute("username"));
}
Throwable problem = null;
try {
chain.doFilter(request, response);
} catch (Throwable t) {
// If an exception is thrown somewhere down the filter chain,
// we still want to execute our after processing, and then
// rethrow the problem after that.
problem = t;
t.printStackTrace();
}
}
}
我正在使用 urlPattern /web/* 以便 web 目录中的每个页面都将通过此过滤器。过滤器现在只是打印用于调试的东西。但是每当我访问网页目录或任何其他页面中的页面时,它都不会通过过滤器。我也尝试使用 /faces/web/* 作为 urlPattern 但这也没有用。但是当我把 /* 作为 urlPattern 时,每个访问的页面都会通过过滤器。
我以http://localhost:8080/CodesignWebApp/faces/web/sign/SelectServer.xhtml http://localhost:8080/CodesignWebApp/faces/web/sign/FileUpload.xhtml?signmethod=MICROSOFT
我怀疑 urlPattern 有问题。
【问题讨论】:
-
“我的页面其实不是直接在web下的,它们就像web/sign/sign.xhtml” 这个很模糊,很混乱。请详细说明那部分。更好的是,让我们从头开始:请将您实际使用的网址复制粘贴到您想要调用此过滤器的网络浏览器的地址栏中。
-
添加了页面的确切 URL。
-
好的。那么,
/faces/web/*就是您需要的那个。这到底是怎么失败的?你确定你正在运行你认为你正在运行的代码吗? (更改 URL 模式后重新构建、重新部署等,即在启动期间仅初始化一次)。更好的选择是完全摆脱古老的 JSF 1.0 样式/faces/*路径映射,并用*.xhtml替换它。
标签: jsf-2 servlet-filters url-pattern