以下の通りtoken interceptorで二重送信防止の処理を設定していて**「1度目の送信で"invalid.token"がreturnされてしまいActionが実行できない」という事象**に遭遇しました。
XML
1<action name="TestAction" class="test.action.TestAction" method="execute"> 2 <interceptor-ref name="defaultStack"/> 3 <interceptor-ref name="token"/> 4 <result name="success">xxx.jsp</result> 5 <result name="invalid.token">yyy.jsp</result> 6</action>
JSPにも<s:token>は仕込み済みです。
しかし、1度目はしっかりとActionを実行できる場合と、1度目でもinvalid.tokenが返ってきてActionが実行できない場合と結果がまちまちであることが分かり、トリガは何かと色々と試してみると、1点気づいたことがありました。それは、
web.xmlのsession-configでsession-timeoutを1分に設定しており、二重送信防止のtokenを仕込んだボタンがある画面で1分以上待ったあとボタンを押すと、1度目にも関わらずinvalid.tokenが返ってきてしまう
ということが分かりました。1分以内に素早くボタンを押せば1度目は成功、ブラウザバックやリロードによる2度目の実行はできません。
しかし、私の頭ではなぜボタンのあるページでセッション切れ状態にすると1度目の実行でinvalid.tokenが返ってきてしまうのか辻褄が合いません。
以下サイト最後のまとめ部分に以下のように書いてありました。
https://www.journaldev.com/2281/struts2-token-interceptor-example
1. When a request is made to the update action, Struts2 tags API generates a unique token and set it to the session. The same token is sent in the HTML response as hidden field.
2.When the form is submitted with token, it is intercepted by token interceptor where it tries to fetch the token from the session and validate that it’s same as the token received in the request form. If token is found in session and validated then the request is forwarded to the next interceptor in the chain. Token interceptor also removes the token from the session.
これを読む限り、リクエストが発生(ボタンを押)したらtokenをセッションにセットするとあるので、ボタンのあるページで1分以上待ってセッションが切れようが、ボタンを押した瞬間にtokenが新たにセッションにセットされそのtokenのセッションが1分間持続することになるはずだ、そしてインターセプタがtokenをセッションから受け取れるはずだ、と思いました。
なのになぜ初回からinvalid.tokenが返ってきてしまうのか? これが理解できません。
解説をいただけると非常に助かりますm(_ _)m
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。