如何在 spring 启动时加密 user/pw 和 ftp 连接?

How to encrypt the user/pw and ftp connection in spring boot?

所以我有一个有效的 ftp 连接,我可以上传文件等等。但是,目前我将 pw/user 存储为硬编码值(我知道这不好)并想看看我是否可以以某种方式 hide/encrypt 它们。如果可能的话,我也想加密实际的 ftp 连接。任何 information/pointers 都会很棒!

private int ftpProcess(String serverName, String userName, String password) {
    FTPClient ftp = new FTPClient();
    int statusCode = 0;
    try {
        ftp.connect(serverName);
        // was required to add this passive mode in TC8 to connect to the mainframe
        ftp.enterLocalPassiveMode();

        ftp.login(userName, password);

        ftp.site("filetype=jes");

        submitJcl(ftp, "submitTest.txt", serverName);

        ftp.quit();
    } catch (Exception e) {
        logger.error(e);
        statusCode = 3;
    }
    return statusCode;
}

您可以使用 Jasypt 加密 application.properties 中的值。

详细说明见下文link:

如果您已经在使用 Spring,您可以添加 org.springframework.boot:spring-boot-starter-security 依赖项并使用 BCryptPasswordEncoder 对值进行编码:

BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String enc = encoder.encode(value);

文档在这里:

这是一个非常有用的教程: