目的:jumpserver 3.0登錄頁面加入驗(yàn)證碼潮模。
網(wǎng)上有第三方包Django Simple Captcha可以實(shí)現(xiàn)這個(gè)圖片驗(yàn)證香椎,用起來相對比較方便的变擒。但是我實(shí)踐時(shí)候是另一種方法颊艳,就當(dāng)過程學(xué)習(xí)了。
1枫疆、代碼生成驗(yàn)證碼圖片(結(jié)尾附上)爵川,驗(yàn)證碼的值存入django cache
調(diào)用的庫 from django.core.cache import cache
2、jumpserver的視圖文件view.py的Login函數(shù)增加驗(yàn)證碼方法
# 調(diào)用verify_code.py生成驗(yàn)證碼值和圖片
? ? today_str = datetime.date.today().strftime("%Y%m%d")
? ? verify_code_img_path = "%s/%s" % (settings.VERIFICATION_CODE_IMGS_DIR,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? today_str)
? ? if not os.path.isdir(verify_code_img_path):
? ? ? ? os.makedirs(verify_code_img_path)
? ? print("session:", request.session.session_key)
? ? random_filename = "".join(random.sample(string.ascii_lowercase,4))
? ? random_code = verify_code.gene_code(verify_code_img_path,random_filename)
? ? cache.set(random_filename, random_code,30)
#賬號(hào)驗(yàn)證息楔,先驗(yàn)證驗(yàn)證碼
username = request.POST.get('username')
? ? ? ? password = request.POST.get('password')
? ? ? ? _verify_code = request.POST.get('verify_code')
? ? ? ? _verify_code_key = request.POST.get('verify_code_key')
? ? ? ? if cache.get(_verify_code_key) == _verify_code:
? ? ? ? ? ? if username and password:
? ? ? ? ? ? ? ? user = authenticate(username=username, password=password)
3寝贡、前端頁面添加驗(yàn)證碼
附網(wǎng)上一份生成驗(yàn)證碼的代碼:
#_*_coding:utf-8_*_
from PIL import Image,ImageDraw,ImageFont,ImageFilter
import random
import math, string
#字體的位置,不同版本的系統(tǒng)會(huì)有不同
font_path = '/Library/Fonts/Arial.ttf'
#font_path = '/Library/Fonts/Hanzipen.ttc'
#生成幾位數(shù)的驗(yàn)證碼
number = 4
#生成驗(yàn)證碼圖片的高度和寬度
size = (100,30)
#背景顏色钞螟,默認(rèn)為白色
bgcolor = (255,255,255)
#字體顏色,默認(rèn)為藍(lán)色
fontcolor = (0,0,255)
#干擾線顏色谎碍。默認(rèn)為紅色
linecolor = (255,0,0)
#是否要加入干擾線
draw_line = True
#加入干擾線條數(shù)的上下限
line_number = (1,5)
def gen_text():
? ? source = list(string.ascii_letters)
? ? for index in range(0,10):
? ? ? ? source.append(str(index))
? ? return ''.join(random.sample(source,number))#number是生成驗(yàn)證碼的位數(shù)
#用來繪制干擾線
def gene_line(draw,width,height):
? ? begin = (random.randint(0, width), random.randint(0, height))
? ? end = (random.randint(0, width), random.randint(0, height))
? ? draw.line([begin, end], fill = linecolor)
def gene_code(save_path,filename):
? ? width,height = size #寬和高
? ? image = Image.new('RGBA',(width,height),bgcolor) #創(chuàng)建圖片
? ? font = ImageFont.truetype(font_path,25) #驗(yàn)證碼的字體和字體大小
? ? #font = ImageFont.truetype(25) #驗(yàn)證碼的字體和字體大小
? ? draw = ImageDraw.Draw(image) #創(chuàng)建畫筆
? ? #text = "我是中國人" #生成字符串
? ? text = gen_text() #生成字符串
? ? print(text)
? ? font_width, font_height = font.getsize(text)
? ? draw.text(((width - font_width) / number, (height - font_height) / number),text,\
? ? ? ? font= font,fill=fontcolor) #填充字符串
? ? if draw_line:
? ? ? ? gene_line(draw, width, height)
? ? ? ? gene_line(draw, width, height)
? ? ? ? gene_line(draw, width, height)
? ? ? ? gene_line(draw, width, height)
? ? image = image.transform((width + 20, height +10), Image.AFFINE, (1, -0.3, 0, -0.1, 1, 0), Image.BILINEAR)? # 創(chuàng)建扭曲
? ? image = image.filter(ImageFilter.EDGE_ENHANCE_MORE)? # 濾鏡鳞滨,邊界加強(qiáng)
? ? image.save('%s/%s.png' %(save_path,filename))? # 保存驗(yàn)證碼圖片
? ? print("savepath:",save_path)
? ? return text
if __name__ == "__main__":
? ? gene_code('/tmp','test') #會(huì)把生成的圖片存成/tmp/test.png
隨機(jī)驗(yàn)證碼代碼
需要安裝python?Pillow-1.7.8庫,先安裝依賴
#?yum install -y libjpeg-devel freetype-devel libpng-devel
不然直接安裝python包蟆淀,生成圖片會(huì)報(bào)錯(cuò)
參考網(wǎng)址:https://www.cnblogs.com/alex3714/articles/6662365.html