1.优化Base64算法,增加异常捕获
2.调整auth所有接口为base64加密存储 3.修复无法获取token的问题
This commit is contained in:
parent
32e82441c2
commit
7bf39c83bd
@ -1,3 +1,6 @@
|
||||
import re
|
||||
|
||||
|
||||
class CaesarCypherClass:
|
||||
"""
|
||||
恺撒密码,提供以恺撒密码方法进行加密及解密的方法,加密方法使用CaesarEncode()函数,解密方法使用CaesarDecode()函数
|
||||
@ -51,21 +54,49 @@ class Base64CypherClass:
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""
|
||||
Base64类初始化函数
|
||||
:param args:
|
||||
:param kwargs:
|
||||
"""
|
||||
import importlib
|
||||
self.base64 = importlib.import_module('base64')
|
||||
self.os = importlib.import_module('os')
|
||||
self.time = importlib.import_module('time')
|
||||
self.re = importlib.import_module('re')
|
||||
|
||||
@staticmethod
|
||||
def base64_encode_str(self, s):
|
||||
"""
|
||||
Base64字符串加密
|
||||
:param self:
|
||||
:param s: 要加密的字符串
|
||||
:return: 加密后的字符串
|
||||
"""
|
||||
return self.base64.b64encode(s.encode('utf-8'))
|
||||
|
||||
@staticmethod
|
||||
def base64_decode_str(self, s):
|
||||
return self.base64.b64decode(s).decode('utf-8')
|
||||
"""
|
||||
Base64字符串解密,解密前先判断是否为Base64加密方式
|
||||
:param self:
|
||||
:param s: 要解密的字符串
|
||||
:return: 解密后的字符串
|
||||
"""
|
||||
try:
|
||||
self.base64.b64decode(s)
|
||||
return self.base64.b64decode(s).decode('utf-8')
|
||||
except Exception as e:
|
||||
return f"base64解密失败,请确定加密方式是否正确。错误信息:{e}"
|
||||
|
||||
@staticmethod
|
||||
def base64_encode_pic(self, pic):
|
||||
"""
|
||||
Base64加密图片,路径不存在则返回"图片路径不存在"
|
||||
:param self:
|
||||
:param pic: 要加密的图片路径
|
||||
:return: 返回加密的base64字符
|
||||
"""
|
||||
if self.os.path.exists(pic):
|
||||
with open(pic, 'rb') as f:
|
||||
read_pic = open(pic, 'rb')
|
||||
@ -77,9 +108,19 @@ class Base64CypherClass:
|
||||
|
||||
@staticmethod
|
||||
def base64_decode_pic(self, pic_bs64):
|
||||
if self.os.path.exists(f"upload/temp/pic{int(self.time.time())}.jpg"):
|
||||
self.os.remove(f"upload/temp/pic{int(self.time.time())}.jpg")
|
||||
with open(f"upload/temp/pic{int(self.time.time())}.jpg", 'wb') as f:
|
||||
"""
|
||||
Base64解密图片
|
||||
:param self:
|
||||
:param pic_bs64:
|
||||
:return: 返回图片路径
|
||||
"""
|
||||
pic_path = f"upload/temp/pic{int(self.time.time())}"
|
||||
if self.os.path.exists(f"{pic_path}.jpg"):
|
||||
self.os.remove(f"{pic_path}.jpg")
|
||||
elif not self.os.path.exists("upload/temp/pic"):
|
||||
self.os.path.mkdir("upload/temp/pic")
|
||||
|
||||
with open(f"{pic_path}.jpg", 'wb') as f:
|
||||
f.write(self.base64.b64decode(pic_bs64))
|
||||
|
||||
return 'upload/temp/pic.jpg'
|
||||
return f"{pic_path}.jpg"
|
||||
|
@ -2,7 +2,7 @@ from django.urls import path
|
||||
from apps.auth import views
|
||||
|
||||
urlpatterns = [
|
||||
path("gettoken/", views.get_token, name="getToken"),
|
||||
path("gettoken/", views.gettoken, name="getToken"),
|
||||
path("searchuser/", views.search_user, name="searchuser"),
|
||||
path("adduser/", views.add_user, name="addUser"),
|
||||
path("login/", views.login_user, name="loginUser"),
|
||||
|
@ -1,15 +1,19 @@
|
||||
import binascii
|
||||
import json, datetime, base64
|
||||
import json, datetime
|
||||
from django.shortcuts import HttpResponse
|
||||
from django.middleware.csrf import get_token
|
||||
from django.views.decorators.http import require_GET, require_POST
|
||||
from apps.auth import models as auth_models
|
||||
from django.contrib.auth.hashers import make_password, check_password
|
||||
from apps.api.common import CaesarCypherClass, Base64CypherClass
|
||||
|
||||
|
||||
# Create your views here.
|
||||
base64 = Base64CypherClass()
|
||||
|
||||
|
||||
@require_GET
|
||||
def get_token(request):
|
||||
def gettoken(request):
|
||||
"""
|
||||
获取token
|
||||
:param request:
|
||||
@ -26,12 +30,12 @@ def search_user(request):
|
||||
:param request:
|
||||
:return:
|
||||
"""
|
||||
username = request.POST.get('username')
|
||||
username = base64.base64_encode_str(base64, request.POST.get("username")).decode('utf-8')
|
||||
user = auth_models.User.objects.filter(username=username)
|
||||
if user.exists():
|
||||
return HttpResponse("用户名已存在")
|
||||
else:
|
||||
return HttpResponse(True)
|
||||
else:
|
||||
return HttpResponse(False)
|
||||
|
||||
|
||||
@require_POST
|
||||
@ -42,11 +46,12 @@ def add_user(request):
|
||||
:return: 注册结果
|
||||
"""
|
||||
try:
|
||||
username = request.POST.get("username")
|
||||
pwd_base64 = base64.b64encode(request.POST.get("pwd"))
|
||||
username = base64.base64_encode_str(base64, request.POST.get("username")).decode('utf-8')
|
||||
pwd_base64 = base64.base64_decode_str(base64, request.POST.get("pwd"))
|
||||
print(username, pwd_base64)
|
||||
pwd = make_password(pwd_base64)
|
||||
email = request.POST.get("email")
|
||||
phone = request.POST.get("phone")
|
||||
email = base64.base64_encode_str(base64, request.POST.get("email")).decode('utf-8')
|
||||
phone = base64.base64_encode_str(base64, request.POST.get("phone")).decode('utf-8')
|
||||
create_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
last_login_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
print(username, pwd, email, phone)
|
||||
@ -73,8 +78,8 @@ def login_user(request):
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
username = request.POST.get("username")
|
||||
pwd_input = base64.b64decode(request.POST.get("pwd"))
|
||||
username = base64.base64_encode_str(base64, request.POST.get("username")).decode('utf-8')
|
||||
pwd_input = base64.base64_decode_str(base64, request.POST.get("pwd"))
|
||||
pwd_made = auth_models.User.objects.filter(username=username).first().pwd
|
||||
pwd = check_password(pwd_input, pwd_made)
|
||||
if pwd:
|
||||
|
@ -6,9 +6,15 @@ from apps.api.common import CaesarCypherClass, Base64CypherClass
|
||||
def home(request):
|
||||
bs64 = Base64CypherClass()
|
||||
s = request.GET.get('s')
|
||||
s_encode = bs64.base64_encode_pic(bs64, s)
|
||||
if s_encode != "图片路径不存在":
|
||||
s_decode = bs64.base64_decode_pic(bs64, s_encode)
|
||||
else:
|
||||
s_decode = ''
|
||||
print(s)
|
||||
s_encode = bs64.base64_encode_str(bs64,s)
|
||||
print(s_encode)
|
||||
s_decode = bs64.base64_decode_str(bs64,s)
|
||||
|
||||
# s_encode = bs64.base64_encode_pic(bs64, s)
|
||||
# if s_encode != "图片路径不存在":
|
||||
# s_decode = bs64.base64_decode_pic(bs64, s_encode)
|
||||
# else:
|
||||
# s_decode = ''
|
||||
# return HttpResponse(f"解密:{s_decode}")
|
||||
return HttpResponse(f"加密:{s_encode}\n解密:{s_decode}")
|
||||
|
BIN
data/db.sqlite3
BIN
data/db.sqlite3
Binary file not shown.
Loading…
Reference in New Issue
Block a user