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
- Package com.company.util;
- Import java.util.ArrayList;
- Import javax.servlet.http.HttpSession;
- Public class Token {
- Private static final String token_list_name = "Tokenlist";
- Public static final String token_string_name = "TOKEN";
- Private static ArrayList Gettokenlist (HttpSession session) {
- Object obj = Session.getattribute (token_list_name);
- if (obj! = null) {
- return (ArrayList) obj;
- } Else {
- ArrayList tokenlist = new ArrayList ();
- Session.setattribute (Token_list_name, tokenlist);
- return tokenlist;
- }
- }
- Private static void savetokenstring (String tokenstr, HttpSession session) {
- ArrayList tokenlist = gettokenlist (session);
- Tokenlist.add (TOKENSTR);
- Session.setattribute (Token_list_name, tokenlist);
- }
- Private static String generatetokenstring () {
- return New Long (System.currenttimemillis ()). ToString ();
- }
- /** *//**
- * Generate a token string, and save the string in session, then return the token string.
- * @param HttpSession Session
- * @return A token string used for enforcing a single request for a particular transaction.
- */
- public static String Gettokenstring (HttpSession session) {
- String tokenstr = generatetokenstring ();
- Savetokenstring (TOKENSTR, session);
- return tokenstr;
- }
- /** *//**
- * Check whether token string is valid. If session contains the token string, return True.
- * Otherwise, return false.
- * @param String Tokenstr
- * @param HttpSession Session
- * @return True:session contains tokenstr; False:session is null or TOKENSTR are ID not in session
- */
- public Static boolean istokenstringvalid (String Tokenstr, HttpSession session) {
- Boolean valid = false;
- if (session! = null) {
- ArrayList tokenlist = gettokenlist (session);
- if (Tokenlist.contains (TOKENSTR)) {
- valid = true;
- Tokenlist.remove (TOKENSTR);
- }
- }
- return valid;
- }
- }
(ii) in the JSP page
1: Import the Token tool class first
[Java]View plain copy
- <%@ page import="Com.company.util.Token"%>
2: Add hidden token values to the form
[HTML]View plain copy
- <form>
- <input type="hidden" name= "<%=token.token_string_name%>" value= "<%= Token.gettokenstring (session)%> ">
- </form>
(iii) Add the following code to the server-side servlet
[Java]View plain copy
- if (Token.istokenstringvalid (Request.getparameter (Token.token_string_name), Request.getsession ())) {
- //to do business code
- }
Token validation in Java