• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

后台API权限认证方式

武飞扬头像
langy1990
帮助1

1.第一个步骤时间是否超时

       客户端发送加密字符串   md5(appid time.time()) "|" time.time()

2.是否已经在访问记录中存在

      md5str in visited  判定是重复发送或者是被截获重发送,提示不合法

3.服务端md5加密后的密文是否一致

          md5str, client_time = client_header_data

          md5(appid client_time) 是否等于  md5str

          client_time < 服务器时间 -10    判定超时

4. 当前时间,这个就是以秒数做单位的
         now_time = time.time()

5.去掉后边的小数点部分
        ntime = now_time.split('.')[0]

6.转化成整数

        n_time = int(ntime)

1 import requests
 2 import hashlib
 3 import time
 4 
 5 appid="8888"
 6 current_time = str(time.time())
 7 
 8 m=hashlib.md5()
 9 m.update(bytes(appid current_time,encoding="utf-8"))
10 temp_appid = m.hexdigest()
11 print(temp_appid)
12 
13 new_appid="%s|%s"%(temp_appid,current_time)
14 print(new_appid)
15 
16 response = requests.get(
17     "http://127.0.0.1:8000/asset/",
18     params={"appid":appid},
19     headers={"appid":new_appid}
20 )
21 
22 print(response.text)

客户端

1 from django.shortcuts import render,HttpResponse
 2 from django.views import View
 3 import hashlib
 4 import time
 5 
 6 APPID="8888"
 7 visited = []
 8 
 9 class AssetView(View):
10 
11     def get(self,request,*args,**kwargs):
12         appid=request.META.get("HTTP_APPID")
13         if not appid:
14             return HttpResponse("没有提供appid")
15 
16         clientappid, client_time = appid.split("|")
17 
18         #clientappid是由appid和client_time一起做md5运算得到的唯一摘要
19         #client_time没有做md5运算 clientappid|client_time
20         #所以在后面可以直接参与时间的计算
21 
22         current_time = time.time()
23         f_client_time = float(client_time)
24         print(appid)
25         current_client={'encrypt':clientappid,'time':client_time}
26         if current_time-10 > f_client_time: #第一关时间是否超时
27             return HttpResponse("请求超时")
28         if current_client in visited:  #第二关合法的客户端key是否已经用过
29             return HttpResponse("appkey已经超时失效")
30 
31         m=hashlib.md5()
32         m.update(bytes(APPID client_time,encoding="utf-8"))
33         #第三关采用md5机制必须按照原来的字符串(appid client_time)进行密文验证
34         #此处没有解密按照原文比较,而是直接采用比对加密后的字符串
35         new_appid = m.hexdigest()
36 
37         if new_appid == clientappid:
38             visited.append({'encrypt': new_appid, 'time': f_client_time})
39             limit_timestamp = float(time.time()-10) #超过10秒的记录自动删除
40             del_keys = []
41             for k, v in enumerate(visited):
42                 m = v['time']
43                 n = v['encrypt']
44                 if m < limit_timestamp:
45                     del_keys.append(v)
46                     continue
47 
48             print("****************************************")
49             print(visited)
50             print(del_keys)
51             print("***************************************")
52 
53             for k in del_keys:
54                 visited.remove(k)
55             print("当前还有%d个访问记录" % (len(visited)))
56             return HttpResponse("验证成功")
57         else:
58             return HttpResponse("验证失败")

服务端

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhibgeae
系列文章
更多 icon
同类精品
更多 icon
继续加载