欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Golang開(kāi)發(fā)一個(gè)輕量級(jí)登錄庫(kù)/框架

 更新時(shí)間:2023年05月07日 09:31:27   作者:秋玻  
幾乎每個(gè)項(xiàng)目都會(huì)有登錄,退出等用戶功能,而登錄又不單僅僅是登錄,我們要考慮很多東西。所以本文就來(lái)用Golang開(kāi)發(fā)一個(gè)輕量級(jí)登錄庫(kù)/框架吧

幾乎每個(gè)項(xiàng)目都會(huì)有登錄,退出等用戶功能,而登錄又不單僅僅是登錄,我們要考慮很多東西。

token該怎么生成?生成什么樣的?

是在Cookie存token還是請(qǐng)求頭存token?讀取的時(shí)候怎么讀???

允許同一個(gè)賬號(hào)被多次登錄嗎?多次登錄他們的token是一樣的?還是不一樣的?

登錄也有可能分成管理員登錄,用戶登錄等多種登錄類(lèi)型

我們要做的就是把這些東西封裝到一起,然后能更方便的使用

而完成這些最難的就是如何設(shè)計(jì)架構(gòu)了,其實(shí)要簡(jiǎn)單的封裝一下并不難,本篇要講的就是如何進(jìn)行架構(gòu)的設(shè)計(jì)了。

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

1.Enforcer

我們可以抽象出一個(gè)供外部調(diào)用的執(zhí)行器,它包括以下幾個(gè)部分

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

type Enforcer struct {
    // 從配置文件讀取配置需要
    conf         string
    // 登錄類(lèi)型
    loginType    string
    config       config.TokenConfig
    // 生成token的函數(shù)
    generateFunc model.GenerateTokenFunc
    // 用于存儲(chǔ)數(shù)據(jù)
    adapter      persist.Adapter
    // 監(jiān)聽(tīng)器
    watcher      persist.Watcher
    // 用于記錄日志
    logger       log.Logger
}

執(zhí)行器的接口,包含供外部調(diào)用的方法

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

var _ IEnforcer = &Enforcer{}

type IEnforcer interface {
    
	Login(id string) (string, error)
	LoginByModel(id string, loginModel *model.Login) (string, error)
	Logout() error
	IsLogin() (bool, error)
	IsLoginById(id string) (bool, error)
	GetLoginId() (string, error)

	Replaced(id string, device string) error
	Kickout(id string, device string) error

	GetRequestToken() string

	SetType(t string)
	GetType() string
	SetContext(ctx ctx.Context)
	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
}

2.Config

首先就是根據(jù)需求抽象出配置信息

一個(gè)是cookie的配置

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

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

一個(gè)是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
   // 多次登錄共享一個(gè)token
   IsShare bool
   // If (IsConcurrent == true && IsShare == false)才支持配置
   // If IsConcurrent == -1, 不檢查登錄數(shù)量
   MaxLoginCount int16

   // 讀取token的方式
   IsReadBody   bool
   IsReadHeader bool
   IsReadCookie bool

   // 是否把token寫(xiě)入響應(yīng)頭
   IsWriteHeader bool

   CookieConfig *CookieConfig
}

3.Adapter

adapter是底層用來(lái)存儲(chǔ)數(shù)據(jù)的結(jié)構(gòu),為了兼容不同的實(shí)現(xiàn)(不同的存儲(chǔ)方式),設(shè)計(jì)成一個(gè)接口。

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
}

4.Context

我們需要從請(qǐng)求讀取token,可能也需要寫(xiě)出token,因此需要兼容不同的web上下文,我們需要設(shè)計(jì)一個(gè)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
}

5.Watcher

監(jiān)聽(tīng)器,用于在一些事件發(fā)生的時(shí)候進(jìn)行一些其他操作。

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

6.Logger

Logger,用于記錄日志,方便debug等等,需要設(shè)計(jì)成可以自由開(kāi)啟關(guān)閉。

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
}

到此,項(xiàng)目的大致的結(jié)構(gòu)就設(shè)計(jì)完成,下一篇會(huì)講講本業(yè)務(wù)的具體實(shí)現(xiàn)

到此這篇關(guān)于基于Golang開(kāi)發(fā)一個(gè)輕量級(jí)登錄庫(kù)/框架的文章就介紹到這了,更多相關(guān)Golang開(kāi)發(fā)登錄庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Golang交叉編譯之跨平臺(tái)編譯使用詳解

    Golang交叉編譯之跨平臺(tái)編譯使用詳解

    這篇文章主要為大家介紹了Golang交叉編譯之跨平臺(tái)編譯使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Go語(yǔ)言io?pipe源碼分析詳情

    Go語(yǔ)言io?pipe源碼分析詳情

    這篇文章主要介紹了Go語(yǔ)言io?pipe源碼分析詳情,pipe是一個(gè)適配器,用于連接Reader和Writer,pipe的方法不多,新的寫(xiě)法卻不少,并且結(jié)構(gòu)體分兩塊,讀寫(xiě)信道和結(jié)束標(biāo)識(shí),下面進(jìn)入文章了解具體的內(nèi)容吧
    2022-02-02
  • Go語(yǔ)言中的指針運(yùn)算實(shí)例分析

    Go語(yǔ)言中的指針運(yùn)算實(shí)例分析

    這篇文章主要介紹了Go語(yǔ)言中的指針運(yùn)算技巧,實(shí)例分析了Go語(yǔ)言指針運(yùn)算的實(shí)現(xiàn)方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • Go語(yǔ)言使用HTTP包創(chuàng)建WEB服務(wù)器的方法

    Go語(yǔ)言使用HTTP包創(chuàng)建WEB服務(wù)器的方法

    這篇文章主要介紹了Go語(yǔ)言使用HTTP包創(chuàng)建WEB服務(wù)器的方法,結(jié)合實(shí)例形式分析了Go語(yǔ)言基于HTTP包創(chuàng)建WEB服務(wù)器客戶端與服務(wù)器端的實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-07-07
  • golang限流庫(kù)兩個(gè)大bug(半年之久無(wú)人提起)

    golang限流庫(kù)兩個(gè)大bug(半年之久無(wú)人提起)

    最近我的同事在使用uber-go/ratelimit[1]這個(gè)限流庫(kù)的時(shí)候,遇到了兩個(gè)大?bug,這兩個(gè)?bug?都是在這個(gè)庫(kù)的最新版本(v0.3.0)中存在的,而這個(gè)版本從?7?月初發(fā)布都已經(jīng)過(guò)半年了,都沒(méi)人提?bug,難道大家都沒(méi)遇到過(guò)么
    2023-12-12
  • Go語(yǔ)言列表List獲取元素的4種方式

    Go語(yǔ)言列表List獲取元素的4種方式

    Golang的列表元素的獲取可以使用內(nèi)置的 Front 函數(shù)獲取頭結(jié)點(diǎn),使用 Back 函數(shù)獲取尾結(jié)點(diǎn),使用 Prev 獲取前一個(gè)結(jié)點(diǎn),使用 Next 獲取下一個(gè)結(jié)點(diǎn),本文就介紹了Go語(yǔ)言列表List獲取元素的4種方式,感興趣的可以了解一下
    2022-04-04
  • Go內(nèi)存節(jié)省技巧簡(jiǎn)單實(shí)現(xiàn)方法

    Go內(nèi)存節(jié)省技巧簡(jiǎn)單實(shí)現(xiàn)方法

    這篇文章主要為大家介紹了Go內(nèi)存節(jié)省技巧簡(jiǎn)單實(shí)現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Golang服務(wù)的請(qǐng)求調(diào)度的實(shí)現(xiàn)

    Golang服務(wù)的請(qǐng)求調(diào)度的實(shí)現(xiàn)

    Golang服務(wù)請(qǐng)求調(diào)度是一種使用Go語(yǔ)言實(shí)現(xiàn)的服務(wù)請(qǐng)求管理方法,本文主要介紹了Golang服務(wù)的請(qǐng)求調(diào)度的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • Golang中的強(qiáng)大Web框架Fiber詳解

    Golang中的強(qiáng)大Web框架Fiber詳解

    在不斷發(fā)展的Web開(kāi)發(fā)領(lǐng)域中,選擇正確的框架可以極大地影響項(xiàng)目的效率和成功,介紹一下Fiber,這是一款令人印象深刻的Golang(Go語(yǔ)言)Web框架,在本文中,我們將深入了解Fiber的世界,探討其獨(dú)特的特性,并理解為什么它在Go生態(tài)系統(tǒng)中引起了如此大的關(guān)注
    2023-10-10
  • Go?Java?算法之字符串解碼示例詳解

    Go?Java?算法之字符串解碼示例詳解

    這篇文章主要為大家介紹了Go?Java?算法之字符串解碼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08

最新評(píng)論