当前位置:网站首页>Flask login implementation
Flask login implementation
2022-07-27 08:33:00 【pink_ Pig___】
1. Generate token
1. Need to install jwt
pip install pyjwt
2. encapsulation token Generation method
import time
import jwt # pip install pyjwt
from flask import current_app
class JwtTool():
'''
jwt management
'''
def create(self,payload,ex=3600):
'''
Generate jwt
:param payload: load
:return: string jwt
'''
payload["exp"] = int(time.time())+ ex # Default expiration date 1 Hours
secret_key = current_app.config.get("SECRET_KEY")
token = jwt.encode(payload,secret_key,algorithm="HS256")
return token
def valid(self,token):
'''
check jwt
:param token: JWT
:return: return payload
'''
secret_key = current_app.config.get("SECRET_KEY")
try:
payload = jwt.decode(token, secret_key, algorithms="HS256")
return payload
except Exception as e:
print(e)
return False
settings/secretconfig.py
class SecretConfig():
RL_ACC_ID = "8a216da88185a9c001818a7781bc0148"
RL_ACC_TOKEN = "c98c0ae0fbc54e4da1a070db3050e529"
RL_APP_ID = "8a216da88185a9c001818a7782bf014f"
RL_TID = "1"
REDIS_HOST = "127.0.0.1"
REDIS_PORT =6379
among SECRET_KEY It is obtained from the configuration item in, and the configuration item needs to be written
SecretConfig Is the configuration required for inheritance and SMS verification code
settings/secret.py
from settings.secret import SecretConfig
class DefaultConfig(SecretConfig):
SECRET_KEY = "asdaweq"
SQLALCHEMY_DATABASE_URI=""
SQLALCHEMY_TRACK_MODIFICATIONS=False
SQLALCHEMY_ECHO=True
JSON_AS_ASCII=False
SMS_TIME = 5
class DevConfig(DefaultConfig):
SQLALCHEMY_DATABASE_URI="mysql+pymysql://root:[email protected]:3306/h2111anews"
2. Send SMS verification code
import time
import jwt # pip install pyjwt
from flask import current_app
class JwtTool():
'''
jwt management
'''
def create(self,payload,ex=3600):
'''
Generate jwt
:param payload: load
:return: string jwt
'''
payload["exp"] = int(time.time())+ ex # Default expiration date 1 Hours
secret_key = current_app.config.get("SECRET_KEY")
token = jwt.encode(payload,secret_key,algorithm="HS256")
return token
def valid(self,token):
'''
check jwt
:param token: JWT
:return: return payload
'''
secret_key = current_app.config.get("SECRET_KEY")
try:
payload = jwt.decode(token, secret_key, algorithms="HS256")
return payload
except Exception as e:
print(e)
return False
Use SMS to import such calls
边栏推荐
- How to view instances of software objects in QSIM?
- XxE & XML vulnerability
- Flask one to many database creation, basic addition, deletion, modification and query
- 如何在qsim查看软件对象的实例?
- Explain cache consistency and memory barrier
- Luogu super Mary game
- Realize SKU management in the background
- Use of "PHP Basics" Boolean
- Create a project to realize login and registration, generate JWT, and send verification code
- Solution of database migration error
猜你喜欢

Notes in "PHP Basics" PHP

vCenter7.0管理Esxi7.0主机

I drew a Gu ailing with characters!

Node installation and debugging

Day4 --- flask blueprint and rest ful

Element display mode: block level, inline, inline block, nesting specification, display mode conversion

海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态

Plato farm is expected to further expand its ecosystem through elephant swap

Cache consistency and memory barrier

Process control - Branch
随机推荐
Day3 -- flag state holding, exception handling and request hook
vCenter7.0管理Esxi7.0主机
如何卸载--奇安信安全终端管理系统
Realization of backstage brand management function
Luogu super Mary game
Cache consistency and memory barrier
Luogu Taotao picks apples
开怀一笑
How to uninstall -- Qianxin secure terminal management system
Explain cache consistency and memory barrier
Minio 安装与使用
Connection failed during installation of ros2 [ip: 91.189.91.39 80]
P7 Day1 get to know the flask framework
Virtual machine cloning
无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
虚拟机克隆
永久设置source的方法
Introduction, installation and use of netdata performance monitoring tool
JS basic exercises
redis配置文件下载