一周前,我学习了ViewExpiredException,并阅读了一些有关它的内容。
我的问题,也就是某些情况下,我想忽略ViewExpiredException。这些情况不需要“会话”,我的Beans是@RequestScoped。例如,页面login.xhtml、register.xhtml和passwordRecovery.xhtml。
在这些情况下,向用户显示一个错误,说明您的会话已经过期,这是非常奇怪的。因此,如果您打开登录页面并停留一段时间,当他通知您的数据并单击Login时,它将被转发到错误页面。我会忽略它,让它对用户透明。
因此,到目前为止,我的解决方案是创建一个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的安全风险?
发布于 2015-07-07 19:37:27
在这种特殊情况下,忽略它们在技术上并不是坏事,但它表明设计是错误的。就好像你用错了工具来做这份工作。也就是说,这些观点实际上不应过期。
只需具体地使这些视图成为无状态的。
<f:view transient="true">
...
</f:view>这可以放置在页面的任何地方,甚至复制,但大多数自文档化都是将其作为页面、组合或定义的顶级标记。
另请参阅:
https://stackoverflow.com/questions/28675300
复制相似问题