`
master3003
  • 浏览: 47042 次
社区版块
存档分类
最新评论

session 超时处理

 
阅读更多
    当我们使用Acegi安全框架时,一般都会对所有请求就像拦截。这样,如果session超时,通过DWR进行Ajax远程调用时,同样会被Acegi的Filter拦截,这时Acegi发现用户没有登录,就会对请求进行转发(转发到登录页面),但由于此时进行的是Ajax的调用,页面并不会将进行跳转,而是返回302的HTTP响应码,这样浏览器就不会有任何的反应,这样对用户是非常不友好的。可以通过一些简单的处理,在session超时后,当用户进行Ajax远程调用时,同其他请求一样,返回到登录页面。

      首先,在Acegi的安全配置文件(即Spring的配置文件)里面,将/dwr/**路径的请求配置为可以匿名用户可以访问的。

      其次,自定义一个DWRRemoter,大概实现如下:

Java代码 
public class MyDWRRemoter extends DefaultRemoter  
{  
 
    public Replies execute( Calls calls )  
    {  
        HttpSession session = WebContextFactory.get().getSession();  
        ISessionContainer sc = ( ISessionContainer ) session.getAttribute( ISessionContainer.SESSION_CONTAINER_KEY );  
 
        //session检查  
        if ( sc == null || sc.getUserInfo() == null )  
        {  
            logOut();  
            return super.execute( new Calls() );  
        }  
        else 
        {  
            IUserInfo userInfo = sc.getUserInfo();  
            if(!SecurityFactory.getInstance().isOnline( userInfo.getUserID(), session.getId() ))  
            {  
                logOut();  
                return super.execute( new Calls() );  
            }  
        }  
        return super.execute( calls );  
    }  
 
    private void logOut()  
    {  
        WebContext wct = WebContextFactory.get();  
        Util utilThis = new Util(wct.getScriptSession());  
        utilThis.addScript( new ScriptBuffer("logOut()"));  
    }  


public class MyDWRRemoter extends DefaultRemoter
{

    public Replies execute( Calls calls )
    {
        HttpSession session = WebContextFactory.get().getSession();
        ISessionContainer sc = ( ISessionContainer ) session.getAttribute( ISessionContainer.SESSION_CONTAINER_KEY );

        //session检查
        if ( sc == null || sc.getUserInfo() == null )
        {
            logOut();
            return super.execute( new Calls() );
        }
        else
        {
            IUserInfo userInfo = sc.getUserInfo();
            if(!SecurityFactory.getInstance().isOnline( userInfo.getUserID(), session.getId() ))
            {
                logOut();
                return super.execute( new Calls() );
            }
        }
        return super.execute( calls );
    }

    private void logOut()
    {
        WebContext wct = WebContextFactory.get();
        Util utilThis = new Util(wct.getScriptSession());
        utilThis.addScript( new ScriptBuffer("logOut()"));
    }
}

在web.xml文件中加入DWR的参数,是自定义的DWRRemoter生效:

Xml代码 
<init-param> 
            <param-name> 
                org.directwebremoting.extend.Remoter  
            </param-name> 
            <param-value>com.xxx.base.framework.web.MyDWRRemoter</param-value> 
        </init-param> 

<init-param>
            <param-name>
                org.directwebremoting.extend.Remoter
            </param-name>
            <param-value>com.xxx.base.framework.web.MyDWRRemoter</param-value>
        </init-param>

最后,在JSP页面上写下方法名为logOut的JavaScript函数,该函数返回到登录页面即可。



通过上面的三个简单步骤,即可实现在DWR调用远程方法时,如果session超时,系统顺利的退出到登录页面的效果。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics