Search code examples
struts2jsp-tags

Modify the token returned by Struts2 s:token tag


In a Struts 2 webapp (which also uses Spring), I want to modify the "output" of the <s:token> tag to use my own token-generation algorithm.

It appears the <s:token> tag is handled by a class called org.apache.struts2.components.Token, which is a UIBean. Is there a standard way using either Struts or Spring by which I can "extend" or "modify" or replace the Token class, or the methods it uses to generate tokens?

Alternatively, can I make up my own <mytoken> tag that works in Struts 2?


Solution

  • At the moment in the latest S2 version 2.3.20 there is no simple way of doing that. The TokenHelper class which is responsible for generating tokens is a simple utility class with static methods.

    You are better off using your own custom token solution. You need an interceptor which will handle tokens and something that will put token into JSP. For that you can create custom tag or just use a hidden input which calls some method in your base action.

    Note: The execAndWait interceptor uses TokenHelper too, so if you are using execAndWait you need to change it as well.