- 包引入
github.com/dgrijalva/jwt-go
- utils目录下编写Jwt.go
package utils
import (
// "crypto/md5"
"fmt"
"github.com/astaxie/beego/logs"
// "github.com/3xxx/go-sso/models"
"github.com/astaxie/beego"
// "github.com/astaxie/beego/orm"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
)
const (
KEY string = "JWT-xxxx"
DEFAULT_EXPIRE_SECONDS int64 = 24 * 60 * 60 //设置有效期为 24 小时
DEFAULT_REFRESH_SECONDS int64 = 1 * 60 * 60 //设置刷新时间为过期前 1 小时
)
//使用这个生成token
func CreateToken(userName string, su_id int, app string) (tokenString string, expSeconds int64, expTime int64, refreshTime int64, err error) {
// token := jwt.New(jwt.SigningMethodHS256)
claims := make(jwt.MapClaims)
expTime = time.Now().Add(time.Second * time.Duration(DEFAULT_EXPIRE_SECONDS)).Unix()
refreshTime = expTime - DEFAULT_REFRESH_SECONDS
//添加令牌期限