使用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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值