Coder Social home page Coder Social logo

gfast-token's Introduction

gfast-token

基于GoFrame框架的token插件,通过服务端验证方式实现token认证

Requirements

gf version >= v2.0.0

Installation

go get -u github.com/tiger1103/gfast-token

Usage

Import

import "github.com/tiger1103/gfast-token"

demo

type User struct {
    UserData string      // 用户数据
    Data    interface{} // 其他需要携带的数据
}

func main() {
    /**
    注意事项:
    1、token存活时间 = 超时时间 + 缓存刷新时间
    2、处理携带token的请求时当前时间大于超时时间并小于缓存刷新时间时token将自动刷新即重置token存活时间
    3、每创建一个gfToken实例时CacheKey必须不相同
    4、GenerateToken函数参数的User.UserKey为用户唯一标识,必须且唯一
    */
    gft := gftoken.NewGfToken(
        gftoken.WithCacheKey("gfToken_"),
        gftoken.WithTimeout(60),
        gftoken.WithMaxRefresh(50),
        gftoken.WithMultiLogin(false),
        gftoken.WithExcludePaths(g.SliceStr{"/excludeDemo"}),
        gftoken.WithGRedisConfig(&gredis.Config{
            Address: "127.0.0.1:6379",
            Db:      9,
        }))
    s := g.Server()
    s.Group("/", func(group *ghttp.RouterGroup) {
        group.GET("/login", func(r *ghttp.Request) {
            userId := r.GetQuery("id").String()
            token, err := gft.GenerateToken(r.GetCtx(), gmd5.MustEncrypt(userId), User{
                UserData: userId,
                Data:    "myData",
            })
    
            if err != nil {
                g.Log().Error(r.GetCtx(), err)
            }
    
            r.Response.Write(token)
        })
    
        gft.Middleware(group)
        group.GET("/user", func(r *ghttp.Request) {
            token, err := gft.GetToken(r)
            if err != nil {
                r.Response.Write(err)
                return
            }
            data, err := gft.ParseToken(token.JwtToken)
            if err != nil {
                r.Response.Write(err)
                return
            }
            r.Response.Write(data)
        })
        group.GET("/excludeDemo", func(r *ghttp.Request) {
            r.Response.Write("Exclude path anyone can access")
        })
    })
    s.SetPort(8080)
    s.Run()
}

gfast-token's People

Contributors

tiger1103 avatar wilgx0 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.