1 package Token; 2 import java.security.MessageDigest; 3 import java.security.NoSuchAlgorithmException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 import java.util.Random; 7 import Decoder.BASE64Encoder; 8 9 //令牌生产器 10 public class TokenProcessor { 11 private TokenProcessor(){} 12 private static TokenProcessor instance = new TokenProcessor(); 13 public static TokenProcessor getInstance(){ 14 return instance; 15 } 16 public String generateTokeCode(){ 17 String value = System.currentTimeMillis()+new Random().nextInt()+""; 18 System.out.println(value); 19 20 21 long currentTime = System.currentTimeMillis(); 22 SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒"); 23 Date date = new Date(currentTime); 24 System.out.println(formatter.format(date)); 25 26 27 //获取数据指纹,指纹是唯一的 28 try { 29 MessageDigest md = MessageDigest.getInstance("md5"); 30 byte[] b = md.digest(value.getBytes());//产生数据的指纹 31 //Base64编码 32 BASE64Encoder be = new BASE64Encoder(); 33 be.encode(b); 34 System.out.println(be.encode(b)); 35 return be.encode(b);//制定一个编码 36 } catch (NoSuchAlgorithmException e){ 37 e.printStackTrace(); 38 } 39 return null; 40 } 41 public static void main(String[] args) { 42 TokenProcessor processor=new TokenProcessor(); 43 processor.generateTokeCode(); 44 } 45 }