Java >> Java tutorial >  >> Tag >> private

Nøglebytes kan kun angives for HMAC-signaturer. Angiv venligst en PublicKey- eller PrivateKey-instans

Jeg tror, ​​jeg har været i stand til at rette fejlen ved at parse publicKey som en RSAPublicKey . Nedenfor kan du se, hvordan jeg gjorde det.

 public static Optional<RSAPublicKey> getParsedPublicKey(){
       // public key content...excluding '---PUBLIC KEY---' and '---END PUBLIC KEY---'
        String PUB_KEY =System.getenv("PUBLIC_KEY") ; 

       // removes white spaces or char 20
        String PUBLIC_KEY = "";
          if (!PUB_KEY.isEmpty()) {
            PUBLIC_KEY = PUB_KEY.replace(" ", "");
        }

        try {
            byte[] decode = com.google.api.client.util.Base64.decodeBase64(PUBLIC_KEY);
            X509EncodedKeySpec keySpecX509 = new X509EncodedKeySpec(decode);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(keySpecX509);
            return Optional.of(pubKey);

        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            e.printStackTrace();
            System.out.println("Exception block | Public key parsing error ");
            return Optional.empty();
        }

Håber det hjælper :).


Dette kan gøres som nedenfor, generere et token ved hjælp af privat nøglesignering og parse krav med den offentlige nøgle

  @Configuration
  public class KeyGeneratorConfig {


    @Value("${jwt.privateKey}")
    private String privateKey; //Encoded private key string


    @Value("${jwt.publicKey}")
    private String publicKey;//Encoded public key string


    @Bean
    public PrivateKey generatePrivateKey() throws NoSuchAlgorithmException, InvalidKeySpecException {

        KeyFactory kf = KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec privKeySpecPKCS8 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey));
        return kf.generatePrivate(privKeySpecPKCS8);
    }

    @Bean
    public PublicKey generatePublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException {

        KeyFactory kf = KeyFactory.getInstance("RSA");
        X509EncodedKeySpec pubKeySpecX509EncodedKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey));
        return kf.generatePublic(pubKeySpecX509EncodedKeySpec);
    }
}

og generering af token og parsing kan udføres på denne måde

@Autowired
private PublicKey publicKey;

@Autowired
private PrivateKey privateKey;

private String doGenerateToken(Map claims) {
    return Jwts.builder()
            .setClaims(claims)
            .setExpiration(generateExpirationDate("token"))
            .signWith(SignatureAlgorithm.RS512, privateKey)
            .compact();
}

public Claims getClaimsFromToken(String token) throws ExpiredJwtException, UnsupportedJwtException,
        MalformedJwtException, SignatureException, IllegalArgumentException {
    Claims claims;

    claims = Jwts.parser()
            .setSigningKey(publicKey)
            .parseClaimsJws(token)
            .getBody();

    return claims;
}

Java tag