`
syq891015
  • 浏览: 11734 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

使用tokenSession时提示没有invalid.token的问题

阅读更多

因为struts2阻塞tokenSession的id值相同的访问,最初的访问结束后直接返回到正常处理代码,后面的访问略过,即只处理第一次访问。

按理说使用tokenSession可以不配置invalid.token的result,但是偶尔有种情况会提示“No result defined for action com.syq2cy.test.LoginAction and result invalid.token”,就是如下情况:

在<s:token/>的页面点击提交时,form提交前做了一次ajax访问,在这次ajax访问中会做tokenSession的id判断,但ajax访问未将tokenSession作为参数提交到后台,就会返回invalid.token。代码:

    @Override

    protected String handleToken(ActionInvocation invocation) throws Exception {

        //see WW-2902: we need to use the real HttpSession here, as opposed to the map

        //that wraps the session, because a new wrap is created on every request

        HttpSession session = ServletActionContext.getRequest().getSession(true);

        synchronized (session) {

            if (!TokenHelper.validToken()) {   //判断参数中是否有tokenSession ajax返回false

                return handleInvalidToken(invocation);   // 走这

            }

            return handleValidToken(invocation);

        }

 

    }

 

 

  protected String handleInvalidToken(ActionInvocation invocation) throws Exception {

        ActionContext ac = invocation.getInvocationContext();

 

        HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);

        HttpServletResponse response = (HttpServletResponse)             ac.get(ServletActionContext.HTTP_RESPONSE);

        String tokenName = TokenHelper.getTokenName();

        String token = TokenHelper.getToken(tokenName);

 

        if ((tokenName != null) && (token != null)) {   // 都是null

           ......

        }

        return INVALID_TOKEN_CODE;   // 即invalid.token

}

 

 

解决办法:

<interceptor-ref name="token">

   <param name="includeMethods">login</param> // 加上这个就ok了

</interceptor-ref>

 

另:1、Unexpected Exception caught setting 'token' on...这个东西不必在意,是params的问题。

       2、很多博客上都说第一次访问tokenSession比对成功后,session重新弄个数,这是不对的,应该是从session中清空这次的tokenSession,所以第二次访问的时候session中的tokenSession已经是null了,代码:public class TokenHelper{

           public static boolean validToken(){

               ...

               session.remove(tokenSessionName);

           }

       }

作者:sunyq

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics