Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

    • AI(Hutool-ai)

    • 注解(annotation)

    • 数组(array)

    • JavaBean(bean)

    • 缓存(cache)

    • 类加载器(classloader)

    • 编码解码(codec)

    • 集合(collection)

    • 比较器(comparator)

    • 压缩(compress)

    • 类型转换(convert)

    • 数据(data)

    • 日期时间(date)

    • 异常(exception)

    • 函数(func)

    • IO流(io)

    • 语言特性(lang)

    • Map(map)

    • 数字数学(math)

    • 网络(net)

      • 网络
      • NetUtil
      • Ipv4Util
      • Ipv6Util
      • MacAddressUtil
      • KeyManagerUtil
        • 主要功能
        • 方法示例
          • 示例1:从JKS密钥库创建密钥管理器
          • 示例2:从PKCS12密钥库创建密钥管理器
          • 示例3:创建空密钥管理器
          • 示例4:获取默认密钥管理器
          • 示例5:从InputStream创建密钥管理器
          • 示例6:与SSLContext结合使用
        • 注意事项
      • SSL上下文工具-SSLContextUtil
      • 信任管理器工具-TrustManagerUtil
      • URL构建器-UrlBuilder
      • URL协议工具-UrlProtocolUtil
      • URL查询工具-UrlQueryUtil
      • URL工具-UrlUtil
    • 对象池(pool)

    • 反射(reflect)

    • 正则(regex)

    • 服务提供(spi)

    • 聚合操作(stream)

    • 字符串文本(text)

    • 并发和线程(thread)

    • 树结构(tree)

    • 工具集合(util)

    • XML操作(xml)

  • 定时任务(hutool-cron)

  • 加密(hutool-crypto)

  • 数据库(hutool-db)

  • 扩展(hutool-extra)

  • HTTP(hutool-http)

  • 日志(hutool-log)

  • 配置文件(hutool-setting)

  • JSON(hutool-json)

  • Office办公(hutool-poi)

  • 套接字(hutool-socket)

  • GUI(hutool-swing)

  • 指南
  • 核心(hutool-core)
  • 网络(net)
Hutool
2026-01-23
目录

KeyManagerUtil

KeyManagerUtil是Hutool中用于处理密钥管理器的工具类,主要用于SSL/TLS相关的密钥管理,提供了丰富的密钥管理器操作功能,包括创建、加载、管理等。

# 主要功能

  • 密钥管理器创建:创建各种类型的密钥管理器
  • 密钥库加载:从密钥库(keystore)加载密钥
  • 支持多种密钥库类型:JKS, PKCS12, JCEKS等
  • 密码保护支持:支持密码保护的密钥库
  • 空密钥管理器:创建空的密钥管理器
  • 默认密钥管理器:获取系统默认密钥管理器
  • 密钥管理器数组:生成用于SSLContext的密钥管理器数组

# 方法示例

# 示例1:从JKS密钥库创建密钥管理器

从JKS格式的密钥库文件创建密钥管理器。

// 密钥库文件路径
String keystorePath = "path/to/keystore.jks";
// 密钥库密码
String keystorePassword = "password";
// 密钥别名
String keyAlias = "alias";
// 密钥密码
String keyPassword = "keyPassword";

// 创建密钥管理器
KeyManager[] keyManagers = KeyManagerUtil.createKeyManagers(keystorePath, keystorePassword.toCharArray(), keyAlias, keyPassword.toCharArray());
// 输出: 密钥管理器数组,可用于SSLContext

# 示例2:从PKCS12密钥库创建密钥管理器

从PKCS12格式的密钥库文件创建密钥管理器。

// 密钥库文件路径
String keystorePath = "path/to/keystore.p12";
// 密钥库密码
String keystorePassword = "password";

// 创建PKCS12密钥管理器
KeyManager[] keyManagers = KeyManagerUtil.createKeyManagers(keystorePath, keystorePassword.toCharArray(), "PKCS12");
// 输出: 密钥管理器数组

# 示例3:创建空密钥管理器

创建一个空的密钥管理器数组。

KeyManager[] emptyKeyManagers = KeyManagerUtil.createEmptyKeyManagers();
// 输出: 空的密钥管理器数组

# 示例4:获取默认密钥管理器

获取系统默认的密钥管理器。

KeyManager[] defaultKeyManagers = KeyManagerUtil.getDefaultKeyManagers();
// 输出: 系统默认的密钥管理器数组

# 示例5:从InputStream创建密钥管理器

从InputStream创建密钥管理器。

try (InputStream is = Files.newInputStream(Paths.get("path/to/keystore.jks"))) {
    KeyManager[] keyManagers = KeyManagerUtil.createKeyManagers(is, "password".toCharArray(), "alias", "keyPassword".toCharArray());
    // 输出: 密钥管理器数组
}

# 示例6:与SSLContext结合使用

将创建的密钥管理器用于SSLContext。

// 创建密钥管理器
KeyManager[] keyManagers = KeyManagerUtil.createKeyManagers("path/to/keystore.jks", "password".toCharArray(), "alias", "keyPassword".toCharArray());

// 创建SSLContext
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, null, null);
// 输出: 初始化好的SSLContext,可用于HttpsURLConnection等

# 注意事项

  1. 密钥库密码和密钥密码应该妥善保管,避免硬编码在代码中
  2. 密钥库文件应该设置适当的权限,避免未授权访问
  3. 支持的密钥库类型包括:JKS, PKCS12, JCEKS, DKS等
  4. KeyManagerUtil提供的方法都是线程安全的,可以在多线程环境下使用
  5. 创建密钥管理器时,需要确保密钥库文件存在且可访问
  6. 不同的密钥库类型需要使用对应的算法和密钥工厂
  7. 在使用完密钥管理器后,建议及时清理敏感信息(如密码数组)
  8. 对于自签名证书,需要结合TrustManagerUtil使用
MacAddressUtil
SSL上下文工具-SSLContextUtil

← MacAddressUtil SSL上下文工具-SSLContextUtil→

Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式