java验证苹果_苹果登录 Sign in with Apple 服务端校验

本文介绍了如何在Java后端进行苹果登录的校验,主要涉及校验苹果授权登录token的正确性。通过解析JWT,设置公钥,检查issuer、audience和subject来验证token的有效性,并提供了相关异常处理。文章还列出了所需的依赖库,如jjwt、fastjson等。
摘要由CSDN通过智能技术生成

APP端苹果登录java后端校验

主要校验苹果授权登录token 是否正确

主要方法

public RSAPublicKeySpec build(final String n, final String e) {

final BigInteger modulus = new BigInteger(1, Base64.decodeBase64(n));

final BigInteger publicExponent = new BigInteger(1, Base64.decodeBase64(e));

return new RSAPublicKeySpec(modulus, publicExponent);

}

public int verify(final PublicKey key, final String jwt, final String audience, final String subject) {

final JwtParser jwtParser = Jwts.parser().setSigningKey(key);

jwtParser.requireIssuer("https://appleid.apple.com");

jwtParser.requireAudience(audience);

jwtParser.requireSubject(subject);

try {

final Jws claim = jwtParser.parseClaimsJws(jwt);

if (claim != null && claim.getBody().containsKey("auth_time")) {

claims = claim;

log.info("[Apple登录解密结果]header:{},body:{},signature:{}", claim.getHeader(), claim.getBody(),

claim.getSignature());

return 1;

}

return 0;

} catch (final ExpiredJwtException e) {

log.error("apple identityToken expired");

return -1;

} catch (final Exception e) {

log.error("apple identityToken illegal");

return -2;

}

}

/**

* 从hex string生成公钥

*

* @param stringN

* @param stringE

* @return 构造好的公钥

* @throws NoSuchAlgorithmException

* @throws InvalidKeySpecException

*/

public static PublicKey createPublicKey(final String stringN, final String stringE)

throws NoSuchAlgorithmException, InvalidKeySpecException {

try {

// BigInteger N = new BigInteger(stringN, 16); // hex base

// BigInteger E = new BigInteger(stringE, 16); // hex base

final BigInteger modulus = new BigInteger(1, Base64.decodeBase64(stringN));

final BigInteger publicExponent = new BigInteger(1, Base64.decodeBase64(stringE));

final RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, publicExponent);

final KeyFactory kf = KeyFactory.getInstance("RSA");

return kf.generatePublic(spec);

} catch (final Exception e) {

e.printStackTrace();

}

return null;

}

需要引用到的pom包

io.jsonwebtoken

jjwt

0.9.1

com.alibaba

fastjson

1.2.30

org.apache.httpcomponents

httpclient

com.javabase64

javabase64

1.3.1

commons-lang

commons-lang

2.6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值