JsonRpcSslContextProvider.java

net.minecraft.server.jsonrpc.security.JsonRpcSslContextProvider

信息

  • 全限定名:net.minecraft.server.jsonrpc.security.JsonRpcSslContextProvider
  • 类型:public class
  • 包:net.minecraft.server.jsonrpc.security
  • 源码路径:src/main/java/net/minecraft/server/jsonrpc/security/JsonRpcSslContextProvider.java
  • 起始行号:L14
  • 职责:

    TODO

字段/常量

  • PASSWORD_ENV_VARIABLE_KEY

    • 类型: String
    • 修饰符: private static final
    • 源码定位: L15
    • 说明:

      TODO

  • PASSWORD_SYSTEM_PROPERTY_KEY

    • 类型: String
    • 修饰符: private static final
    • 源码定位: L16
    • 说明:

      TODO

  • log

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L17
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static SslContext createFrom(String keystorePath, String keystorePasswordFromServerProperties) @ L19

  • 方法名:createFrom
  • 源码定位:L19
  • 返回类型:SslContext
  • 修饰符:public static

参数:

  • keystorePath: String
  • keystorePasswordFromServerProperties: String

说明:

TODO

private static String getKeystorePassword(String keystorePasswordFromServerProperties) @ L33

  • 方法名:getKeystorePassword
  • 源码定位:L33
  • 返回类型:String
  • 修饰符:private static

参数:

  • keystorePasswordFromServerProperties: String

说明:

TODO

private static SslContext loadKeystoreFromPath(File keyStoreFile, String password) @ L43

  • 方法名:loadKeystoreFromPath
  • 源码定位:L43
  • 返回类型:SslContext
  • 修饰符:private static

参数:

  • keyStoreFile: File
  • password: String

说明:

TODO

public static void printInstructions() @ L57

  • 方法名:printInstructions
  • 源码定位:L57
  • 返回类型:void
  • 修饰符:public static

参数:

说明:

TODO

代码

public class JsonRpcSslContextProvider {
    private static final String PASSWORD_ENV_VARIABLE_KEY = "MINECRAFT_MANAGEMENT_TLS_KEYSTORE_PASSWORD";
    private static final String PASSWORD_SYSTEM_PROPERTY_KEY = "management.tls.keystore.password";
    private static final Logger log = LogUtils.getLogger();
 
    public static SslContext createFrom(String keystorePath, String keystorePasswordFromServerProperties) throws Exception {
        if (keystorePath.isEmpty()) {
            throw new IllegalArgumentException("TLS is enabled but keystore is not configured");
        } else {
            File file = new File(keystorePath);
            if (file.exists() && file.isFile()) {
                String keystorePassword = getKeystorePassword(keystorePasswordFromServerProperties);
                return loadKeystoreFromPath(file, keystorePassword);
            } else {
                throw new IllegalArgumentException("Supplied keystore is not a file or does not exist: '" + keystorePath + "'");
            }
        }
    }
 
    private static String getKeystorePassword(String keystorePasswordFromServerProperties) {
        String keystorePassword = System.getenv().get("MINECRAFT_MANAGEMENT_TLS_KEYSTORE_PASSWORD");
        if (keystorePassword != null) {
            return keystorePassword;
        } else {
            String systemPropertyKeystorePassword = System.getProperty("management.tls.keystore.password", null);
            return systemPropertyKeystorePassword != null ? systemPropertyKeystorePassword : keystorePasswordFromServerProperties;
        }
    }
 
    private static SslContext loadKeystoreFromPath(File keyStoreFile, String password) throws Exception {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
 
        try (InputStream keystoreStream = new FileInputStream(keyStoreFile)) {
            keyStore.load(keystoreStream, password.toCharArray());
        }
 
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, password.toCharArray());
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);
        return SslContextBuilder.forServer(keyManagerFactory).trustManager(trustManagerFactory).build();
    }
 
    public static void printInstructions() {
        log.info("To use TLS for the management server, please follow these steps:");
        log.info("1. Set the server property 'management-server-tls-enabled' to 'true' to enable TLS");
        log.info("2. Create a keystore file of type PKCS12 containing your server certificate and private key");
        log.info("3. Set the server property 'management-server-tls-keystore' to the path of your keystore file");
        log.info(
            "4. Set the keystore password via the environment variable 'MINECRAFT_MANAGEMENT_TLS_KEYSTORE_PASSWORD', or system property 'management.tls.keystore.password', or server property 'management-server-tls-keystore-password'"
        );
        log.info("5. Restart the server to apply the changes.");
    }
}

引用的其他类

  • LoggedChatMessage

    • 引用位置: 方法调用
    • 关联成员: System.getProperty(), System.getenv()
  • EventLogDirectory

    • 引用位置: 参数/构造调用
    • 关联成员: File()