码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 用go设计开发一个自己的轻量级登录库/框架吧


    合集 - token-go(5)
    1.用go设计开发一个自己的轻量级登录库/框架吧(项目维护篇)05-11
    2.用go设计开发一个自己的轻量级登录库/框架吧05-06
    3.用go设计开发一个自己的轻量级登录库/框架吧(业务篇)05-134.用go设计开发一个自己的轻量级登录库/框架吧(拓展篇)05-215.用go封装一下封禁功能05-25
    收起

    用go设计开发一个自己的轻量级登录库/框架吧

    几乎每个项目都会有登录,退出等用户功能,而登录又不单仅仅是登录,我们要考虑很多东西。

    token该怎么生成?生成什么样的?

    是在Cookie存token还是请求头存token?读取的时候怎么读取?

    允许同一个账号被多次登录吗?多次登录他们的token是一样的?还是不一样的?

    登录也有可能分成管理员登录,用户登录等多种登录类型

    我们要做的就是把这些东西封装到一起,然后能更方便的使用

    而完成这些最难的就是如何设计架构了,其实要简单的封装一下并不难,本篇要讲的就是如何进行架构的设计了。

    源码:weloe/token-go: a light login library (github.com)

    Enforcer

    我们可以抽象出一个供外部调用的执行器,它包括以下几个部分

    token-go/enforcer.go at master · weloe/token-go (github.com)

    type Enforcer struct {
        // 从配置文件读取配置需要
    	conf         string
        // 登录类型
    	loginType    string
    	config       config.TokenConfig
        // 生成token的函数
    	generateFunc model.GenerateTokenFunc
        // 用于存储数据
    	adapter      persist.Adapter
        // 监听器
    	watcher      persist.Watcher
        // 用于记录日志
    	logger       log.Logger
    }
    

    执行器的接口,包含供外部调用的方法

    token-go/enforcer_interface.go at master · weloe/token-go · GitHub

    type IEnforcer interface {
    	Login(id string, ctx ctx.Context) (string, error)
    	LoginByModel(id string, loginModel *model.Login, ctx ctx.Context) (string, error)
    	Logout(ctx ctx.Context) error
    	IsLogin(ctx ctx.Context) (bool, error)
    	IsLoginById(id string) (bool, error)
    	GetLoginId(ctx ctx.Context) (string, error)
    
    	Replaced(id string, device string) error
    	Kickout(id string, device string) error
    
    	GetRequestToken(ctx ctx.Context) string
    
    	SetType(t string)
    	GetType() string
    	GetAdapter() persist.Adapter
    	SetAdapter(adapter persist.Adapter)
    	SetWatcher(watcher persist.Watcher)
    	SetLogger(logger log.Logger)
    	EnableLog()
    	IsLogEnable() bool
    	GetSession(id string) *model.Session
    	SetSession(id string, session *model.Session, timeout int64) error
    }
    
    

    Config

    首先就是根据需求抽象出配置信息

    一个是cookie的配置

    token-go/cookie.go at master · weloe/token-go · GitHub

    type CookieConfig struct {
    	Domain   string
    	Path     string
    	Secure   bool
    	HttpOnly bool
    	SameSite string
    }
    

    一个是token的配置

    token-go/token.go at master · weloe/token-go · GitHub

    type TokenConfig struct {
       // TokenStyle
       // uuid | uuid-simple | random-string32 | random-string64 | random-string128
       TokenStyle string
        
       TokenName   string
    
       Timeout int64
    
       // 允许多次登录
       IsConcurrent bool
       // 多次登录共享一个token
       IsShare bool
       // If (IsConcurrent == true && IsShare == false)才支持配置
       // If IsConcurrent == -1, 不检查登录数量
       MaxLoginCount int16
    
       // 读取token的方式
       IsReadBody   bool
       IsReadHeader bool
       IsReadCookie bool
    
       // 是否把token写入响应头
       IsWriteHeader bool
    
       CookieConfig *CookieConfig
    }
    

    Adapter

    adapter是底层用来存储数据的结构,为了兼容不同的实现(不同的存储方式),设计成一个接口。

    token-go/adapter.go at master · weloe/token-go · GitHub

    type Adapter interface {
    
    	// GetStr string operate string value
    	GetStr(key string) string
    	// SetStr set store value and timeout
    	SetStr(key string, value string, timeout int64) error
    	// UpdateStr only update value
    	UpdateStr(key string, value string) error
    	// DeleteStr delete string value
    	DeleteStr(key string) error
    	// GetStrTimeout get expire
    	GetStrTimeout(key string) int64
    	// UpdateStrTimeout update expire time
    	UpdateStrTimeout(key string, timeout int64) error
    
    	// Get get interface{}
    	Get(key string) interface{}
    	// Set store interface{}
    	Set(key string, value interface{}, timeout int64) error
    	// Update only update interface{} value
    	Update(key string, value interface{}) error
    	// Delete delete interface{} value
    	Delete(key string) error
    	// GetTimeout get expire
    	GetTimeout(key string) int64
    	// UpdateTimeout update timeout
    	UpdateTimeout(key string, timeout int64) error
    }
    
    

    Context

    我们需要从请求读取token,可能也需要写出token,因此需要兼容不同的web上下文,我们需要设计一个Context接口

    token-go/context.go at master · weloe/token-go · GitHub

    type Context interface {
    	Request() Request
    	Response() Response
    	ReqStorage() ReqStorage
    	MatchPath(pattern string, path string) bool
    	IsValidContext() bool
    }
    

    Watcher

    监听器,用于在一些事件发生的时候进行一些其他操作。

    token-go/watcher.go at master · weloe/token-go · GitHub

    // Watcher event watcher
    type Watcher interface {
    	// Login called after login
    	Login(loginType string, id interface{}, tokenValue string, loginModel *model.Login)
    	// Logout called after logout
    	Logout(loginType string, id interface{}, tokenValue string)
    }
    

    Logger

    Logger,用于记录日志,方便debug等等,需要设计成可以自由开启关闭。

    token-go/logger.go at master · weloe/token-go · GitHub

    type Logger interface {
    	persist.Watcher
    
    	// Enable turn on or off
    	Enable(bool bool)
    
    	// IsEnabled return if logger is enabled
    	IsEnabled() bool
    }
    

    到此,项目的大致的结构就设计完成,下一篇会讲讲本业务的具体实现

  • 相关阅读:
    牛客前端刷题(五)—— CSS相关概念
    QT+OSG/osgEarth编译之四十七:osgFX+Qt编译(一套代码、一套框架,跨平台编译,版本:OSG-3.6.5核心库osgFX)
    基于C语言开发实现的一个用户级线程库
    老卫带你学---leetcode刷题(47. 全排列 II)
    线性思维和系统思维
    Workfine新手入门:数据规范之列表
    如何参与开源项目 - 细说 GitHub 上的 PR 全过程
    我在windows环境下的YOLOV3环境搭建过程
    [信息抽取]基于ERNIE3.0的多对多信息抽取算法:属性关系抽取
    谷歌学术镜像mark
  • 原文地址:https://www.cnblogs.com/weloe/p/17378215.html
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号