flask-security快速入门
时间: 2023-10-05 20:14:01 浏览: 63
Flask-Security是一个基于 Flask 的安全扩展,可以帮助你快速实现常见的用户认证和授权功能。下面是一个快速入门指南:
1. 安装 Flask-Security
使用 pip 安装 Flask-Security:
```
pip install flask-security
```
2. 初始化 Flask-Security
在 Flask 应用中初始化 Flask-Security:
```python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_security import Security, SQLAlchemyUserDatastore
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///myapp.db'
app.config['SECRET_KEY'] = 'super-secret'
db = SQLAlchemy(app)
from myapp.models import User, Role
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
```
在这个例子中,我们使用 SQLAlchemyUserDatastore 将 Flask-Security 和 SQLAlchemy 集成在一起,同时使用 Security 类来初始化 Flask-Security。
3. 创建用户模型
创建一个 User 模型来存储用户信息:
```python
from flask_security import UserMixin, RoleMixin
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
roles = db.relationship('Role', secondary='user_roles',
backref=db.backref('users', lazy='dynamic'))
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))
```
在这里,我们使用 UserMixin 和 RoleMixin 来添加一些常见的用户和角色属性。
4. 创建用户和角色关系模型
创建一个 user_roles 表来存储用户和角色之间的关系:
```python
user_roles = db.Table('user_roles',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
```
5. 配置 Flask-Security
在 Flask 应用中配置 Flask-Security:
```python
app.config['SECURITY_PASSWORD_SALT'] = 'super-secret-salt'
app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_SEND_REGISTER_EMAIL'] = False
app.config['SECURITY_USER_IDENTITY_ATTRIBUTES'] = ['email']
```
在这个例子中,我们为密码添加了一个盐值,启用了用户注册功能,并且关闭了注册时发送电子邮件的功能。
6. 添加用户认证和授权路由
添加用户认证和授权路由到 Flask 应用中:
```python
from flask import render_template, redirect, url_for
from flask_security import login_required, current_user
@app.route('/')
def home():
return render_template('home.html')
@app.route('/dashboard')
@login_required
def dashboard():
return render_template('dashboard.html', user=current_user)
```
在这里,我们使用 login_required 装饰器来保护 dashboard 路由,只有已登录用户才能访问该路由。
7. 创建模板
创建一个 home.html 模板来显示主页:
```html
<h1>Welcome to My App</h1>
{% if current_user.is_authenticated() %}
<p>Hello {{ current_user.email }}!</p>
<p><a href="{{ url_for('security.logout') }}">Logout</a></p>
{% else %}
<p><a href="{{ url_for('security.login') }}">Login</a></p>
{% endif %}
```
创建一个 dashboard.html 模板来显示仪表盘:
```html
<h1>Dashboard</h1>
<p>Hello {{ user.email }}!</p>
<p><a href="{{ url_for('security.logout') }}">Logout</a></p>
```
在这里,我们使用 current_user 对象来检查当前用户是否已经登录,并使用 url_for 方法来生成登出链接。
这就是 Flask-Security 的快速入门指南。有了这个基础,你可以探索 Flask-Security 的更多功能,例如密码重置、电子邮件确认、角色管理等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)