6

我正在使用 JSF 和 RichFacecs 创建一个 Web 门户。我想在会话超时时将用户重定向到登录页面。我试图在会话到期/注销阶段抛出一个 SecurityException,如下所示

<error-page>
    <exception-type>java.lang.SecurityException</exception-type>
    <location>/Login.jsf</location>
</error-page>

但这对我不起作用。哪种是正确的处理方法?

4

5 回答 5

7

这应该这样做:

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/sessionExpired.jsf</location>
</error-page>
于 2009-09-17T20:24:09.737 回答
4

你应该在你的 web.xml 中设置一个超时并注册一个超时过滤器,如这个线程中所示:在 JSF 应用程序中自动注销在 ajax 的情况下,你的重定向必须像这样完成:

    String facesRequestHeader = httpServletRequest
            .getHeader( "Faces-Request" );

    boolean isAjaxRequest = facesRequestHeader != null
            && facesRequestHeader.equals( "partial/ajax" );

    if( isAjaxRequest )
    {
            String url = MessageFormat.format( "{0}://{1}:{2,number,####0}{3}",
            request.getScheme(), request.getServerName(),
            request.getServerPort(), timeoutPath );

            PrintWriter pw = response.getWriter();
                pw.println( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
            pw.println( "<partial-response><redirect url=\"" + url
            + "\"></redirect></partial-response>" );
            pw.flush(););
    }
    else
    {
        httpServletResponse.sendRedirect( timeoutPath );
    }
于 2012-05-03T12:04:59.437 回答
1

解决方法是使用 Richfaces 自己的session expired event.

将此添加到容易过期的页面:

<a4j:region>
 <script language="javascript">
 A4J.AJAX.onExpired = function(loc, expiredMsg){
 alert('expired!');
 window.location = "/login.jsf";
 }
 </script>
</a4j:region>

更多信息可以在 RichFaces 文档中找到:http: //docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/ArchitectureOverview.html#SessionExpiredHandling

于 2010-03-05T12:42:42.830 回答
1

我在会话到期后发出 A4J 请求时遇到了一些问题。我把这个

<context-param>
    <param-name>com.sun.faces.enableRestoreView11Compatibility</param-name>
    <param-value>true</param-value>
</context-param>

在我的 web.xml 中,对我来说它解决了这个问题。

于 2011-05-02T14:58:35.233 回答
0

另一种解决方案是创建扩展 ViewHandler 并覆盖 restoreView 方法的 CustomViewHandler

@Override
public UIViewRoot restoreView(FacesContext facesContext, String viewId) {
/**
 * {@link javax.faces.application.ViewExpiredException}. This happens only  when we try to logout from timed out pages.
 */
    UIViewRoot root = null; 
    root = parent.restoreView(facesContext, viewId);
    if(root == null) {          
        root = createView(facesContext, viewId);
    }
    return root;
}

然后你需要将它添加到你的 faces-config.xml

<view-handler>com.demo.CustomViewHandler</view-handler>

这将阻止您获得 ViewExpiredException 的

于 2009-10-19T15:57:23.603 回答