Token validation in Java

Source: Internet
Author: User

What is token: It is a token that is randomly unpredictable.

Why you need to use Token:1 to prevent duplicate submissions of forms

2: To prevent cross-site request forgery

Token's use flow is: first generate a random token value on the server side and save it on the server side, then pass the token value to the client during the request process. After the page operation is completed to the server to submit data to the server side, while comparing the token value is already present in the server side, if there is, the visit is secure, and the server side to the token value is deleted, if not, then this visit is invalid.

OK, look at the code after token usage (you can paste it directly in the project)

---------------------------------------------------------------------The following is the reproduced code:

(i) First, the Token tool class

[Java]View plain copy
  1. Package com.company.util;
  2. Import java.util.ArrayList;
  3. Import javax.servlet.http.HttpSession;
  4. Public class Token {
  5. Private static final String token_list_name = "Tokenlist";
  6. Public static final String token_string_name = "TOKEN";
  7. Private static ArrayList Gettokenlist (HttpSession session) {
  8. Object obj = Session.getattribute (token_list_name);
  9. if (obj! = null) {
  10. return (ArrayList) obj;
  11. } Else {
  12. ArrayList tokenlist = new ArrayList ();
  13. Session.setattribute (Token_list_name, tokenlist);
  14. return tokenlist;
  15. }
  16. }
  17. Private static void savetokenstring (String tokenstr, HttpSession session) {
  18. ArrayList tokenlist = gettokenlist (session);
  19. Tokenlist.add (TOKENSTR);
  20. Session.setattribute (Token_list_name, tokenlist);
  21. }
  22. Private static String generatetokenstring () {
  23. return New Long (System.currenttimemillis ()). ToString ();
  24. }
  25. /** *//** 
  26. * Generate a token string, and save the string in session, then return the token string.
  27. * @param HttpSession Session
  28. * @return A token string used for enforcing a single request for a particular transaction.
  29. */
  30. public static String Gettokenstring (HttpSession session) {
  31. String tokenstr = generatetokenstring ();
  32. Savetokenstring (TOKENSTR, session);
  33. return tokenstr;
  34. }
  35. /** *//** 
  36. * Check whether token string is valid. If session contains the token string, return True.
  37. * Otherwise, return false.
  38. * @param String Tokenstr
  39. * @param HttpSession Session
  40. * @return True:session contains tokenstr; False:session is null or TOKENSTR are ID not in session
  41. */
  42. public Static boolean istokenstringvalid (String Tokenstr, HttpSession session) {
  43. Boolean valid = false;
  44. if (session! = null) {
  45. ArrayList tokenlist = gettokenlist (session);
  46. if (Tokenlist.contains (TOKENSTR)) {
  47. valid = true;
  48. Tokenlist.remove (TOKENSTR);
  49. }
  50. }
  51. return valid;
  52. }
  53. }



(ii) in the JSP page

1: Import the Token tool class first

[Java]View plain copy
    1. <%@ page import="Com.company.util.Token"%>

2: Add hidden token values to the form

[HTML]View plain copy
    1. <form>
    2. <input type="hidden" name= "<%=token.token_string_name%>" value= "<%= Token.gettokenstring (session)%> ">
    3. </form>

(iii) Add the following code to the server-side servlet

[Java]View plain copy
      1. if (Token.istokenstringvalid (Request.getparameter (Token.token_string_name), Request.getsession ())) {
      2. //to do business code
      3. }

Token validation in Java

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.