[Struts] Token Usage and principle

Source: Internet
Author: User

Struts Token Usage

1, first call the Savetoken (HttpServletRequest request) method in an action. Then turn to the JSP page with the form.

2, in the JSP page submit the form to an action, and then the action is to make a duplicate submission of the judgment.

if (Istokenvalid (request, True)) {

What you should do when you do not repeat the commit

Return Mapping.findforward ("Success");

} else {

What you need to do when you repeat a commit

Savetoken (Request);

Return Mapping.findforward ("error");

}

Struts Token Mechanism:

1, called by the first action Savetoken (HttpServletRequest request), this method is implemented internally as follows:

protected void Savetoken (HttpServletRequest request) {

Token.savetoken (Request);

}

Token.savetoken (Request);

The implementation of this method is as follows:

Public synchronized void Savetoken (HttpServletRequest request) {



HttpSession session = Request.getsession ();

String token = generatetoken (request);

if (token! = null) {

Session.setattribute (Globals.transaction_token_key, TOKEN);

}

}

This method calls the Generatetoken method implementation as follows:

Public synchronized void Savetoken (HttpServletRequest request) {



HttpSession session = Request.getsession ();

String token = generatetoken (request);

if (token! = null) {

Session.setattribute (Globals.transaction_token_key, TOKEN);

}

}

When the Generatetoken is complete, the resulting unique value is setattribute to the session.

Session.setattribute (Globals.transaction_token_key, TOKEN);

The value of the Globals.transaction_token_key is: "Org.apache.struts.action.TOKEN"

Then jump to the JSP page.

2, the JSP page of Struts Custom label

The doStartTag () method of this class invokes the RenderToken () method of this class.

Protected String RenderToken () {

StringBuffer results = new StringBuffer ();

HttpSession session = Pagecontext.getsession ();



if (session! = NULL) {

String token =

(String) Session.getattribute (Globals.transaction_token_key);



if (token! = null) {

Results.append ("<input type=\" hidden\ "name=\" ");

Results.append (Constants.token_key);

Results.append ("\" value=\ "");

Results.append (token);

if (this.isxhtml ()) {

Results.append ("\"/> ");

} else {

Results.append ("\" > ");

}

}

}



return results.tostring ();

}

This will generate a similar

<input type= "hidden" name= "Org.apache.struts.taglib.html.TOKEN"
Value= "6aa35341f25184fd996c4c918255c3ae" >

The hidden label.

Then submit to an action, using the Istokenvalid () method in action to compare the value of this key of "Org.apache.struts.action.TOKEN" in the session and the " Org.apache.struts.action.TOKEN "This value is consistent.

If true, then the proof can be submitted. If False, the proof is repeated and no commit is allowed.

[Struts] Token Usage and principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.