我正在使用omnifaces和prmifaces进行登录并尝试从omnifaces实现webFilter。但我总是得到404,没有任何事情发生。有人可以帮帮我吗! 这是我的web.xml。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>icarus-red</param-value>
</context-param>
<context-param>
<param-name>primefaces.FONT_AWESOME</param-name>
<param-value>true</param-value>
</context-param>
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>pt.isec.pd.pgs.login.LoginFilter</filter-class>
</filter>
<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>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.xhtml</welcome-file>
</welcome-file-list>
</web-app>
这是我的LoginFilter类。
@WebFilter("/secured/*")
public class LoginFilter extends HttpFilter {
@Inject
private LoginBackingBean loginBackingBean;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
@Override
public void doFilter(HttpServletRequest request, HttpServletResponse response, HttpSession session, FilterChain chain) throws ServletException, IOException {
System.out.println("doFilter");
String loginURL = request.getContextPath() + "/login.xhtml";
boolean loggedIn = (session != null) && (session.getAttribute("user") != null);
boolean loginRequest = request.getRequestURI().equals(loginURL);
boolean resourceRequest = Servlets.isFacesResourceRequest(request);
if (loggedIn || loginRequest || resourceRequest) {
if (!resourceRequest) { // Prevent browser from caching restricted resources. See also https://stackoverflow.com/q/4194207/157882
Servlets.setNoCacheHeaders(response);
}
chain.doFilter(request, response); // So, just continue request.
} else {
Servlets.facesRedirect(request, response, loginURL);
}
}
}
我有/login.xhtml和/secured/home.xhtml。我的backingBean是LoginBackingBean。
我做错了什么!?如果有人需要更多信息,请告诉我。因为我非常沮丧。 提前谢谢你。
答案 0 :(得分:0)
将我的方法改为“默认”java LoginFilter。它解决了我的问题。 这是我的过滤器类
@WebFilter("/secured/*")
public class LoginFilter implements Filter {
final static Logger LOGGER = Logger.getLogger(LoginFilter.class);
@Inject
private LoginBackingBean loginBean;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
LOGGER.debug("init()");}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
LOGGER.debug("doFilter() -> LoggedIn: " + loginBean.isLoggedIn());
if (loginBean == null || !loginBean.isLoggedIn()) {
LOGGER.debug("to login!");
String contextPath = ((HttpServletRequest) request).getContextPath();
((HttpServletResponse) response).sendRedirect(contextPath + "/login.xhtml");
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
LOGGER.debug("destroy()");
}
}
这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>icarus-red</param-value>
</context-param>
<context-param>
<param-name>primefaces.FONT_AWESOME</param-name>
<param-value>true</param-value>
</context-param>
<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>LoginFilter</filter-name>
<filter-class>pt.isec.pd.pgs.login.LoginFilter</filter-class>
</filter>
<welcome-file-list>
<welcome-file>secured/home.xhtml</welcome-file>
</welcome-file-list>
有任何疑问,你可以联系。