Categorygithub.com/kotlin2018/jwt
repositorypackage
0.1.0
Repository: https://github.com/kotlin2018/jwt.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

gf-jwt

基于GoFrame框架的JWT认证

使用

下载安装

$ go get github.com/kotlin2018/jwt

导入

import "github.com/kotlin2018/jwt"

参数说明

claims["expire"] token的过期时间.

Claims["current_time"] 生成token的当前时间.

r.SetParam("JWT_Payload", claims) 将有效载荷设置到请求中.

r.GetString("Jwt_Token").

请求头中的Authorization字段保存token的值.

例子

查看示例 example/auth/auth.go , 使用 ExtractClaims 可以自定义用户数据.

Demo

运行 example/server/server.go8000端口.

$ go run example/server/server.go

api screenshot

通过 httpie ,在命令行来测试下效果.

登录接口:

$ http -v --form  POST localhost:8000/login username=admin password=admin

命令行输出

api screenshot

刷新 token 接口:

$ http -v -f GET localhost:8000/user/refresh_token "Authorization:Bearer xxxxxxxxx"  "Content-Type: application/json"

命令行输出

api screenshot

info 接口

我们使用用户名 admin 和密码 admin 测试一下 info 接口的返回

$ http -f GET localhost:8000/user/info "Authorization:Bearer xxxxxxxxx"  "Content-Type: application/json"

命令行输出

api screenshot

用户验证接口

我们用未授权的 token 来测试 info 接口的返回

$ http -f GET localhost:8000/user/info "Authorization:Bearer xxxxxxxxx"  "Content-Type: application/json"

命令行输出

api screenshot

再次感谢https://github.com/appleboy/gin-jwt