我正在尝试使用 JSF ,休眠和 WildFly 构建一个简单的登录/注册界面。到目前为止,我已经使用了以下AuthorizationFilter类,以便在会话未通过身份验证时重定向到登录页面。 我还实现了一个名为
的简单用户CRUD视图。/admin.xhtml
管理数据库中的用户条目。现在的问题是,每当我单击一个按钮对admin.xhtml中的User条目进行CRUD时,我都会自动重定向到登录页面。当我禁用此WebFilter时,CRUD接口可以正常运行。 登录并创建会话后,如何防止此WebFilter重定向我?
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebFilter(filterName = "AuthFilter", urlPatterns = { "*.xhtml" })
public class AuthorizationFilter implements Filter {
public AuthorizationFilter() {
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
try {
HttpServletRequest reqt = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
HttpSession ses = reqt.getSession(false);
String reqURI = reqt.getRequestURI();
if (reqURI.contains("/login.xhtml")
|| (ses != null && ses.getAttribute("username") != null)
|| reqURI.contains("/public/")
|| reqURI.contains("javax.faces.resource"))
chain.doFilter(request, response);
else
resp.sendRedirect(reqt.getContextPath() + "/faces/login.xhtml");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Override
public void destroy() {
}
}