IOS登录JWT验证io.jsonwebtoken.SignatureException: Unable to verify RSA signature using configured Public

xcfdsarfew 2021-08-15 18:14:06

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>

 

不知道问题在哪里 百度了半天也没太折腾明白哪位大神指点下错在哪里了

...全文
1460 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xcfdsarfew 2021-08-16
  • 打赏
  • 举报
回复 2

解决了 用这个base64

commons-codec commons-codec 1.10
LL1187740947 2021-10-21
  • 举报
回复
@xcfdsarfew 是这个吗?很急import org.apache.tomcat.util.codec.binary.Base64;
LL1187740947 2021-10-21
  • 举报
回复
@xcfdsarfew 找到了,是这个import org.apache.commons.codec.binary.Base64;
么么大先知 2023-12-01
  • 举报
回复
@xcfdsarfew 哥,真的行。我看了下用的是tomcat的base64,换了org.apache.commons的就行了

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧