几乎每个项目都会有登录,退出等用户功能,而登录又不单仅仅是登录,我们要考虑很多东西。
token该怎么生成?生成什么样的?
是在Cookie存token还是请求头存token?读取的时候怎么读取?
允许同一个账号被多次登录吗?多次登录他们的token是一样的?还是不一样的?
登录也有可能分成管理员登录,用户登录等多种登录类型
我们要做的就是把这些东西封装到一起,然后能更方便的使用
而完成这些最难的就是如何设计架构了,其实要简单的封装一下并不难,本篇要讲的就是如何进行架构的设计了。
源码:weloe/token-go: a light login library (github.com)
1.Enforcer
我们可以抽象出一个供外部调用的执行器,它包括以下几个部分
token-go/enforcer.go at master · weloe/token-go (github.com)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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
首先就是根据需求抽象出配置信息
一个是cookie的配置
token-go/cookie.go at master · weloe/token-go · GitHub
1
2
3
4
5
6
7
|
type CookieConfig struct { Domain string Path string Secure bool HttpOnly bool SameSite string } |
一个是token的配置
token-go/token.go at master · weloe/token-go · GitHub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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 } |
3.Adapter
adapter是底层用来存储数据的结构,为了兼容不同的实现(不同的存储方式),设计成一个接口。
token-go/adapter.go at master · weloe/token-go · GitHub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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
我们需要从请求读取token,可能也需要写出token,因此需要兼容不同的web上下文,我们需要设计一个Context接口
token-go/context.go at master · weloe/token-go · GitHub
1
2
3
4
5
6
7
|
type Context interface { Request() Request Response() Response ReqStorage() ReqStorage MatchPath(pattern string , path string ) bool IsValidContext() bool } |
5.Watcher
监听器,用于在一些事件发生的时候进行一些其他操作。
token-go/watcher.go at master · weloe/token-go · GitHub
1
2
3
4
5
6
7
|
// 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等等,需要设计成可以自由开启关闭。
token-go/logger.go at master · weloe/token-go · GitHub
1
2
3
4
5
6
7
8
9
|
type Logger interface { persist.Watcher // Enable turn on or off Enable( bool bool ) // IsEnabled return if logger is enabled IsEnabled() bool } |
到此,项目的大致的结构就设计完成,下一篇会讲讲本业务的具体实现
到此这篇关于基于Golang开发一个轻量级登录库/框架的文章就介绍到这了,更多相关Golang开发登录库内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/weloe/p/17378215.html