简体   繁体   English

如何在连接字符串中“转义”用户名:密码对?

[英]How do you 'escape' a username:password pair in a connection string?

We have a connection string that is built like so: 我们有一个像这样构建的连接字符串:

http://${i.user}:${i.pass}@${i.host}:22

However, if that users password contains an '@', then we end up with a string like this: 但是,如果该用户密码包含“@”,那么我们最终得到一个如下字符串:

http://user:p@ss@1.2.3.4:22

This obviously throws the script off. 这显然会抛弃脚本。 Is there a way to escape that first @, or something to that effect? 有没有办法逃脱第一个@,或者那种效果?

Thanks 谢谢

用“%40”替换用户或密码中的每个“@”

Characters like "@" in the user info portion of a URI have to be escaped. 必须转义URI的用户信息部分中的“@”等字符。 When parsing such a URI, the URI consumer should unescape any escaped characters: 在解析这样的URI时,URI使用者应该忽略任何转义字符:

import java.net.URI;
import java.net.URISyntaxException;

public class URITest {

    public static void main(String[] args) throws URISyntaxException {
        URI uri = new URI("http", "user:p@ss", "example.com", -1,
                null, null, null);
        System.out.println(uri.toString());

        URI uri2 = new URI(uri.toString());
        System.out.println(uri2.getUserInfo());
    }
}

On my system, this outputs: 在我的系统上,这输出:

http://user:p%40ss@example.com
user:p@ss

In the original question, it looks like you're not escaping the '@' when constructing the URI. 在最初的问题中,看起来你在构造URI时没有转义'@'。 In your comment to @leblma, it sounds like whatever is using the URI isn't unescaping the % sequence. 在你对@leblma的评论中,听起来像使用URI的任何事情都不会破坏%序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在URL连接字符串中定义用户名和密码? - How can I define username and password in the URL connection string? 如何在GitHub(Java)上隐藏MySQL连接字符串-用户名和密码 - How to hide MySQL Connection String- Username and Password on GitHub (Java) 如何使用Java在Mac Keychain中存储用户名/密码? - How do you store a username/password in the Mac Keychain using Java? 如何使用用户名/密码(无证书)通过https设置CXF / SOAP客户端连接 - How do I setup a CXF/SOAP client connection over https with username/password (no certificate) 索引 103 处格式错误的转义对:http://localhost:8025/oauth2db/oauth/token?grant_type=password&username=nithi5@gmail.com&password=nithi%" - Malformed escape pair at index 103: http://localhost:8025/oauth2db/oauth/token?grant_type=password&username=nithi5@gmail.com&password=nithi%" 如何在属性文件中转义冒号 (:)? - How do you escape colon (:) in Properties file? 没有用户名或密码连接到Oracle - Connection to Oracle without a username or password JDBC 连接 URL 中的用户名和密码 - Username & Password in JDBC Connection URL 如何在 Java 中转义字符串? - How do I escape a string in Java? 用户名和密码字符串比较问题 - Username & password string comparison problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM