使用go 对session的支持
Skip to content This repository Search Pull requests Issues Gist @dongtian3240 Unwatch 1 Star 0 Fork 0 dongtian3240/gosession Code Issues 0 Pull requests 0 Wiki Pulse Graphs Settings Branch: master Find file Copy pathgosession/gosession.go f79751f 5 days ago dongtian3240 提交session验证有效期问题 0 contributors RawBlameHistory 194 lines (150 sloc) 3.87 KB package gosession import ( "crypto/rand" "encoding/base64" "fmt" "net/http" "sync" "time" ) var ( Domain string = "localhost" Path string = "/" HttpOnly bool = false MaxAge int = 3600 GoSessionId string = "gosession-id" ) type SyncGoSession interface { UpdateSession(response http.ResponseWriter, request *http.Request) } type GoSession struct { SId string data map[string]interface{} } type GoSessionManager struct { gosessions map[string]*GoSession CreateAts map[string]time.Time GCInterval time.Duration GoSessionId string MaxAge int Domain string HttpOnly bool Path string sync.Mutex } func NewGoSessionManager(gosessionsId string, gcInterval time.Duration) *GoSessionManager { if gosessionsId == "" { gosessionsId = GoSessionId } if gcInterval <= 0 { gcInterval = time.Millisecond * 100 } return &GoSessionManager{ gosessions: make(map[string]*GoSession), CreateAts: make(map[string]time.Time), MaxAge: MaxAge, Domain: Domain, HttpOnly: HttpOnly, Path: Path, GoSessionId: gosessionsId, GCInterval: gcInterval, } } //获取gosession func (gsm *GoSessionManager) GetGoSession(response http.ResponseWriter, request *http.Request) *GoSession { currentCookie, err := request.Cookie(gsm.GoSessionId) //创建一个新的gosession if err != nil || currentCookie == nil { return gsm.createGoSession(response, request) } else { //当前gosession是否存在 goSession, has := gsm.hasGoSession(currentCookie.Value) if has { // 如果存在将直接返回 return goSession } else { return gsm.createGoSession(response, request) } } } // 查询是否已经存在 func (gsm *GoSessionManager) hasGoSession(key string) (*GoSession, bool) { goSesion, ok := gsm.gosessions[key] return goSesion, ok } //创建一个新的gosession func (gsm *GoSessionManager) createGoSession(response http.ResponseWriter, request *http.Request) *GoSession { var bys = make([]byte, 32) rand.Read(bys) cValue := base64.StdEncoding.EncodeToString(bys) c := &http.Cookie{ Name: gsm.GoSessionId, Value: cValue, Path: gsm.Path, Domain: gsm.Domain, MaxAge: gsm.MaxAge, HttpOnly: gsm.HttpOnly, } http.SetCookie(response, c) goSession := &GoSession{ SId: cValue, data: make(map[string]interface{}), } gsm.gosessions[cValue] = goSession gsm.CreateAts[cValue] = time.Now().Add(time.Second * time.Duration(gsm.MaxAge)) return goSession } func (gs *GoSession) Get(key string) interface{} { va, ok := gs.data[key] if ok { return va } else { return nil } } func (gs *GoSession) Set(key string, va interface{}) { gs.data[key] = va } func (gs *GoSession) Delete(key string) { delete(gs.data, key) } //垃圾收集处理过期的 GoSession func StartGC(gsm *GoSessionManager) { go func() { for _ = range time.Tick(gsm.GCInterval) { fmt.Println("************ GC **************") for k, v := range gsm.CreateAts { if gsm.expiredSession(v) == false { gsm.Delete(k) } } } }() } //判断是否过期 func (gsm *GoSessionManager) expiredSession(at time.Time) bool { return time.Now().Before(at) } //根据key删除gosession func (gsm *GoSessionManager) Delete(key string) { gsm.Lock() defer gsm.Unlock() delete(gsm.CreateAts, key) delete(gsm.gosessions, key) } // 更新gosession func (gsm *GoSessionManager) UpdateGoSession(response http.ResponseWriter, request *http.Request) { currentCookie, err := request.Cookie(gsm.GoSessionId) var key string if err != nil || currentCookie == nil { return } key = currentCookie.Value _, ok := gsm.CreateAts[key] if ok { fmt.Println("=======update goSession ===========") gsm.CreateAts[key] = time.Now().Add(time.Second * time.Duration(gsm.MaxAge)) fmt.Println("at ", gsm.CreateAts[key], "key=", key) currentCookie.MaxAge = gsm.MaxAge http.SetCookie(response, currentCookie) } } Status API Training Shop Blog About © 2016 GitHub, Inc. Terms Privacy Security Contact Help
传送门 https://github.com/dongtian3240/gosession
相关推荐
2.Go语言基础 2.1. 你好,Go 2.2. Go基础 2.3. 流程和函数 2.4. struct 2.5. 面向对象 2.6. interface 2.7. 并发 2.8. 小结 3.Web基础 3.1 web工作方式 3.2 Go搭建一个简单的web服务 3.3 Go如何使得web工作 3.4 Go的...
Go session:Go编程语言最快的Web session管理器。同时支持net/http和fasthttp
Seago是golang实现的简单的web框架,router包来自web.go和martini 功能 支持RESTful 支持Session 支持Cache 支持Middleware 标签:Seago Web框架
模板支持include子模板内置基于文件和memcache的session支持,同时支持自定义sessionHandler支持静态文件服务提供ini配置文件读取,benchmark,log等辅助方法支持mysql和memcache的dao封装,简化数据操作提供pv、流量...
Django完全支持也匿名会话,简单说就是使用跨网页之间可以进行通讯,比如显示用户名,用户是否已经发表评论。session框架让你存储和获取访问者的数据信息,这些信息保存在服务器上(默认是数据库中),以 cookies 的...
Go语言程序, 可直接下载使用 在 (Linux, MacOS)终端/(Windows)命令提示符 中运行。 Windows 也可双击程序运行。 本程序会开启本地 http 服务器, 默认端口为9090. 程序运行前带入参数 "-h" 以查看帮助. 程序会列出...
cookie是一个key-value的数据结构(类似python字典),用于保存需要维护状态的数据,cookie与session最大的区别是cookie的数据保存在客户端,而session把数据保存在服务端。 cookie一般由服务器设置,并可以存放在...
Baetyl-Broker 基于 Golang 语言开发,是一个单机版地消息订阅和发布中心,采用 MQTT3.1.1 协议,可在低带宽、不可靠网络中提供可靠的消息传输服务。其作为 Baetyl 框架端侧的消息中间件,为所有服务提供消息驱动的...
go get github.com/tango-contrib/session 简单的例子 package main import ( "github.com/lunny/tango" "github.com/tango-contrib/session" ) type SessionAction struct { session. Session } func ( a * ...
简单会话对Golang的基于文件的会话支持-特征简单,精确和简洁的代码快速高效,易于使用没有依赖注入仅使用go标准库(不包括第三方库) 最适合需要基于文件的会话处理的应用程序中小型应用用法创建新会话全新创建了...
简单化:RESTful 支持、MVC 模型,可以使用 bee 工具快速地开发应用,包括监控代码修改进行热编译、自动化测试代码以及自动化打包部署。 智能化:支持智能路由、智能监控,可以监控 QPS、内存消耗、CPU 使用,以及 ...
通过session,我们可以在多次浏览器请求中保持数据, 接下来的部分就是用session来处理用户登录了。 当然,不能仅凭用户的一面之词,我们就相信,所以我们需要认证。 当然了,Django 也提供了工具来处理这样的常见...
frp_0.32.0_windows_386,...FRP 是一个使用 Go 语言开发的高性能的反向代理应用,可以帮助您轻松地进行内网穿透,对外网提供服务。FRP 支持 TCP、UDP、HTTP、HTTPS等协议类型,并且支持 Web 服务根据域名进行路由转发。
缺少的主要部分是客户端和服务器之间的功能协商,对于我们的使用,我们对一组受支持的协商标志进行了硬编码。作为NTLM客户端的示例用法import "github.com/ThomsonReutersEikon/go-ntlm/ntlm"session , err = ntlm ...
GoBatisIntroductionAn easy ORM tool for Golang, support MyBatis-Like XML template SQL待完成的任务重构 parser对象继承的实现延迟加载的实现有泛型了,可以尝试下type Lazy[T any] struct { value T session ...
Django默认支持Session,并且默认是将Session数据存储在数据库中的 修改session存取放在数据库中 SESSION_ENGINE = 'django.contrib.sessions.backends.db' # 引擎(默认) session存在缓存里 SESSION_ENGINE =...
Easyssh Go的SSH包装器特征: 支持PEM密钥认证(密码保护和非密码保护) 支持基于密码的身份验证批处理和单命令处理文件: 用法: go get github.com/stevenbrichards/easyssh 单个命令: sshSession := easyssh....
API 文档设计 可视化 API 文档管理,零学习成本。...支持调用 javascript、java、python、php、js、BeanShell、go、shell、ruby、lua 等语言代码。 数据库操作 支持读取数据库数据,作为 API 请求参数使用。
2. 支持session会话,也就是说设置了多长时间登录过期,如果用户没有登录,直接进查询列表页面,会重定向到登录页面去,如果用户登录了后,把浏览器关掉,直接输入列表查询页面,会直接进入列表页面的。 3. 列表数据...
它使用了官方的MongoDB golang驱动程序和来自Olivere的社区支持的Elasticsearch驱动程序。 与MongoDB相关的某些monstache设置已在此版本中删除,因为它们现在在受支持 与先前版本的变化 现在,Monstache默认使用...