Spring shell 2.0 如何使用掩码读取输入

Spring shell 2.0 how to read inputs with mask

有什么方法可以屏蔽 Spring Shell 中的用户输入 2.0.x ?

我需要从用户那里收集密码.. 没有找到任何 shell api 来做到这一点。

谢谢!!

发现 LineReader#readLine(msg,mask) 提供了那个选项。

您所要做的就是注入 LineReader bean。

如果您不想依赖第三方库,您可以随时进行标准 Java 控制台输入,例如:

private String inputPassword() {
  Console console = System.console();
  return new String(console.readPassword());
}

请注意,当 运行 this 在 IDE 中时,System.console() 可能为空。所以你应该处理如果 IDE 中的 运行 是你想要支持的东西(例如用于测试..),比如:

private String inputPassword() {
    Console console = System.console();
    // Console can be null when running in an IDE
    if (console == null) {
        System.out.println("WARNING - CAN'T HIDE PASSWORD");
        return new Scanner(System.in).next();
    }

    return new String(console.readPassword());
}