基于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)文章
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-07golang限流庫(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-12Go內(nèi)存節(jié)省技巧簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要為大家介紹了Go內(nèi)存節(jié)省技巧簡(jiǎn)單實(shí)現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Golang服務(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