JAVA后端生成Token(令牌),用于校验客户端

1.概述:在web项目中,服务端和前端经常需要交互数据,有的时候由于网络相应慢,客户端在提交某些敏感数据(比如按照正常的业务逻辑,此份数据只能保存一份)时,如果前端多次点击提交按钮会导致提交多份数据,这种情况我们是要防止发生的。

2.解决方法:

①前端处理:在提交之后通过js立即将按钮隐藏或者置为不可用。

②后端处理:对于每次提交到后台的数据必须校验,也就是通过前端携带的令牌(一串唯一字符串)与后端校验来判断当前数据是否有效。

3.总结:第一种方法相对来说比较简单,但是安全系数不高,第二种方法从根本上解决了问题,所以我推荐第二种方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

**

 * 生成Token的工具类:

 */

package red.hearing.eval.modules.token;

  

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.Random;

  

import sun.misc.BASE64Encoder;

  

/**

 * 生成Token的工具类

 * @author zhous

 * @since 2018-2-23 13:59:27

 *

 */

public class TokenProccessor {

     

     private TokenProccessor(){};

     private static final TokenProccessor instance = new TokenProccessor();

      

    public static TokenProccessor getInstance() {

        return instance;

    }

  

    /**

     * 生成Token

     * @return

     */

    public String makeToken() {

        String token = (System.currentTimeMillis() + new Random().nextInt(999999999)) + "";

         try {

            MessageDigest md = MessageDigest.getInstance("md5");

            byte md5[] =  md.digest(token.getBytes());

            BASE64Encoder encoder = new BASE64Encoder();

            return encoder.encode(md5);

        catch (NoSuchAlgorithmException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

         return null;

    }

}

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

/**

 *

 */

package red.hearing.eval.modules.token;

  

import javax.servlet.http.HttpServletRequest;

  

import org.apache.commons.lang3.StringUtils;

  

/**

 * Token的工具类

 * @author zhous

 * @since 2018-2-23 14:01:41

 *

 */

public class TokenTools {

     

    /**

     * 生成token放入session

     * @param request

     * @param tokenServerkey

     */

    public static void createToken(HttpServletRequest request,String tokenServerkey){

        String token = TokenProccessor.getInstance().makeToken();

        request.getSession().setAttribute(tokenServerkey, token);

    }

     

    /**

     * 移除token

     * @param request

     * @param tokenServerkey

     */

    public static void removeToken(HttpServletRequest request,String tokenServerkey){

        request.getSession().removeAttribute(tokenServerkey);

    }

     

    /**

     * 判断请求参数中的token是否和session中一致

     * @param request

     * @param tokenClientkey

     * @param tokenServerkey

     * @return

     */

    public static boolean judgeTokenIsEqual(HttpServletRequest request,String tokenClientkey,String tokenServerkey){

        String token_client = request.getParameter(tokenClientkey);

        if(StringUtils.isEmpty(token_client)){

            return false;

        }

        String token_server = (String) request.getSession().getAttribute(tokenServerkey);

        if(StringUtils.isEmpty(token_server)){

            return false;

        }

         

        if(!token_server.equals(token_client)){

            return false;

        }

         

        return true;

    }

     

}

  • 13
    点赞
  • 88
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值