複製代碼代碼如下:
導入com.google.common.base.charsets;
導入com.google.common.hash.hashing;
導入java.util.date;
導入org.apache.commons.lang.time.fastDateFormat;
/**
*
* @author查爾斯
*/
公共類Tokenutils {
私有靜態最終字符串privateKey =“ fdas34ljfr sja@#8 $%dfkl; js&4*daklfjsdl; akfjsa342“;
公共靜態字符串getToken(字符串密碼,字符串日期){
返回hashing.md5()。newhasher()。
putString(密碼,charsets.utf_8)。
putString(privateKey,charsets.utf_8)。
putString(date,charsets.utf_8).hash()。toString();
}
公共靜態字符串getToken(字符串密碼,日期){
返回hashing.md5()。newhasher()。
putString(密碼,charsets.utf_8)。
putString(privateKey,charsets.utf_8)。
putString(getDate(date),charsets.utf_8).hash()。toString();
}
公共靜態字符串getToken(字符串密碼){
返回hashing.md5()。newhasher()。
putString(密碼,charsets.utf_8)。
putString(privateKey,charsets.utf_8).putString(getDate(),charsets.utf_8).hash()。toString();
}
公共靜態布爾有效孔(字符串令牌,字符串密碼){
字符串確認= getToken(密碼);
if(confirn..equals(token)){
返回true;
} 別的 {
返回false;
}
}
公共靜態字符串getDate(){
日期= new Date(System.CurrentTimeMillis());
返回fastdateformat.getInstance(“ yyyymmddhh”)。格式(date);
}
公共靜態字符串getDate(現在日期){
返回fastdateformat.getInstance(“ yyyymmddhh”)。格式(now);
}
公共靜態字符串getNexThour(現在日期){
日期= new Date(Now.getTime()+60*60*1000);
返回fastdateformat.getInstance(“ yyyymmddhh”)。格式(date);
}
}