防止用户重复提交表单数据,session方式,js方式

xiaoxiao2021-02-28  108

1. 使用session的方式创建Token令牌解决

创建一个生成令牌的工具类,在该类中有返回类的对象,生成token的方法 public class TokenUtil { /* *单例设计模式(保证类的对象在内存中只有一个) *1、把类的构造函数私有 *2、自己创建一个类的对象 *3、对外提供一个公共的方法,返回类的对象 */ private TokenUtil(){} private static final TokenUtil instance = new TokenUtil(); /** * 返回类的对象 * @return */ public static TokenUtil getInstance(){ return instance; } /** * 生成Token * Token:Nv6RRuGEVvmGjB+jimI/gw== * @return */ public String makeToken(){ //checkException String token = (System.currentTimeMillis() + new Random().nextInt(999999999)) + ""; //数据指纹 128位长 16个字节 md5 try { MessageDigest md = MessageDigest.getInstance("md5"); byte md5[] = md.digest(token.getBytes()); //base64编码--任意二进制编码明文字符 adfsdfsdfsf BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(md5); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } }

待完善。。。

转载请注明原文地址: https://www.6miu.com/read-34157.html

最新回复(0)