[小程序]基于token的权鉴测试

一、服务器配置

        服务器基于flask,需要额外安装flask_jwt_extended

from flask import Flask        #导入Flask包
from flask import request
from flask import jsonify      #用来返回json消息
from flask_jwt_extended import create_access_token, jwt_required, JWTManager, get_jwt_identity  #权鉴相关
app.config['JWT_SECRET_KEY'] = 'super-secret'   #初始化密钥
jwt = JWTManager(app)

users = {
    'admin': '123',
}

        定义一个POST接口供小程序端获取token 

@app.route('/login', methods=['POST'])
def login():
    account = request.json.get('account', None)
    psw = request.json.get('psw', None)
    if not account or not psw:
        return jsonify({"msg": "缺少用户名或密码"}), 400

    if account not in users:
        return jsonify({"msg": "未找到用户"}), 404

    if users[account] != psw:
        return jsonify({"msg": "用户名或密码错误"}), 401

    token = create_access_token(identity=account)
    return jsonify(token=token), 200

        定义一个需要token的GET请求来验证效果

@app.route('/protected', methods=['GET'])
@jwt_required()
def protected():
    current_user = get_jwt_identity()
    return jsonify(msg=current_user), 200

二、效果程序实现

        1.获取并存储token

login(e){
    var that = this
    wx.request({
            url:'请求链接',
            method:'POST',
            data:{            //POST账号密码
              account:that.data.account,
              psw:that.data.psw,
            },
            header: {'Content-Type': 'application/json'},
            success(res) {
              wx.setStorage({key:'token',data:res.data.token})    //存储token
            },
            fail(res){console.log(res)}
          });
  },

        大体思路是:使用POST指令向服务器提交账号和密码,并从服务器收到token字符串,然后将token字符串存在本地。

        2.发送带token的GET

privatePost(e){
    var that = this
    wx.request({
            url:'链接',
            method:'GET',
            header: {'Content-Type': 'application/json',
            'Authorization':'Bearer '+wx.getStorageSync('token')},    //构建token头
            success(res) {
              that.setData({msg:res.data.msg})    //显示返回信息
            },
            fail(res){console.log(res)}
          });
  },

        这段代码和普通GET指令唯一的区别在于其在header中设置了一个Authorization属性。如果使用Flask服务器请务必按照这个格式构建header。

        3.登出

        这个更简单了,直接把本地存储的token删了就好了

logout(e){
    wx.clearStorage('token')
  },

效果如下图:

相关推荐

  1. Vue登陆方案(token

    2024-01-22 14:54:02       25 阅读
  2. webaccess_token、AK/SK、session/cookie

    2024-01-22 14:54:02       24 阅读
  3. 基于SpringCloudGateway实现接口

    2024-01-22 14:54:02       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-22 14:54:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-22 14:54:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-22 14:54:02       82 阅读
  4. Python语言-面向对象

    2024-01-22 14:54:02       91 阅读

热门阅读

  1. JVM知识点总结

    2024-01-22 14:54:02       58 阅读
  2. spring mvc的HandlerInterceptor的原理以及使用场景

    2024-01-22 14:54:02       54 阅读
  3. Android:RecyclerView自由拖动item

    2024-01-22 14:54:02       58 阅读
  4. Ubuntu 22报错:PAM unable to dlopen(pam_tally2.so)

    2024-01-22 14:54:02       60 阅读
  5. QT 浏览器组件使用

    2024-01-22 14:54:02       55 阅读
  6. 二、docker的常用命令(持续补充img)

    2024-01-22 14:54:02       50 阅读
  7. SpringMVC使用步驟

    2024-01-22 14:54:02       57 阅读
  8. windows11+GPU1060安装强化学习环境之pytorch

    2024-01-22 14:54:02       56 阅读
  9. 【shell编程入门】正则表达式

    2024-01-22 14:54:02       56 阅读
  10. elemeentui el-table封装

    2024-01-22 14:54:02       46 阅读
  11. -交换机-

    2024-01-22 14:54:02       65 阅读