基于Golang開發(fā)一個(gè)輕量級(jí)登錄庫/框架
幾乎每個(gè)項(xiàng)目都會(huì)有登錄,退出等用戶功能,而登錄又不單僅僅是登錄,我們要考慮很多東西。
token該怎么生成?生成什么樣的?
是在Cookie存token還是請(qǐng)求頭存token?讀取的時(shí)候怎么讀???
允許同一個(gè)賬號(hào)被多次登錄嗎?多次登錄他們的token是一樣的?還是不一樣的?
登錄也有可能分成管理員登錄,用戶登錄等多種登錄類型
我們要做的就是把這些東西封裝到一起,然后能更方便的使用
而完成這些最難的就是如何設(shè)計(jì)架構(gòu)了,其實(shí)要簡單的封裝一下并不難,本篇要講的就是如何進(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
// 登錄類型
loginType string
config config.TokenConfig
// 生成token的函數(shù)
generateFunc model.GenerateTokenFunc
// 用于存儲(chǔ)數(shù)據(jù)
adapter persist.Adapter
// 監(jiān)聽器
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寫入響應(yīng)頭
IsWriteHeader bool
CookieConfig *CookieConfig
}3.Adapter
adapter是底層用來存儲(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,可能也需要寫出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)聽器,用于在一些事件發(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ì)成可以自由開啟關(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開發(fā)一個(gè)輕量級(jí)登錄庫/框架的文章就介紹到這了,更多相關(guān)Golang開發(fā)登錄庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用go實(shí)現(xiàn)常見的數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了使用go實(shí)現(xiàn)常見的數(shù)據(jù)結(jié)構(gòu),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
golang 獲取當(dāng)前執(zhí)行程序路徑的操作
這篇文章主要介紹了golang 獲取當(dāng)前程序執(zhí)行路徑的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Go語言實(shí)現(xiàn)遺傳算法的實(shí)例代碼
Go 是一個(gè)開源的編程語言,它能讓構(gòu)造簡單、可靠且高效的軟件變得容易。本文將重點(diǎn)介紹如何用Go語言實(shí)現(xiàn)遺傳算法。如果你還沒有參加過GoLang Tour,我還建議你快速看一下這門語言的介紹2017-11-11
golang在GRPC中設(shè)置client的超時(shí)時(shí)間
這篇文章主要介紹了golang在GRPC中設(shè)置client的超時(shí)時(shí)間,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
Ruby序列化和持久化存儲(chǔ)(Marshal、Pstore)操作方法詳解
這篇文章主要介紹了Ruby序列化和持久化存儲(chǔ)(Marshal、Pstore)操作方法詳解,包括Ruby Marshal序列化,Ruby Pstore存儲(chǔ),需要的朋友可以參考下2022-04-04
Go使用TimerController解決timer過多的問題
多路復(fù)用,實(shí)際上Go底層也是一種多路復(fù)用的思想去實(shí)現(xiàn)的timer,但是它是底層的timer,我們需要解決的問題就過多的timer問題!本文給大家介紹了Go使用TimerController解決timer過多的問題,需要的朋友可以參考下2024-12-12
Go語言中Redis緩存與本地內(nèi)存緩存實(shí)戰(zhàn)
在現(xiàn)代高并發(fā)系統(tǒng)中,緩存技術(shù)是提升性能和降低數(shù)據(jù)庫壓力的關(guān)鍵手段,本文將為大家介紹一下Redis緩存與本地內(nèi)存緩存的具體應(yīng)用,需要的可以了解下2025-03-03
go語言中基本數(shù)據(jù)類型及應(yīng)用快速了解
這篇文章主要為大家介紹了go語言中基本數(shù)據(jù)類型應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

