系统是java开发的,单点登录到python的系统,就需要使用python 解析 java jwt生产的token
之所以能在各个语言互通加密解密,就是因为 JSON Web Tokens (JWT)是开发的一个行业标准。所以通用
1.java生成jwt
pom
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.2.0</version>
</dependency>
生产token
public static String create(Long userNo, Long date) {
try {
return JWT.create().withIssuer(ISSUER).withClaim(USERNO, userNo.toString()).withExpiresAt(new Date(System.currentTimeMillis() + date)).sign(Algorithm.HMAC256(TOKEN_SECRET));
} catch (IllegalArgumentException | JWTCreationException | UnsupportedEncodingException e) {
logger.error("JWT生成失败", e);
return "";
}
}
输出
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTm8iOiIyMDE4MTEyMjA5NTMxODAzIiwiaXNzIjoiUk9OQ09PIiwiZXhwIjoxNjM0MTgzOTM3fQ.Uv9ScXxCjU0rtbY........
2.python解析token
pip install pyjwt
token = handler.get_cookie('template_token')
user = jwt.decode(token,'JIUzI1NiJ9',algorithms = ['HS256'])
print(user)
输出
{'userid': '2021', 'iss': 'is', 'exp': 1634183937}