douju1953 2016-03-29 18:59
浏览 779
已采纳

Golang和JWT-简单注销

I'm currently working on an API and after a bit of time, I now understand how to use JWT in Go to get a token. With this token, I can keep a user connected but, how can I logout from the client application?

Here is my token.go code:

package main

import (
    "github.com/dgrijalva/jwt-go"
    "time"
)

const (
    tokenEncodeString = "something"
)

func createToken(user User) (string, error) {
    // create the token                                                                                                                                                                                  
    token := jwt.New(jwt.SigningMethodHS256)

    // set some claims                                                                                                                                                                                   
    token.Claims["username"] = user.Username;
    token.Claims["password"] = user.Password;
    token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix()

    //Sign and get the complete encoded token as string                                                                                                                                                  
    return (token.SignedString([]byte(tokenEncodeString)))
}

func parseToken(unparsedToken string) (bool, string) {
    token, err := jwt.Parse(unparsedToken, func(token *jwt.Token) (interface{}, error) {
            return []byte(tokenEncodeString), nil
    })

    if err == nil && token.Valid {
            return true, unparsedToken
    } else {
            return false, ""
    }
}

After research, I found out that I can use a black list, but I really want to know if it's possible with something easier, like the code above.

I also want to find a solution that works with the memory used by the JWT process. Someone who disconnects/connects himself all the time has to have only one token for each session, not one for him and a hundred in a given black list.

  • 写回答

1条回答 默认 最新

  • douchaqi3369 2016-03-29 21:45
    关注

    First: Don't (ever) put sensitive credentials in the token. They are not encrypted, and you shouldn't need to do that.

    To note:

    • JWTs are stateless: you issue one, and it lives as long as you have allowed/are validating against on the server.
    • You could issue a new JWT with an expiry of 'now', but the old JWT would still be valid (i.e. a security risk).
    • Read through http://jwt.io/introduction/

    If you need control over expiring access tokens after issuance, then you should implement a server-side scheme, which would allow you to expire tokens directly. The user would only hold an ID that references the server-side store.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退