1. Token usage
Token is the token of the HTTP request, which is a common point of proof, and is designed to prevent the API from being accessed freely.
Can be generated using random numbers, or you can use a user ID, password, or time to sort or encrypt claims.
Tokens generally have an expiration date, such as more than 1 hours or 2 hours, and need to be re-logged in to get a new one.
2. Procedure principle
Token of this program is generated using the user ID and the current time (exact to hours).
A. After the user logs on, the server returns the token value to the client;
B. Client requests other interfaces with token
C. Whether or not the server has a time-out check for tokens
D. Validation through business processing, the validation does not pass the notification client.
3. Related code
Importcom.google.common.base.Charsets;Importcom.google.common.hash.Hashing;Importjava.util.Date;ImportOrg.apache.commons.lang.time.FastDateFormat;/*** Token is valid between 1-2 hours*/ Public classTokenutils {Private Static FinalString Privatekey = "FDAS34LJFR good [email protected] #8 $%dfkl;js&4*daklfjsdl;akfjsa342"; Public Staticstring GetToken (String userId, String date) {returnhashing.md5 (). Newhasher (). Putstring (UserId, Charsets.utf_8). Putstring (Privatekey, Charsets.utf_8). Putstring (date, charsets.utf_8). hash (). toString (); } Public Staticstring GetToken (string userId, date date) {returnhashing.md5 (). Newhasher (). Putstring (UserId, Charsets.utf_8). Putstring (Privatekey, Charsets.utf_8). Putstring (getDate (date), charsets.utf_8). hash (). toString (); } Public Staticstring GetToken (String userId) {returnhashing.md5 (). Newhasher (). Putstring (UserId, Charsets.utf_8). Putstring (Privatekey, Charsets.utf_8). Putstring (GetDate (), charsets.utf_8). hash (). toString (); } /*** Within 2 hours of verification through * *@paramtoken *@paramUserId *@return */ Public Static BooleanValidtoken (String token, string userId) {string Confirm=GetToken (userId); String Confirmnexthour=GetToken (UserId, Getnexthour ()); if(confirm.equals (token) | |confirmnexthour.equals (token)) { return true; } Else { return false; } } Public StaticString getDate () {Date Date=NewDate (System.currenttimemillis ()); returnFastdateformat.getinstance ("YYYYMMDDHH"). Format (date); } Public StaticString getDate (Date now) {returnFastdateformat.getinstance ("YYYYMMDDHH"). Format (now); } Public StaticString Getnexthour () {Date Date=NewDate (System.currenttimemillis () + 60 * 60 * 1000); returnFastdateformat.getinstance ("YYYYMMDDHH"). Format (date); } Public StaticString getnexthour (date now) {Date Date=NewDate (Now.gettime () + 60 * 60 * 1000); returnFastdateformat.getinstance ("YYYYMMDDHH"). Format (date); } Public Static voidMain (string[] args) {Date now=NewDate (); System.out.println ("GetToken (string userId, String date):" + GetToken ("135", "2016061523")); System.out.println ("String GetToken (String userId):" + GetToken ("135")); System.out.println ("String getDate ():" +getDate ()); System.out.println ("String GetDate (Date Now):" +GetDate (now)); System.out.println ("Getnexthour (Date Now):" +Getnexthour (now)); System.out.println ("Getnexthour ():" +Getnexthour ()); System.out.println ("Validtoken (String token, string userId):" + Validtoken ("0dc01307bd76368628a2a0a4c3e65b61", "135")); }}
4. Maven Dependency
<Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupId>Com.binfoo.www</groupId> <Artifactid>Javastudy</Artifactid> <version>1.0</version><Dependencies> <Dependency> <groupId>Com.google.guava</groupId> <Artifactid>Guava</Artifactid> <version>14.0.1</version> </Dependency> <!--Http://mvnrepository.com/artifact/commons-lang/commons-lang - <Dependency> <groupId>Commons-lang</groupId> <Artifactid>Commons-lang</Artifactid> <version>2.6</version> </Dependency></Dependencies></Project>
5. Test results
A tokenutils program, pro-Test available