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

这篇具有很好参考价值的文章主要介绍了用go设计开发一个自己的轻量级登录库/框架吧。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

用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
}

到此,项目的大致的结构就设计完成,下一篇会讲讲本业务的具体实现文章来源地址https://www.toymoban.com/news/detail-435084.html

到了这里,关于用go设计开发一个自己的轻量级登录库/框架吧的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • go-carbon 2.2.12 版本发布, 轻量级、语义化、对开发者友好的 Golang 时间处理库

    carbon 是一个轻量级、语义化、对开发者友好的 golang 时间处理库,支持链式调用。 目前已被 awesome-go 收录,如果您觉得不错,请给个 star 吧 github.com/golang-module/carbon gitee.com/golang-module/carbon 安装使用 Golang 版本大于等于1.16 Golang 版本小于1.16 更新日志 增加对荷兰语的支持 测试

    2024年02月06日
    浏览(32)
  • go-carbon v2.3.5 发布,轻量级、语义化、对开发者友好的 golang 时间处理库

    carbon 是一个轻量级、语义化、对开发者友好的 golang 时间处理库,支持链式调用。 目前已被 awesome-go 收录,如果您觉得不错,请给个 star 吧 github.com/golang-module/carbon gitee.com/golang-module/carbon 安装使用 Golang 版本大于等于 1.16 Golang 版本小于 1.16 更新日志 Default 结构体新增 Locale

    2024年02月01日
    浏览(34)
  • go-carbon v2.3.6 发布,轻量级、语义化、对开发者友好的 golang 时间处理库

    carbon 是一个轻量级、语义化、对开发者友好的 golang 时间处理库,支持链式调用。 目前已被 awesome-go 收录,如果您觉得不错,请给个 star 吧 github.com/golang-module/carbon gitee.com/golang-module/carbon 安装使用 Golang 版本大于等于 1.16 Golang 版本小于 1.16 更新日志 将日历提取出来作为独立

    2024年01月24日
    浏览(47)
  • 使用Go语言打造轻量级Web框架

    前言 Web框架是Web开发中不可或缺的组件。它们的主要目标是抽象出HTTP请求和响应的细节,使开发人员可以更专注于业务逻辑的实现。在本篇文章中,我们将使用Go语言实现一个简单的Web框架,类似于Gin框架。 功能 我们的Web框架需要实现以下功能: 路由:处理HTTP请求的路由

    2023年04月08日
    浏览(29)
  • 特制自己的ChatGPT:多接口统一的轻量级LLM-IFT平台

    ©PaperWeekly 原创 · 作者 |  佀庆一 单位 |  中科院信息工程研究所 研究方向 |  视觉问答 项目简称: Alpaca-CoT(当羊驼遇上思维链) 项目标题: Alpaca-CoT: An Instruction Fine-Tuning Platform with Instruction Data Collection and Unified Large Language Models Interface 项目链接: https://github.com/PhoebusSi

    2024年02月04日
    浏览(24)
  • CasaOS一个轻量级的家庭云系统

    简介 CasaOS是一款轻量级的家庭云系统,基于Docker安装部署,支持pc和手机,可玩性非常高,万物皆可以打成docker镜像后都可以安装。 你要你拥有一台电脑装上ubuntu你就能做all in one ,nas全家桶。安装简单,但是受网速影响至少要一个小时。 准备工作 一台装有docker的ubantu系统

    2024年02月05日
    浏览(35)
  • C#轻量级日志功能(只有一个类)

    最近在开发基于.net6的一个数据监控软件,使用其它开源log库都有点麻烦,就想着对Console.WriteLine()方法重定向到文件,非常方便的实现日志记录功能,同时也不影响之前的代码结构。 软件开始的地方要设置该重定向:

    2024年01月21日
    浏览(44)
  • 轻量灵动: 革新轻量级服务开发

    从 JDK 8 升级到 JDK 17 可以让你的应用程序受益于新的功能、性能改进和安全增强。下面是一些 JDK 8 升级到 JDK 17 的最佳实战: 1.1、确定升级的必要性:首先,你需要评估你的应用程序是否需要升级到 JDK 17。查看 JDK 17 的新特性、改进和修复的 bug,以确定它们对你的应用程序

    2024年02月07日
    浏览(30)
  • Mainflux IoT:Go语言轻量级开源物联网平台,支持HTTP、MQTT、WebSocket、CoAP协议

    Mainflux是一个由法国的创业公司开发并维护的 安全、可扩展 的开源物联网平台,使用 Go语言开发、采用微服务的框架。Mainflux支持多种接入设备,包括设备、用户、APP;支持多种协议,包括HTTP、MQTT、WebSocket、CoAP,并支持他们之间的协议互转。 Mainflux的南向接口连接设备,北

    2024年02月01日
    浏览(90)
  • 教你使用PHP实现一个轻量级HTML模板引擎

    🏆作者简介,黑夜开发者,全栈领域新星创作者✌,2023年6月csdn上海赛道top4。多年电商行业从业经验,对系统架构,数据分析处理等大规模应用场景有丰富经验。 🏆本文已收录于PHP专栏:PHP进阶实战教程。 🏆另有专栏PHP入门基础教程,希望各位大佬多多支持❤️。 在 W

    2024年02月15日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包