51,411
社区成员
发帖
与我相关
我的任务
分享io.jsonwebtoken.SignatureException: Unable to verify RSA signature using configured PublicKey. Signature length not correct: got 256 but was expecting 250
at io.jsonwebtoken.impl.crypto.RsaSignatureValidator.isValid(RsaSignatureValidator.java:50)
at io.jsonwebtoken.impl.crypto.DefaultJwtSignatureValidator.isValid(DefaultJwtSignatureValidator.java:47)
at io.jsonwebtoken.impl.DefaultJwtParser.parse(DefaultJwtParser.java:351)
public AccountCode ioslogin(String uuid,
String nickName, String email, String identoken, String ipAddress) throws Exception {
AccountCode dReturn = new AccountCode();
//String identityToken = new String(dcoder.decode(identoken));
if (identoken.split("\\.").length <= 1){
dReturn.setCode(AccountCode.CODE_IOS_授权异常);
dReturn.setErrmsg(AccountCode.CODE_MESSAGE_IOS_授权异常);
return dReturn;
}
//System.out.println("token:"+identoken);
String firstDate = new String(Base64.decodeBase64(identoken.split("\\.")[0]),"UTF-8");
String claim = new String(Base64.decodeBase64(identoken.split("\\.")[1]),"UTF-8");
String kid = JSONObject.parseObject(firstDate).get("kid").toString();
String aud = JSONObject.parseObject(claim).get("aud").toString();
String sub = JSONObject.parseObject(claim).get("sub").toString();
PublicKey publicKey = loadIOSPublicKey(kid);
if (publicKey == null) {
dReturn.setCode(AccountCode.CODE_IOS_验证PUBLICKEY错误);
dReturn.setErrmsg(AccountCode.CODE_MESSAGE_IOS_验证PUBLICKEY错误);
return dReturn;
}
dReturn = initIOSVerify(publicKey, identoken, aud, sub);
//苹果登录授权不成功;
if (dReturn.getCode() != AccountCode.CODE_成功) {
return dReturn;
}
return dReturn;
}
/**
* 载入IOS Public Key
* @param kid
* @return
* @throws Exception
*/
private PublicKey loadIOSPublicKey(String kid) throws Exception{
String resp = UrlUtil.dSendGet(SystemParam.IOS_AUTH_URL);
if (resp == null) {
return null;
}
//System.out.println(resp);
JSONObject jsonObject = JSONObject.parseObject(resp);
String keys = jsonObject.getString("keys");
JSONArray jsonArray = JSONObject.parseArray(keys);
if (jsonArray.isEmpty()) {
return null;
}
for (Object object : jsonArray) {
JSONObject json = ((JSONObject) object);
if (json.getString("kid").equals(kid)) {
String n = json.getString("n");
String e = json.getString("e");
BigInteger modulus = new BigInteger(1, Base64.decodeBase64(n));
BigInteger publicExponent = new BigInteger(1, Base64.decodeBase64(e));
RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, publicExponent);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
}
}
return null;
}
/**
* 验证IOS登录
* @param key
* @param jwt - 就是 identityToken:授权用户的JWT凭证
* @param audience - audience就是APPID
* @param subject - subject 就是 就是userId
* @return
* @throws Exception
*/
private AccountCode initIOSVerify(PublicKey key,
String jwt, String audience, String subject) throws Exception {
// System.out.println("key:"+key);
// System.out.println("jwt:"+jwt);
// System.out.println("audience:"+audience);
// System.out.println("subject:"+subject);
AccountCode dReturn = new AccountCode();
dReturn.setCode(AccountCode.CODE_IOS_授权异常);
dReturn.setErrmsg(AccountCode.CODE_MESSAGE_IOS_授权异常);
JwtParser jwtParser = Jwts.parser().setSigningKey(key);
jwtParser.requireIssuer(SystemParam.IOS_ISS);
jwtParser.requireAudience(audience);
jwtParser.requireSubject(subject);
try {
Jws<Claims> claim = jwtParser.parseClaimsJws(jwt);
if (claim != null && claim.getBody().containsKey("auth_time")) {
dReturn.setCode(AccountCode.CODE_成功);
dReturn.setErrmsg(AccountCode.CODE_MESSAGE_成功);
return dReturn;
}
} catch (ExpiredJwtException e) {
dReturn.setCode(AccountCode.CODE_IOS_TOKEN过期);
dReturn.setErrmsg(AccountCode.CODE_MESSAGE_IOS_TOKEN过期);
} catch (SignatureException | MalformedJwtException e) {
dReturn.setCode(AccountCode.CODE_IOS_TOKEN非法);
dReturn.setErrmsg(AccountCode.CODE_MESSAGE_IOS_TOKEN非法);
log.error(""+e.getMessage(),e);
}
return dReturn;
}
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.auth0/jwks-rsa -->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>jwks-rsa</artifactId>
<version>0.9.0</version>
</dependency>
不知道问题在哪里 百度了半天也没太折腾明白哪位大神指点下错在哪里了
解决了 用这个base64
commons-codec commons-codec 1.10