嗨,我正在使用 jsf2.0,prettyfaces,primesfaces
我创建了一个过滤器来监控用户会话
@WebFilter(urlPatterns= {"*.xhtml"} , dispatcherTypes = {DispatcherType.REQUEST})
public class Authentication implements Filter {
@Override
public void init(FilterConfig config) throws ServletException {
System.out.println("[Authentication Filter] : init Method");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
PrettyContext context = PrettyContext.getCurrentInstance(request);
if (!(context.getCurrentMapping().getId().equals("login")) && (session == null || session.getAttribute("username") == null)) {
{
response.sendRedirect(request.getContextPath()+"/login");
}
else {
chain.doFilter(req, res); // Logged-in user found, so just continue request.
}
@Override
public void destroy() {}
}
web.xml
<welcome-file-list>
<welcome-file>/</welcome-file>
</welcome-file-list>
<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>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
漂亮的配置.xml
<url-mapping id="home">
<pattern value="/" />
<view-id value="index.xhtml" />
</url-mapping>
当我使用 dispatcherTypes = {DispatcherType.REQUEST} 我在 context.getCurrentMapping().getId() 上得到空指针异常,该语句在 if 语句中使用,见上文,但是当我 dispatcherTypes = {DispatcherType.FORWARD} 它对我来说工作正常发生了什么事可以指导我吗?谢谢,我想知道 DispatcherType.REQUEST 和 DispatcherType.FORWARD 之间的区别,另一个问题是当我使用 FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); 用于销毁会话,如果我从浏览器按 BACK 按钮,它会将我带到上一页,为什么浏览器会显示我的隐私页面?,我想在 BAck 按钮上登录页面,谢谢。