JWT在Java和Android中的使用,android开发基础入门

JWT在Java和Android中的使用,android开发基础入门,第1张

JWT在Java和Android中的使用,android开发基础入门 二、JWT在Java中的使用

首先我们要在https://jwt.io/主页上找到Java项目的入口,我在这里选择的是"maven: com.auth0 / java-jwt / 3.3.0",点击该项右下角的"View Repo"按钮即可跳转到项目Github主页进行导入。我在这里直接将导入步骤写下来,该版本为写文章时的最新版本。

Maven

com.auth0

java-jwt

3.4.0

Gradle

compile ‘com.auth0:java-jwt:3.4.0’

1.JWT生成

public String JWTGenerate(String key, String secret, String jwtSecret) {

Map headers = new HashMap();

headers.put(“alg”, “HS256”);

headers.put(“typ”, “JWT”);

String builder = JWT.create().withHeader(hearMap).withClaim(“key”, key).withClaim(“secret”, secret).sign(Algorithm.HMAC256(jwtSecret));

return builder;

}

2.JWT解码

public void JWTDecode(String token) {

try {

DecodedJWT jwt = JWT.decode(token);

//Returns the Algorithm value

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

or null if it’s not defined in the Header.

String algorithm = jwt.getAlgorithm();

//Returns the Type value or null if it’s not defined in the Header.

String type = jwt.getType();

//Returns the Content Type value or null if it’s not defined in the Header.

String contentType = jwt.getContentType();

//Returns the Key Id value or null if it’s not defined in the Header.

String keyId = jwt.getKeyId();

//Private Claims

Claim claim = jwt.getHeaderClaim(“owner”);

//Returns the Issuer value or null if it’s not defined in the Payload.

String issuer = jwt.getIssuer();

//Returns the Subject value or null if it’s not defined in the Payload.

String subject = jwt.getSubject();

//Returns the Audience value or null if it’s not defined in the Payload.

List audience = jwt.getAudience();

//Returns the Expiration Time value or null if it’s not defined in the Payload.

Date expiresAt = jwt.getExpiresAt();

//Returns the Not Before value or null if it’s not defined in the Payload.

Date notBefore = jwt.getNotBefore();

//Returns the Issued At value or null if it’s not defined in the Payload.

Date issuedAt = jwt.getIssuedAt();

//Returns the JWT ID value or null if it’s not defined in the Payload.

String id = jwt.getId();

//Private Claims

Claim claim2 = jwt.getClaim(“isAdmin”);

} catch (JWTDecodeException exception){

//Invalid token

}

}

三、JWT在Android中的使用

最初我在安卓中也使用的上面的Java库,但发现在使用org.apache.commons.codec.binary.base64时会与Android系统中的包出现冲突,自己尝试封装了一下并改了包名,可以成功调用,但这显然不是最好的方案。所以我在这里使用到另外一个库。我们在https://jwt.io/主页上找到Java项目的入口,我在这里选择的是"maven: io.jsonwebtoken / jjwt / 0.9.0",点击该项右下角的"View Repo"按钮即可跳转到项目Github主页进行导入。下面简单说明一下依赖库的方式。

Maven:

io.jsonwebtoken

jjwt

0.9.1

Gradle:

dependencies {

compile ‘io.jsonwebtoken:jjwt:0.9.1’

}

1.JWT生成

public String JWTGenerate() {

Map map = new HashMap<>();

map.put(“claim1”, “claim1value”);

map.put(“claim2”, “claim2value”);

String key = base64.encodeToString(“secret”.getBytes(), 0);

//Key key = MacProvider.generateKey(SignatureAlgorithm.HS256);

Date exp = new Date(System.currentTimeMillis() + 60 * 1000);//过期时间

String compactJws = Jwts.builder().addClaims(map).setHeaderParam(“typ”, “JWT”)

.signWith(SignatureAlgorithm.HS256, key).setExpiration(exp).compact();

try {

Jwts.parser().setSigningKey(key).parseClaimsJws(compactJws);

//OK, we can trust this JWT

} catch (SignatureException e) {//don’t trust the JWT!

e.printStackTrace();

} catch (ExpiredJwtException e) {//The key is expiration

e.printStackTrace();

}

return compactJws;

}

2.JWT解码

先看一下使用上面的"jjwt"库如何进行解码 *** 作:

public void JWTParse(String jwt) {

String key = base64.encodeToString(“secret”.getBytes(), 0);

//Key key = MacProvider.generateKey(SignatureAlgorithm.HS256);

//在解析的时候一定要传key进去,否则无法通过key的认证

Jwt parse = Jwts.parser().setSigningKey(key).parse(jwt);

Header header = parse.getHeader();

Map map = (Map) parse.getBody();

String param = (String) map.get(“param”);

}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5672609.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存