授权凭证
用户授权凭证
应用授权凭证
注意:出于安全性考虑,在计算应用 accessToken 时需要加入过期时间参数,建议值是 1 小时。
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
public static final Long EXPIRES_IN = 1 * 3600 * 1000L;
public static final String TOKEN_APPID = "_appId";
public static String genAppToken(String appId, String appSecret) {
if (StringUtils.isEmpty(appId) || StringUtils.isEmpty(appSecret)) {
return null;
}
Algorithm algorithm = Algorithm.HMAC256(appSecret);
long timestamp = System.currentTimeMillis();
Date issuedAt = new Date(timestamp);
Date expiresAt = new Date(timestamp + EXPIRES_IN);
return JWT.create()
.withClaim(TOKEN_APPID, appId)
.withIssuedAt(issuedAt)
.withExpiresAt(expiresAt)
.sign(algorithm);
}
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.4.0</version>
</dependency>
import (
"fmt"
"github.com/dgrijalva/jwt-go"
)
token := jwt.New(jwt.SigningMethodHS256)
claims := make(jwt.MapClaims)
claims["exp"] = time.Now().Add(time.Hour * time.Duration(1)).Unix()
claims["iat"] = time.Now().Unix()
claims["_appId"] = appId
token.Claims = claims
tokenString, err := token.SignedString([]byte(secretKey))
header = {}
payload = {
_appId: "YOU_APP_ID",
iat: NOW_SECOND,
exp: NOW_SECOND + 3600
}
jwt.sign(header, payload, "YOU_APP_SECRET")
修改于 2022-02-28 07:55:18