zoukankan      html  css  js  c++  java
  • 防止用户重复提交表单数据,session方式,js方式

    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);
                 }
            }
    }

    待完善。。。

  • 相关阅读:
    Task Schedule
    Number Game
    CDQ分治
    Friends and Subsequences
    HDU5266 pog loves szh III
    P1593 因子和
    求一个数的欧拉函数的优化
    Grandpa's Estate POJ
    LightOJ
    Paint The Wall HDU
  • 原文地址:https://www.cnblogs.com/sean-zeng/p/11024811.html
Copyright © 2011-2022 走看看