mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add KeyManagerUtil
This commit is contained in:
parent
a37fbf8aa1
commit
143af4258d
@ -14,17 +14,18 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.dromara.hutool.crypto;
|
package org.dromara.hutool.core.net.ssl;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.exception.HutoolException;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.crypto.provider.GlobalProviderFactory;
|
|
||||||
|
|
||||||
import javax.net.ssl.KeyManager;
|
import javax.net.ssl.KeyManager;
|
||||||
import javax.net.ssl.KeyManagerFactory;
|
import javax.net.ssl.KeyManagerFactory;
|
||||||
import java.security.*;
|
import java.security.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link KeyManager}相关工具
|
* {@link KeyManager}相关工具<br>
|
||||||
|
* 此工具用于读取和使用数字证书、对称密钥等相关信息
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
@ -32,13 +33,22 @@ import java.security.*;
|
|||||||
public class KeyManagerUtil {
|
public class KeyManagerUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取{@link KeyManagerFactory},使用全局算法提供者
|
* 获取{@link KeyManagerFactory}
|
||||||
*
|
*
|
||||||
* @param algorithm 算法,{@code null}表示默认算法,如SunX509
|
|
||||||
* @return {@link KeyManagerFactory}
|
* @return {@link KeyManagerFactory}
|
||||||
*/
|
*/
|
||||||
public static KeyManagerFactory getKeyManagerFactory(final String algorithm) {
|
public static KeyManagerFactory getDefaultKeyManagerFactory() {
|
||||||
return getKeyManagerFactory(algorithm, GlobalProviderFactory.getProvider());
|
return getDefaultKeyManagerFactory(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取{@link KeyManagerFactory}
|
||||||
|
*
|
||||||
|
* @param provider 算法提供者,{@code null}使用JDK默认
|
||||||
|
* @return {@link KeyManagerFactory}
|
||||||
|
*/
|
||||||
|
public static KeyManagerFactory getDefaultKeyManagerFactory(final Provider provider) {
|
||||||
|
return getKeyManagerFactory(null, provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -56,23 +66,36 @@ public class KeyManagerUtil {
|
|||||||
try {
|
try {
|
||||||
return null == provider ? KeyManagerFactory.getInstance(algorithm) : KeyManagerFactory.getInstance(algorithm, provider);
|
return null == provider ? KeyManagerFactory.getInstance(algorithm) : KeyManagerFactory.getInstance(algorithm, provider);
|
||||||
} catch (final NoSuchAlgorithmException e) {
|
} catch (final NoSuchAlgorithmException e) {
|
||||||
throw new CryptoException(e);
|
throw new HutoolException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从KeyStore中获取{@link KeyManager}列表
|
* 从KeyStore中获取{@link KeyManager}列表
|
||||||
*
|
*
|
||||||
* @param keyStore KeyStore
|
* @param keyStore KeyStore
|
||||||
* @param password 密码
|
* @param password 密码
|
||||||
* @return {@link KeyManager}列表
|
* @return {@link KeyManager}列表
|
||||||
*/
|
*/
|
||||||
public static KeyManager[] getKeyManagers(final KeyStore keyStore, final char[] password) {
|
public static KeyManager[] getDefaultKeyManagers(final KeyStore keyStore, final char[] password) {
|
||||||
final KeyManagerFactory keyManagerFactory = getKeyManagerFactory(null);
|
return getKeyManagers(keyStore, password, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从KeyStore中获取{@link KeyManager}列表
|
||||||
|
*
|
||||||
|
* @param keyStore KeyStore
|
||||||
|
* @param password 密码
|
||||||
|
* @param algorithm 算法,{@code null}表示默认算法,如SunX509
|
||||||
|
* @param provider 算法提供者,{@code null}使用JDK默认
|
||||||
|
* @return {@link KeyManager}列表
|
||||||
|
*/
|
||||||
|
public static KeyManager[] getKeyManagers(final KeyStore keyStore, final char[] password, final String algorithm, final Provider provider) {
|
||||||
|
final KeyManagerFactory keyManagerFactory = getKeyManagerFactory(algorithm, provider);
|
||||||
try {
|
try {
|
||||||
keyManagerFactory.init(keyStore, password);
|
keyManagerFactory.init(keyStore, password);
|
||||||
} catch (final KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException e) {
|
} catch (final KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException e) {
|
||||||
throw new CryptoException(e);
|
throw new HutoolException(e);
|
||||||
}
|
}
|
||||||
return keyManagerFactory.getKeyManagers();
|
return keyManagerFactory.getKeyManagers();
|
||||||
}
|
}
|
@ -28,7 +28,8 @@ import java.security.NoSuchAlgorithmException;
|
|||||||
import java.security.Provider;
|
import java.security.Provider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link TrustManager}相关工具类
|
* {@link TrustManager}相关工具类<br>
|
||||||
|
* 此工具用于管理可信任的证书等信息
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user