我有一个/token
正在使用password
授权的端点。因为它正在编码一个JWT
令牌,所以它有大约 1 秒的高延迟。有没有办法更快地签署 JWT?
我在github.com/dgrijalva/jwt-go
库中使用 Go。
package main
import (
"crypto/rsa"
"git.snappfood.ir/golang/framework/assert"
"io/ioutil"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/sirupsen/logrus"
)
var (
private *rsa.PrivateKey
public *rsa.PublicKey
)
func main() {
var err error
var priv, pub []byte
pub, err = ioutil.ReadFile("public.pem")
if err!=nil{
panic(err)
}
priv, err = ioutil.ReadFile("private.pem")
if err!=nil{
panic(err)
}
public, err = jwt.ParseRSAPublicKeyFromPEM(pub)
if err!=nil{
panic(err)
}
private, err = jwt.ParseRSAPrivateKeyFromPEM(priv)
if err!=nil{
panic(err)
}
data := map[string]interface{}{
"jti": "dara",
"scopes": func() []string {
return []string{"sara", "dara"}
}(),
"aud": "aud",
"sub": "",
}
cl := jwt.MapClaims{}
for k, v := range data {
cl[k] = v
}
cl["iat"] = time.Now().Add(-6 * time.Hour).Unix()
cl["exp"] = time.Now().UTC().Add(1 * time.Hour).Unix()
t := jwt.NewWithClaims(jwt.GetSigningMethod("RS256"), cl)
t2 := time.Now()
tokenString, err := t.SignedString(private)
assert.Nil(err)
logrus.Warn(time.Since(t2))
logrus.Warn(tokenString)
}