【发布时间】:2016-12-11 07:00:08
【问题描述】:
我对以下问题有点绝望。在 JSF/PrimeFaces (Wildfly 10) 中,我们同时使用 AJAX 和非 AJAX 调用。如果会话过期并且有人与系统交互(点击),则应将其重定向到login.xhtml 并要求用户重新登录。问题是它只适用于非 AJAX 控件。如果我单击 AJAX 控件,我会进入登录页面,但如果我填写登录名/密码,则会收到以下错误:
<partial-response id="j_id1">
<changes>
<update id="j_id1:javax.faces.ViewState:0">
<![CDATA[ 982009515771098472:-6468147585577406798 ]]>
</update>
</changes>
</partial-response>
我阅读了大量材料并实现了以下课程:
JsfAjaxPhaseListener.java
public class JsfAjaxPhaseListener implements PhaseListener {
public void beforePhase(PhaseEvent event) {
}
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
@Override
public void afterPhase(PhaseEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest httpRequest = (HttpServletRequest) context.getExternalContext().getRequest();
if (httpRequest.getRequestedSessionId() != null && !httpRequest.isRequestedSessionIdValid()) {
String facesRequestHeader = httpRequest.getHeader("Faces-Request");
boolean isAjaxRequest = facesRequestHeader != null && facesRequestHeader.equals("partial/ajax");
if (isAjaxRequest) {
try {
context.getExternalContext().redirect("/admin/login.xhtml");
} catch (IOException ex) {
Logger.getLogger(JsfAjaxPhaseListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
SessionExceptionHandler.java
public class SessionExceptionHandler extends ExceptionHandlerWrapper {
private static final Logger LOGGER = LoggerFactory.getLogger(SessionExceptionHandler.class);
private ExceptionHandler wrapped;
SessionExceptionHandler(ExceptionHandler exception) {
this.wrapped = exception;
}
@Override
public ExceptionHandler getWrapped() {
return wrapped;
}
@Override
public void handle() throws FacesException {
final Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator();
while (i.hasNext()) {
ExceptionQueuedEvent event = i.next();
ExceptionQueuedEventContext context
= (ExceptionQueuedEventContext) event.getSource();
Throwable t = context.getException();
if (t instanceof ViewExpiredException) {
ViewExpiredException vee =(ViewExpiredException) t;
FacesContext fc =FacesContext.getCurrentInstance();
Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
NavigationHandler nav =
fc.getApplication().getNavigationHandler();
try {
((ConfigurableNavigationHandler)nav).performNavigation("admin/login.xhtml?faces-redirect=true");
fc.renderResponse();
} finally {
i.remove();
}
}
}
getWrapped().handle();
}
}
SessionExceptionHandlerFactory.java
public class SessionExceptionHandlerFactory extends ExceptionHandlerFactory {
private ExceptionHandlerFactory parent;
public SessionExceptionHandlerFactory(ExceptionHandlerFactory parent) {
this.parent = parent;
}
@Override
public ExceptionHandler getExceptionHandler() {
ExceptionHandler handler = new SessionExceptionHandler(parent.getExceptionHandler());
return handler;
}
}
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://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>facelets.SKIP_COMMENTS</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>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
10
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>admin/index.xhtml</welcome-file>
</welcome-file-list>
<security-constraint>
<display-name>securityConstraint</display-name>
<web-resource-collection>
<web-resource-name>resources</web-resource-name>
<url-pattern>/admin/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
<auth-constraint>
<role-name>Administrator</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>incomakerSecurityDomain</realm-name>
<form-login-config>
<form-login-page>/login.xhtml</form-login-page>
<form-error-page>/errorLogin.xhtml</form-error-page>
</form-login-config>
</login-config>
<security-role>
<role-name>Administrator</role-name>
</security-role>
<error-page>
<error-code>500</error-code>
<location>/errorCatchall.xhtml</location>
</error-page>
</web-app>
我们尝试了各种重定向,对调用进行了各种修改,花了整整两天时间,但没有任何帮助。如果我手动重新加载登录页面,一切正常。如果我尝试以编程方式重新加载它,它总是会因部分响应而失败。
感谢您的帮助。
【问题讨论】:
-
谢谢@BalusC。不幸的是,它似乎不起作用。首先,我们使用的是 PrimeFaces,而不是 OmniFaces。我试图在 context.getExternalContext().redirect() 之前添加 invalidateSession(),但是没有任何成功。似乎有帮助的是使用 ConfigurableNavigationHandler.performNavigation() 而不是 context.getExternalContext().redirect,但我不知道为什么并认为它是一种解决方法而不是解决方案。 :-(
-
OmniFaces 不是 PrimeFaces 那样的组件库。它是一个实用程序库,可用于任何基于 JSF 的应用程序。
-
好的,谢谢,会检查的。
标签: ajax jsf redirect wildfly j-security-check