jwt解析验证token-RS256

package com.irootech.customercloud.common.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwsHeader;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.Jwts;
import org.apache.commons.codec.binary.Base64;

import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;

public class JwtForRsUtil {
    /**
     * 获取PublicKey对象
     *
     * @param publicKeyBase64
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     */
    public static PublicKey getPublicKey(String publicKeyBase64)
            throws NoSuchAlgorithmException, InvalidKeySpecException {
        String pem = publicKeyBase64.replaceAll("\\-*BEGIN PUBLIC KEY\\-*", "").replaceAll("\\-*END PUBLIC KEY\\-*", "")
                .trim();
        java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(pem));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(pubKeySpec);
        //System.out.println(publicKey);
        return publicKey;
    }

    /**
     * 验证jwt
     *
     * @param token
     * @return
     * @throws Exception
     */
    public static boolean verifyToken(String token) throws Exception {
        String accessTokenKey=Constant.ACCESSTOKEN_PUBLICKEY;
        String idTokenKey=Constant.IDTOKEN_PUBLICKEY;
        try {
            PublicKey publicKey = getPublicKey(accessTokenKey);
            Jwt<JwsHeader, Claims> parseClaimsJwt = Jwts.parser().setSigningKey(publicKey).parseClaimsJws(token);

            //System.out.println("1==="+parseClaimsJwt);
            //System.out.println("2===="+parseClaimsJwt.getHeader());
            //System.out.println("3==="+parseClaimsJwt.getBody());
        } catch (Exception e) {
            return false;
            //throw new RuntimeException(e);
            //e.printStackTrace();
        }
        return true;
    }

    public static void main(String[] args) {
        String idToken = "eyJraWQiOiJvREN6ckQ1djlPbG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MmI4Yi1iYmIxLTQwOTgtYjdkMC02YTYyOGFmZWUwYTciLCJldmVudF9pZCI6IjAxZGI0MWE5LTM4MjItNDlkMC04OTAxLTAyNWU4YTljMmEyNSIsInRva2VuX3VzZSI6ImFjY2VzcyIsInNjb3BlIjoiYXdzLmNvZ25pdG8uc2lnbmluLnVzZXIuYWRtaW4iLCJhdXRoX3RpbWUiOjE1NzI1MDU0MzEsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC5ldS1jZW50cmFsLTEuYW1hem9uYXdzLmNvbVwvZXUtY2VudHJhbC0xX2pHWGV4ZlhqNSIsImV4cCI6MTU3MjUxMjk1NCwiaWF0IjoxNTcyNTA5MzU0LCJqdGkiOiIyYzkwYjQ5Zi01YWY0LTQwYWEtOTQ3ZS03NGM4YmQ4YzYyZDAiLCJjbGllbnRfaWQiOiIzNmVzaTkzNWF2cDVkdTFzbmZvYmxpdGE2IiwidXNlcm5hbWUiOiJjY2RkMmI4Yi1iYmIxLTQwOTgtYjdkMC02YTYyOGFmZWUwYTcifQ.JJJNu-_pLqpkSXXb2d8G0-jKYX_zwmp3LU2DWMl7Tfdr_ltL3ssckQpYNn1h2HIG-QvXSVzKTABFVWCc47Pm3Vdrh_6ChVKpCmuuvxQ2v9nE5pr-uV6nlqipnidk5w2i5QvkCl_J8U2VMJfeNm--WNibMfkIiXFc2Dz2-6tOEc5UPqgLyxzDQ8nuX2-uy3x1cwylzuqwXwEu-prABkfW45g_uXAeKtspsad2P9vUPMiwdyuasVG8zJSILRPsDuicGOqL4c7kj2QXcOS2nAvXP8swxXj9syrZpbsVtp6JX87f43CH4pTsL9pR_2bmGJriT8umh2arLbglMHEp8MPaeA";
        try {
            verifyToken(accToken);
            stampToDate("1572423828");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

package com.irootech.customercloud.common.util;

import java.util.UUID;

public class Constant {
    public static final String IDTOKEN_PUBLICKEY ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmFFG9jESksMVP/NjaAoT"
            +"KcDuMo5oA+2b+bNdm6H1+qJKIFfnwIaIJE0aMnqlLgEbbH60uSD8cTtsXnEYoQxf"
            +"J4QO8YMd8S7f56hhW3rJwRpgYT2la4aJYdQLxFop53Fx3QYUqG2IPYtjLWqRZSL9"
            +"KWSb29mtP+NveNqjeS7XWr7HkJ9C8lHkhQ9AhTj2IsU17NbAMz8Q9mAqTX0VXBtf"
            +"xFC6QXVa9DqkCFprPGpX1p8vfPOc/1iF+pxebygcsuuw0VoHV4BjHZclQP3WO17V"
            +"rQXuGgtEeJdDPfB6U/8DRc+8Fxd4tRIX1OoD7cBBhmtiKMi4E0YU0O/OfayPN+bS"
            +"/QIDAQAB";
    public static final String ACCESSTOKEN_PUBLICKEY ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk9wMD3SjN+0BK1VazW6R"
            +"i1serDn3g7JZQ9PFvAzPZfS2h+AX6xQ+wl+FO8cs5NV7VpG0/KsrSazPBOuEIy5v"
            +"un9srE9B7cPk+PA/TaHkneQ/7F5ZqQFS8A80KRBrZmU4G599S2zBaURIU74zOadW"
            +"t+Kea8nyfSQI3R0XgRlPElcX04qluYxloJ0gC2A8v1AKa3HWZUm09SYyYMPhoC44"
            +"z6j8h08pCFT/wDxBUo1rmhvjrW95ynOnzlzTaBJMe4uoWnKKktLFcoJ+GlOY/F6x"
            +"km4ilp0qJNB9SPxDszIcNnZMr6GPxf2uelUNXl1Vyv79oGQ7iG7IkVj3+s/VET2X"
            +"lQIDAQAB";
}

你可能感兴趣的:(java)