demo为v1版本,详细demo请看example示例: GitHub - gogf/gf-jwt: GoFrame HTTP JWT middleware.

启动

运行example/main.go,端口8000

$ go run example/main.go

  SERVER  | DOMAIN  | ADDRESS | METHOD |        ROUTE        |                           HANDLER                           |                        MIDDLEWARE
|---------|---------|---------|--------|---------------------|-------------------------------------------------------------|----------------------------------------------------------|
  default | default | :8000   | ALL    | /                   | github.com/gogf/gf-jwt/example/api.(*workApi).Works-fm      |
|---------|---------|---------|--------|---------------------|-------------------------------------------------------------|----------------------------------------------------------|
  default | default | :8000   | POST   | /login              | github.com/gogf/gf-jwt.(*GfJWTMiddleware).LoginHandler-fm   |
|---------|---------|---------|--------|---------------------|-------------------------------------------------------------|----------------------------------------------------------|
  default | default | :8000   | ALL    | /user/info          | github.com/gogf/gf-jwt/example/api.(*workApi).Info-fm       | service.(*middlewareService).CORS-fm,main.middlewareAuth
|---------|---------|---------|--------|---------------------|-------------------------------------------------------------|----------------------------------------------------------|
  default | default | :8000   | ALL    | /user/logout        | github.com/gogf/gf-jwt.(*GfJWTMiddleware).LogoutHandler-fm  | service.(*middlewareService).CORS-fm,main.middlewareAuth
|---------|---------|---------|--------|---------------------|-------------------------------------------------------------|----------------------------------------------------------|
  default | default | :8000   | ALL    | /user/refresh_token | github.com/gogf/gf-jwt.(*GfJWTMiddleware).RefreshHandler-fm | service.(*middlewareService).CORS-fm,main.middlewareAuth
|---------|---------|---------|--------|---------------------|-------------------------------------------------------------|----------------------------------------------------------|

2021-01-30 21:24:47.672 14000: http server started listening on [:8000]

登录

账号密码 admin

$ curl -s -XPOST localhost:8000/login -d 'username=admin&password=admin'
{
    "code": 200,
    "expire": "2021-01-30T21:23:33+08:00",
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTIwMTMwMTMsImlhdCI6MTYxMjAxMjcxMywiaWQiOjEsInVzZXJuYW1lIjoiYWRtaW4ifQ.avsmOHGyHWIovihtoW7bjuQbXs81v1nKiZ9iYMZYwOs"
}

查看用户信息

Header携带Authorization:Bearer xxxxxx,或Body携带token: xxxxxxxxxxxxxx

$ curl -s -XPOST localhost:8000/user/info -d 'token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTIwMTMwMTMsImlhdCI6MTYxMjAxMjcxMywiaWQiOjEsInVzZXJuYW1lIjoiYWRtaW4ifQ.avsmOHGyHWIovihtoW7bjuQbXs81v1nKiZ9iYMZYwOs'
{
    "id": 1,
    "payload": {
        "exp": 1612013013,
        "iat": 1612012713,
        "id": 1,
        "username": "admin"
    }
}

idIdentityKey,由开发者自由控制,一般为用户id,且必须存在于私有载荷。

payload是jwt的载荷,由预定义iat(签发时间)、exp(过期时间)、私有载荷组成,私有载荷可由开发者自行设置,在例子中{"id: 1, "username": "admin"}就是私有载荷。具体设置请看下一章节。

刷新/续签

$ curl -s -XPOST localhost:8000/user/refresh_token -d 'token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTIwMTMwMTMsImlhdCI6MTYxMjAxMjcxMywiaWQiOjEsInVzZXJuYW1lIjoiYWRtaW4ifQ.avsmOHGyHWIovihtoW7bjuQbXs81v1nKiZ9iYMZYwOs'
{
    "code": 200,
    "expire": "2021-01-30T21:27:28+08:00",
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTIwMTMyNDgsImlhdCI6MTYxMjAxMjk0OCwiaWQiOjEsInVzZXJuYW1lIjoiYWRtaW4ifQ.UYKBsnXipeRBR4GENFVnpE09wazagHlCLMnQ7o7EJE4"
}

退出/注销

$ curl -s -XPOST localhost:8000/user/logout -d 'token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTIwMTMyNDgsImlhdCI6MTYxMjAxMjk0OCwiaWQiOjEsInVzZXJuYW1lIjoiYWRtaW4ifQ.UYKBsnXipeRBR4GENFVnpE09wazagHlCLMnQ7o7EJE4'
{
    "code": 200,
    "message": "success"
}

401

token过期或token无效

$ curl -s -XPOST localhost:8000/user/info -d 'token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTIwMTMyNDgsImlhdCI6MTYxMjAxMjk0OCwiaWQiOjEsInVzZXJuYW1lIjoiYWRtaW4ifQ.UYKBsnXipeRBR4GENFVnpE09wazagHlCLMnQ7o7EJE4'
{
    "code": 401,
    "msg": "Token is expired"
}
  • No labels

4 Comments

  1. 这个demo的完整代码怎么下载??

    光有说明没有完整代码下载方式,想测试试验都没办法……

    1. 怎么会没有呢,github仓库的example有的