在使用spring-config时,读取ssh文件报错,Illegal base64 character 2e如何解?

在使用spring-config时,读取ssh文件报错,Illegal base64 character 2e。
报错内容如下:

java.lang.IllegalArgumentException: Illegal base64 character 2e
    at java.util.Base64$Decoder.decode0(Base64.java:714) ~[na:1.8.0_231]
    at java.util.Base64$Decoder.decode(Base64.java:526) ~[na:1.8.0_231]
    at java.util.Base64$Decoder.decode(Base64.java:549) ~[na:1.8.0_231]
    at org.apache.sshd.common.config.keys.PublicKeyEntryDataResolver.decodeEntryKeyData(PublicKeyEntryDataResolver.java:53) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.apache.sshd.common.config.keys.PublicKeyEntry.parsePublicKeyEntry(PublicKeyEntry.java:408) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.apache.sshd.common.config.keys.AuthorizedKeyEntry.parseAuthorizedKeyEntry(AuthorizedKeyEntry.java:318) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.apache.sshd.common.config.keys.AuthorizedKeyEntry.parseAuthorizedKeyEntry(AuthorizedKeyEntry.java:273) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.eclipse.jgit.internal.transport.sshd.KnownHostEntryReader.parseHostEntry(KnownHostEntryReader.java:139) ~[org.eclipse.jgit.ssh.apache-5.13.1.202206130422-r.jar:5.13.1.202206130422-r]
    at org.eclipse.jgit.internal.transport.sshd.KnownHostEntryReader.lambda$0(KnownHostEntryReader.java:79) ~[org.eclipse.jgit.ssh.apache-5.13.1.202206130422-r.jar:5.13.1.202206130422-r]
    at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_231]
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_231]
    at java.util.stream.ReferencePipeline$Head.forEachOrdered(ReferencePipeline.java:590) ~[na:1.8.0_231]
    at org.eclipse.jgit.internal.transport.sshd.KnownHostEntryReader.readFromFile(KnownHostEntryReader.java:70) ~[org.eclipse.jgit.ssh.apache-5.13.1.202206130422-r.jar:5.13.1.202206130422-r]
    at org.eclipse.jgit.internal.transport.sshd.OpenSshServerKeyDatabase$HostKeyFile.reload(OpenSshServerKeyDatabase.java:588) ~[org.eclipse.jgit.ssh.apache-5.13.1.202206130422-r.jar:5.13.1.202206130422-r]
    at org.eclipse.jgit.internal.transport.sshd.OpenSshServerKeyDatabase$HostKeyFile.get(OpenSshServerKeyDatabase.java:575) ~[org.eclipse.jgit.ssh.apache-5.13.1.202206130422-r.jar:5.13.1.202206130422-r]
    at org.eclipse.jgit.internal.transport.sshd.OpenSshServerKeyDatabase.lookup(OpenSshServerKeyDatabase.java:183) ~[org.eclipse.jgit.ssh.apache-5.13.1.202206130422-r.jar:5.13.1.202206130422-r]
    at org.eclipse.jgit.internal.transport.sshd.JGitServerKeyVerifier.lookup(JGitServerKeyVerifier.java:72) ~[org.eclipse.jgit.ssh.apache-5.13.1.202206130422-r.jar:5.13.1.202206130422-r]
    at org.eclipse.jgit.internal.transport.sshd.JGitClientSession.resolveAvailableSignaturesProposal(JGitClientSession.java:275) ~[org.eclipse.jgit.ssh.apache-5.13.1.202206130422-r.jar:5.13.1.202206130422-r]
    at org.apache.sshd.common.session.helpers.AbstractSession.resolveAvailableSignaturesProposal(AbstractSession.java:2356) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.apache.sshd.common.session.helpers.AbstractSession.sendKexInit(AbstractSession.java:2279) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.eclipse.jgit.internal.transport.sshd.JGitClientSession.sendKexInit(JGitClientSession.java:183) ~[org.eclipse.jgit.ssh.apache-5.13.1.202206130422-r.jar:5.13.1.202206130422-r]
    at org.apache.sshd.client.session.AbstractClientSession.initializeKeyExchangePhase(AbstractClientSession.java:316) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.apache.sshd.client.session.ClientSessionImpl.<init>(ClientSessionImpl.java:112) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.eclipse.jgit.internal.transport.sshd.JGitClientSession.<init>(JGitClientSession.java:97) ~[org.eclipse.jgit.ssh.apache-5.13.1.202206130422-r.jar:5.13.1.202206130422-r]
    at org.eclipse.jgit.internal.transport.sshd.JGitSshClient$JGitSessionFactory.doCreateSession(JGitSshClient.java:384) ~[org.eclipse.jgit.ssh.apache-5.13.1.202206130422-r.jar:5.13.1.202206130422-r]
    at org.apache.sshd.client.session.SessionFactory.doCreateSession(SessionFactory.java:31) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.apache.sshd.common.session.helpers.AbstractSessionFactory.createSession(AbstractSessionFactory.java:47) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.apache.sshd.common.session.helpers.AbstractSessionIoHandler.sessionCreated(AbstractSessionIoHandler.java:40) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.apache.sshd.common.io.nio2.Nio2Connector$ConnectionCompletionHandler.onCompleted(Nio2Connector.java:151) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.apache.sshd.common.io.nio2.Nio2Connector$ConnectionCompletionHandler.onCompleted(Nio2Connector.java:116) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at org.apache.sshd.common.io.nio2.Nio2CompletionHandler.lambda$completed$0(Nio2CompletionHandler.java:38) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_231]
    at org.apache.sshd.common.io.nio2.Nio2CompletionHandler.completed(Nio2CompletionHandler.java:37) ~[sshd-osgi-2.7.0.jar:2.7.0]
    at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126) ~[na:1.8.0_231]
    at sun.nio.ch.Invoker$2.run(Invoker.java:218) ~[na:1.8.0_231]
    at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112) ~[na:1.8.0_231]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_231]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_231]
    at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_231]

我知道这是我的ssh_host文件中带有换行符,但这种文件在生产环境不能随意更改,在代码中如何解决这个问题呢?
我曾经尝试过如下配置,但未奏效

@Configuration
public class Base64Config {
    @Bean
    public Base64.Decoder decoder(){
        return Base64.getMimeDecoder();
    }
}
阅读 1k
avatarAI BotBETA

你的问题是在使用Spring Boot读取SSH配置文件时,遇到了非法base64字符的问题。这个问题的原因可能是SSH配置文件中的某些内容被错误地解析为base64编码,而实际上这些内容并不是有效的base64编码。

在你的问题中,你提到SSH配置文件中的内容可能包含换行符,这可能是导致问题的原因。Base64编码要求输入的数据只能是ASCII字符,且不能包含任何换行符或其他非标准字符。

解决这个问题的一种方法是确保你的SSH配置文件中的所有内容都是有效的Base64编码,并且不包含任何非标准字符。然而,如果你不能更改SSH配置文件的内容,你可能需要在读取和解析这些内容时做一些特殊的处理。

下面是一种可能的解决方案:

import java.nio.charset.StandardCharsets;
import java.util.Base64;

@Configuration
public class SshConfig {
    @Value("${ssh.config}")
    private String sshConfig;

    @Bean
    public SshConfig getSshConfig() {
        String[] lines = sshConfig.split("\n");
        StringBuilder sb = new StringBuilder();
        for (String line : lines) {
            if (!line.isEmpty() && !line.startsWith("#")) { // 忽略注释和空行
                String value = line.split(" ")[1]; // 假设配置格式为 "Key Value"
                sb.append(value.replaceAll("\n", "")); // 移除换行符
                sb.append(" ");
            }
        }
        String base64Str = sb.toString().trim(); // 去除最后的空格
        byte[] decodedBytes = Base64.getDecoder().decode(base64Str);
        return new SshConfig(new String(decodedBytes, StandardCharsets.UTF_8));
    }
}

在这个例子中,我们首先将SSH配置文件的内容分割成多行,然后忽略空行和注释(假设配置文件的格式为"Key Value",你可以根据实际情况调整)。然后,我们移除每行的换行符,并将所有行的内容合并成一个字符串。最后,我们将这个字符串解码为字节数组,然后将其转换为字符串。这样就可以避免非法base64字符的问题了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏