首页 > 代码库 > Java令牌生成器

Java令牌生成器

 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 = http://www.mamicode.com/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 }

 

Java令牌生成器