一周前,我研究了 ViewExpiredException,并阅读了一些关于它的内容。
我的问题,在某些情况下,我想忽略ViewExpiredException
. 这些是不需要“会话”的情况,我的 Bean 是@RequestScoped
. 例如,login.xhtml
页面register.xhtml
和passwordRecovery.xhtml
。
在这些情况下,向用户显示您的会话已过期的错误是非常奇怪的。所以如果你打开登录页面,站了一会儿,当他通知你的数据并点击登录时,它会被转发到一个错误页面。我会忽略它并让用户透明。
所以,到目前为止,我的解决方案是创建一个ExceptionHandler
来忽略这些异常:
@Override
public void handle() throws FacesException {
for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
ExceptionQueuedEvent event = i.next();
ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
Throwable t = context.getException();
// just remove the exception from queue
if (t instanceof ViewExpiredException) {
i.remove();
}
}
getWrapped().handle();
}
然后,我创建了一个过滤器来检查用户是否已登录,如果没有,则重定向到登录页面(此过滤器仅适用于需要身份验证的页面):
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
if (!loginManagedBean.isLogged()) {
String pathLogin = request.getContextPath() + "/" + LOGIN_VIEW;
if (isAJAXRequest(request)) {
response.setContentType("text/xml");
response.getWriter()
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>", pathLogin);
return;
}
pathLogin += "?source=" + request.getServletPath();
response.sendRedirect(pathLogin);
return;
}
chain.doFilter(request, response);
}
所以当会话过期时,不会影响用户在登录和注册页面的体验。在我希望会话的页面上,由过滤器处理。
那将是一个很好的解决方案?是否有任何安全风险需要ViewExpiredException
忽略ExceptionHandler
?