当前位置:网站首页>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 merge multiple columns in an excel table into one column
- Initial summary of flask framework creation project
- Introduction to depth first search (DFS)
- CMD command and NPM command
- 永久设置source的方法
- Implementation of registration function
- After downloading URL loader and specifying the size of the image with limit, the image will not be displayed
- Alibaba cloud international receipt message introduction and configuration process
- Hundreds of people participated. What are these people talking about in the opengauss open source community?
- Installation and use of beef XSS
猜你喜欢

无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。

Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?

Background image related applications - full, adaptive
![[MRCTF2020]PYWebsite 1](/img/d4/2d9cd06abd7188add668cde77d3075.png)
[MRCTF2020]PYWebsite 1

Login to homepage function implementation

Attack and defense World Lottery

Attack and defense world MFW

Idea remote debugging

regular expression

MCDF top level verification scheme
随机推荐
Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
信息化项目风险控制与应用
Idea remote debugging
Make a game by yourself with pyGame 01
All in one 1353 -- expression bracket matching (stack)
Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
You may need an additional loader to handle the result of these loaders.
How to view instances of software objects in QSIM?
Forced login, seven cattle cloud upload pictures
arguments
Chapter 2 foreground data display
One book 1201 Fibonacci sequence
"PHP Basics" PHP statements and statement blocks
永久设置source的方法
Hundreds of people participated. What are these people talking about in the opengauss open source community?
Realize SPU management in the background
Map structure
我用字符画出了一个谷爱凌!
JS basic knowledge - daily learning summary ①
Block, there is a gap between the block elements in the row