3.4.0
Gradle
compile ‘com.auth0:java-jwt:3.4.0’
1.JWT生成
public String JWTGenerate(String key, String secret, String jwtSecret) {
Map<String, Object> headers = new HashMap<String, Object>();
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);
/**
- Header Claims
*/
//Returns the Algorithm value 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”);
/**
- Payload Claims
*/
//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<String, Object> 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<String, Object> map = (Map<String, Object>) parse.getBody();
String param = (String) map.get(“param”);
}
另外还可以使用其它的库,可前往 https://github.com/auth0/JWTDecode.Android 进行学习和使用,当然,你也可以用上面提到的Java库来进行解码。
添加依赖:
compile ‘com.auth0.android:jwtdecode:1.1.1’
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
总结
【Android 详细知识点思维脑图(技能树)】
其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。
虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。
这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。
由于篇幅有限,这里以图片的形式给大家展示一小部分。
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。
由于篇幅有限,这里以图片的形式给大家展示一小部分。
[外链图片转存中…(img-7buJ0efK-1713385340599)]
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!