Spaces:
Runtime error
Runtime error
远兮
commited on
Commit
·
d44aea5
1
Parent(s):
dd2231b
添加用户登录态校验
Browse files- redis/secret_key.py +4 -0
- redis/test_user_redis.py +35 -13
redis/secret_key.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import secrets
|
2 |
+
|
3 |
+
secret_key = secrets.token_hex(16) # 生成一个长度为16字节的随机十六进制字符串
|
4 |
+
print(secret_key)
|
redis/test_user_redis.py
CHANGED
@@ -1,29 +1,29 @@
|
|
1 |
import json
|
2 |
import random
|
3 |
import string
|
4 |
-
from flask import Flask, request, jsonify
|
5 |
from redis import Redis
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
-
|
9 |
-
|
10 |
-
#
|
|
|
11 |
|
12 |
|
|
|
13 |
def generate_verification_code():
|
14 |
# code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
|
15 |
code = ''.join(random.choices(string.digits, k=6))
|
16 |
return code
|
17 |
|
18 |
-
# 发送验证码到用户邮箱(这里只是模拟发送过程)
|
19 |
-
|
20 |
|
|
|
21 |
def send_verification_code(email, code):
|
22 |
print(f'Sending verification code {code} to {email}...')
|
23 |
|
24 |
-
# 用户请求发送验证码
|
25 |
-
|
26 |
|
|
|
27 |
@app.route('/send_verification_code', methods=['POST'])
|
28 |
def send_verification_code_endpoint():
|
29 |
# 从请求中获取邮箱地址
|
@@ -40,9 +40,8 @@ def send_verification_code_endpoint():
|
|
40 |
|
41 |
return jsonify({'message': 'Verification code sent'})
|
42 |
|
43 |
-
# 用户注册
|
44 |
-
|
45 |
|
|
|
46 |
@app.route('/register', methods=['POST'])
|
47 |
def register():
|
48 |
# 从请求中获取注册信息
|
@@ -55,7 +54,7 @@ def register():
|
|
55 |
stored_code = redis.get(email)
|
56 |
if stored_code is None or verification_code != stored_code.decode('utf-8'):
|
57 |
return jsonify({'message': 'Invalid verification code'}), 400
|
58 |
-
|
59 |
# 检查用户名是否已被注册
|
60 |
if redis.hexists('users', username):
|
61 |
return jsonify({'message': 'Username already exists'}), 400
|
@@ -69,9 +68,8 @@ def register():
|
|
69 |
|
70 |
return jsonify({'message': 'Registration successful'})
|
71 |
|
72 |
-
# 用户登录
|
73 |
-
|
74 |
|
|
|
75 |
@app.route('/login', methods=['POST'])
|
76 |
def login():
|
77 |
# 从请求中获取登录信息
|
@@ -87,8 +85,32 @@ def login():
|
|
87 |
if password != eval(user_data)['password']:
|
88 |
return jsonify({'message': 'Invalid password'}), 400
|
89 |
|
|
|
|
|
|
|
90 |
return jsonify({'message': 'Login successful'})
|
91 |
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
if __name__ == '__main__':
|
94 |
app.run(debug=True)
|
|
|
1 |
import json
|
2 |
import random
|
3 |
import string
|
4 |
+
from flask import Flask, request, jsonify, session
|
5 |
from redis import Redis
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
+
app.secret_key = '333888'
|
9 |
+
redis = Redis(host='192.168.3.229', port=6379, password='lizhen-redis')
|
10 |
+
# redis = Redis(host='10.254.13.87', port=6379)
|
11 |
+
# redis = Redis(host='localhost', port=6379)
|
12 |
|
13 |
|
14 |
+
# 生成验证码
|
15 |
def generate_verification_code():
|
16 |
# code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
|
17 |
code = ''.join(random.choices(string.digits, k=6))
|
18 |
return code
|
19 |
|
|
|
|
|
20 |
|
21 |
+
# 发送验证码到用户邮箱(这里只是模拟发送过程)
|
22 |
def send_verification_code(email, code):
|
23 |
print(f'Sending verification code {code} to {email}...')
|
24 |
|
|
|
|
|
25 |
|
26 |
+
# 用户请求发送验证码
|
27 |
@app.route('/send_verification_code', methods=['POST'])
|
28 |
def send_verification_code_endpoint():
|
29 |
# 从请求中获取邮箱地址
|
|
|
40 |
|
41 |
return jsonify({'message': 'Verification code sent'})
|
42 |
|
|
|
|
|
43 |
|
44 |
+
# 用户注册
|
45 |
@app.route('/register', methods=['POST'])
|
46 |
def register():
|
47 |
# 从请求中获取注册信息
|
|
|
54 |
stored_code = redis.get(email)
|
55 |
if stored_code is None or verification_code != stored_code.decode('utf-8'):
|
56 |
return jsonify({'message': 'Invalid verification code'}), 400
|
57 |
+
|
58 |
# 检查用户名是否已被注册
|
59 |
if redis.hexists('users', username):
|
60 |
return jsonify({'message': 'Username already exists'}), 400
|
|
|
68 |
|
69 |
return jsonify({'message': 'Registration successful'})
|
70 |
|
|
|
|
|
71 |
|
72 |
+
# 用户登录
|
73 |
@app.route('/login', methods=['POST'])
|
74 |
def login():
|
75 |
# 从请求中获取登录信息
|
|
|
85 |
if password != eval(user_data)['password']:
|
86 |
return jsonify({'message': 'Invalid password'}), 400
|
87 |
|
88 |
+
# 登录验证通过,将用户信息存储到会话中
|
89 |
+
session['username'] = request.json.get('username')
|
90 |
+
|
91 |
return jsonify({'message': 'Login successful'})
|
92 |
|
93 |
|
94 |
+
# 需要验证登录状态的接口
|
95 |
+
@app.route('/protected', methods=['GET'])
|
96 |
+
def protected():
|
97 |
+
# 检查会话中的用户信息
|
98 |
+
if 'username' in session:
|
99 |
+
username = session['username']
|
100 |
+
# 其他业务逻辑...
|
101 |
+
return jsonify({'message': f'Hello, {username}! This is a protected endpoint.'})
|
102 |
+
|
103 |
+
# 如果用户未登录,则返回未授权的响应
|
104 |
+
return jsonify({'message': 'Unauthorized'}), 401
|
105 |
+
|
106 |
+
|
107 |
+
# 用户注销
|
108 |
+
@app.route('/logout', methods=['POST'])
|
109 |
+
def logout():
|
110 |
+
# 清除会话中的用户信息
|
111 |
+
session.pop('username', None)
|
112 |
+
return jsonify({'message': 'Logout successful'})
|
113 |
+
|
114 |
+
|
115 |
if __name__ == '__main__':
|
116 |
app.run(debug=True)
|