Files
metaloop/src/main/java/com/metalloop/modules/auth/controller/RsaPublicKeyController.java
T
2026-09-11 17:34:14 +08:00

38 lines
1.2 KiB
Java

package com.metalloop.modules.auth.controller;
import com.metalloop.common.auth.encrypt.KeyProperties;
import com.metalloop.common.auth.encrypt.KeyStoreKeyFactory;
import org.springframework.web.bind.annotation.ResponseBody;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;
/**
* 获取公钥端点
*
* @author zhaowenhao
* @since 2023-03-19
*/
public class RsaPublicKeyController {
private final KeyProperties keyProperties;
public RsaPublicKeyController(KeyProperties keyProperties) {
this.keyProperties = keyProperties;
}
@ResponseBody
public String getPublicKey() {
if (keyProperties != null) {
KeyPair keyPair = new KeyStoreKeyFactory(keyProperties.getKeyStore().getLocation(), keyProperties.getKeyStore().getSecret().toCharArray()).getKeyPair(keyProperties.getKeyStore().getAlias());
PublicKey publicKey = keyPair.getPublic();
RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
byte[] publicKeyBytes = rsaPublicKey.getEncoded();
return Base64.getEncoder().encodeToString(publicKeyBytes);
}
return "获取失败";
}
}