web-Django-22-验证码

  • 目录
  • 生成验证码
  • 认证验证码
  • 插件认证

生成验证码

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
def get_valid_img(request):
# 第一步
# f=open("default.jpg",'rb')
# data=f.read()

# 第二步
# import PIL
# from PIL import Image
# img = Image.new(mode='RGB', size=(120, 30), color=(123, 222, 255))
# f=open('code.png','wb')
# img.save(f,'png')
# f = open('code.png', 'rb')
# data=f.read()
# f.close()

# 第三步
# from io import BytesIO
# f=BytesIO()
#
# from PIL import Image
# img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 66))
# img.save(f,"png")
# data=f.getvalue()

# # 第四步:
#
# from io import BytesIO
#
# f = BytesIO()
# from PIL import Image, ImageDraw, ImageFont
#
# # 画字
# img = Image.new(mode='RGB', size=(120, 30), color=(0, 255, 0))
# draw = ImageDraw.Draw(img, mode='RGB')
# font=ImageFont.truetype("blog/static/dist/fonts/kumo.ttf",28)
# draw.text([15,0],'y',"red",font=font)
#
# img.save(f,"png")
# data=f.getvalue()


# 第五步:
import random
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
f = BytesIO()
img = Image.new(mode='RGB', size=(120, 30),
color=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
draw = ImageDraw.Draw(img, mode='RGB')

char_list = []
# # 画字
for i in range(5):
char = random.choice([chr(random.randint(65, 90)), str(random.randint(1, 9)), chr(random.randint(97, 122)), ])

font = ImageFont.truetype("blog/static/dist/fonts/kumo.ttf", 28)
draw.text([i * 24, 0], char, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),font=font)
char_list.append(char)
#
# # =========================
width = 120
height = 30


def rndColor():
"""
生成随机颜色
:return:
"""
return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))


# 写干扰点
for i in range(40):
draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())

# # 写干扰圆圈
# for i in range(40):
# draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
# x = random.randint(0, width)
# y = random.randint(0, height)
# draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())
#
# # 画干扰线
# for i in range(5):
# x1 = random.randint(0, width)
# y1 = random.randint(0, height)
# x2 = random.randint(0, width)
# y2 = random.randint(0, height)
#
# draw.line((x1, y1, x2, y2), fill=rndColor())

img.save(f, "png")
data = f.getvalue()

s = ''.join(char_list)

request.session["valid_code"] = s

'''
Dajngo:
set_cookie("sessionId","asdsa234asd3sad")
in session表
sessionkey sessiondata
asdsa234asd3sad {"valid_code":s}
'''

return HttpResponse(data)

认证验证码

注意: 验证码的验证过程也是基于session/cookie,session/cookie知识必备

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
from PIL import Image
from blog.utils import BaseResponse
from django.contrib import auth
from blog import models

import json
from blog.forms import RegForm
def login(request):
if request.method=="GET":
return render(request,"login.html")
elif request.is_ajax():
username=request.POST.get("username")
password=request.POST.get("password")
valid=request.POST.get("valid") # 输入的验证码
keep_valid = request.session.get("valid_code")
loginResponse = BaseResponse()

if valid.upper() == keep_valid.upper():
user=auth.authenticate(username=username,password=password) # auth模块做认证
if user:
loginResponse.user = username
auth.login(request,user) # auth模块加session
else:
loginResponse.error = "用户名或密码错误"
else:
loginResponse.error="验证码错误"

return HttpResponse(json.dumps(loginResponse.__dict__))

插件认证

这个插件用于拦截机器识别,确定人为登录,使认证系统更加安全
等补充…

参考