苹果登录的后台验证token(JAVA)sign with apple

小咪咪 2023-07-10 11:25 10阅读 0赞

苹果登录后台token校验分为2种方式:
1、jwt校验
2、授权码校验
我这里记录一下第一种方式
流程大致如下:
在这里插入图片描述
添加maven依赖:

  1. <dependency>
  2. <groupId>io.jsonwebtoken</groupId>
  3. <artifactId>jjwt</artifactId>
  4. <version>0.9.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.auth0</groupId>
  8. <artifactId>jwks-rsa</artifactId>
  9. <version>0.9.0</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.alibaba</groupId>
  13. <artifactId>fastjson</artifactId>
  14. <version>1.2.60</version>
  15. </dependency>

代码献上:

  1. /** * @Description apple登录--identifyToken校验 * @Author Chongwen.jiang * @Date 2020/2/24 19:28 * @ModifyDate 2020/2/24 19:28 * @Params [identifyToekn] * @Return boolean false:未通过token校验,true:通过校验 */
  2. private boolean checkIdentifyToken(BaseRequest request) {
  3. String identifyToken = request.getIdentifyToken();
  4. logger.info("checkIdentifyToken-identifyToken:{}", identifyToken);
  5. // 向苹果后台获取公钥参数
  6. String appleResp = null;
  7. try {
  8. appleResp = HttpClientCloudUtils.getHttpExecute("https://appleid.apple.com/auth/keys");
  9. logger.info("checkIdentifyToken-appleResp:{}", appleResp);
  10. } catch (Exception e) {
  11. logger.info("checkIdentifyToken-get apple public key fail " + e.getMessage());
  12. throw new PicaException("get apple public key fail Exception", "get apple public key fail");
  13. }
  14. JSONObject appleRespJson = JSONObject.parseObject(appleResp);
  15. String keys = appleRespJson.getString("keys");
  16. JSONArray keysArr = JSONObject.parseArray(keys);
  17. if (identifyToken.split("\\.").length < 2) {
  18. throw new PicaException("get identifyToken fail Exception", "get identifyToken format Exception");
  19. }
  20. JSONObject useAppleAuth = new JSONObject();
  21. String inAuth = new String(Base64.decodeBase64(identifyToken.split("\\.")[0]));
  22. String inKid = JSONObject.parseObject(inAuth).get("kid").toString();
  23. for(Object obj : keysArr){
  24. JSONObject appleAuth = JSONObject.parseObject(obj.toString());
  25. if(inKid.equals(appleAuth.getString("kid"))){
  26. useAppleAuth = appleAuth;
  27. logger.info("checkIdentifyToken-jsonObject1:{}", useAppleAuth);
  28. break;
  29. }
  30. }
  31. // 通过jar生成publicKey
  32. PublicKey publicKey;
  33. try {
  34. Jwk jwa = Jwk.fromValues(useAppleAuth);
  35. publicKey = jwa.getPublicKey();
  36. } catch (Exception e) {
  37. logger.info("checkIdentifyToken-generate publicKey fail " + e.getMessage());
  38. throw new PicaException("checkIdentifyToken-generate publicKey fail", "generate publicKey fail");
  39. }
  40. // 分割前台传过来的identifyToken(jwt格式的token)用base64解码使用
  41. String aud;
  42. String sub;
  43. try {
  44. String claim = new String(Base64.decodeBase64(identifyToken.split("\\.")[1]));
  45. //logger.info("checkIdentifyToken-claim:{}", claim);
  46. aud = JSONObject.parseObject(claim).get("aud").toString();
  47. sub = JSONObject.parseObject(claim).get("sub").toString();
  48. // appleUserId从token中解码取出后赋值
  49. request.setAppleUserId(sub);
  50. } catch (Exception e) {
  51. logger.info("checkIdentifyToken-token decode fail " + e.getMessage());
  52. throw new PicaException("checkIdentifyToken-token decode fail Exception", "token decode fail");
  53. }
  54. return this.verify(publicKey, identifyToken, aud, sub, request);
  55. }
  56. /** * @Description 验证苹果公钥 * @Author Chongwen.jiang * @Date 2020/2/24 19:49 * @ModifyDate 2020/2/24 19:49 * @Params [key, jwt, audience, subject] * @Return boolean */
  57. private boolean verify(PublicKey key, String jwt, String audience, String subject, BaseRequest request) {
  58. JwtParser jwtParser = Jwts.parser().setSigningKey(key);
  59. jwtParser.requireIssuer("https://appleid.apple.com");
  60. jwtParser.requireAudience(audience);
  61. jwtParser.requireSubject(subject);
  62. try {
  63. logger.info("checkIdentifyToken-apple-verify-starting");
  64. Jws<Claims> claim = jwtParser.parseClaimsJws(jwt);
  65. logger.info("acheckIdentifyToken-apple-verify-claim:{}", JSON.toJSONString(claim));
  66. //logger.info("apple-verify-claim.getBody:{}", JSON.toJSONString(claim.getBody()));
  67. if (claim != null && claim.getBody().containsKey("auth_time")) {
  68. request.setInfo(JSON.toJSONString(claim.getBody()));
  69. JSONObject claimBody = JSONObject.parseObject(JSON.toJSONString(claim.getBody()), JSONObject.class);
  70. request.setAppleId(claimBody.getString("email"));
  71. return true;
  72. }
  73. return false;
  74. } catch (ExpiredJwtException e) {
  75. logger.info("checkIdentifyToken-apple token expired " + e.getMessage());
  76. throw new PicaException("apple token expired Exception", "apple token expired");
  77. } catch (Exception e) {
  78. logger.info("checkIdentifyToken-apple token illegal " + e.getMessage());
  79. throw new PicaException("apple token illegal Exception", "apple token illegal");
  80. }
  81. }

参考链接:
jwt技术认识
sign with apple
官方文档
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,10人围观)

还没有评论,来说两句吧...

相关阅读