【问题标题】:How to escape "@" in properties file spring boot如何在属性文件spring boot中转义“@”
【发布时间】:2019-06-30 16:41:08
【问题描述】:

我有一个连接到 salesforce 的 spring boot 应用程序以从 salesforce 获取数据并将数据推送到 SQL 服务器数据库中。 我试图从 application.properties 文件中获取所有属性。 我的密码有@,因此在执行期间读取该属性时,我得到“example”作为我的密码,而不是“example@country”

Eg : 
Pwd = example@country

我检查了一些有用的链接,但没有运气,感谢任何帮助

https://docs.oracle.com/cd/E23095_01/Platform.93/ATGProgGuide/html/s0204propertiesfileformat01.html

用于获取配置道具的实用程序包。

                package SFToJava.Utils;

                import org.springframework.beans.factory.annotation.Autowired;
                import org.springframework.context.annotation.Configuration;
                import org.springframework.core.env.Environment;
                @Configuration
                public class configUtility {
                    @Autowired
                    private Environment env;

                    public String getProperty(String pPropertyKey) {
                        return env.getProperty(pPropertyKey);
                    }
                }

属性文件:

password = example@country

我在我的服务类中获取数据如下:

            loginParams.add(new BasicNameValuePair("client_id",configUtil.getProperty("consumerKey")));
            loginParams.add(new BasicNameValuePair("client_secret", configUtil.getProperty("consumerSecret")));
            loginParams.add(new BasicNameValuePair("username", configUtil.getProperty("username")));
            loginParams.add(new BasicNameValuePair("password", configUtil.getProperty("password")));

我希望密码在解析到列表时应该有“example@country”,而不是只得到“example”。

仅供参考。可以直接使用下面的密码连接到 Salesforce

            private static final String username = "******";
            private static final String password = "example@coutnry";
            private static final String consumerKey = "*************8";
            private static final String consumerSecret = "************";

loginParams.add(new BasicNameValuePair("client_id", consumerKey));
        loginParams.add(new 
BasicNameValuePair("client_secret", consumerSecret));
        loginParams.add(new 
BasicNameValuePair("grant_type", "password"));
        loginParams.add(new 
BasicNameValuePair("username", username));
        loginParams.add(new 
BasicNameValuePair("password", password));

【问题讨论】:

  • 你试过\@或\\@吗?
  • 你用的是什么版本的spring boot?我不必转义@ 字符。它适用于@\@。使用 spring boot 2.1.4 测试。
  • 我正在使用 2.1.6,我尝试了 \@ 和 \\@ 并且我只是得到错误说明:此行有多个标记 - '@' 处缺少 LineBreak - '\' 处缺少 LineBreak - “用户名”是未知属性

标签: java spring-boot properties-file


【解决方案1】:

通常您不必转义@,或者您可以在.properties 文件中使用转义的\\@

mail.address=user\\@domain.com

但我在使用它进行身份验证时遇到了一些问题。在这种情况下,您可能希望将编码的%40 用于@

# replace @ in email with %40
mail.address=user%40domain.com

【讨论】:

    猜你喜欢
    • 2018-09-28
    • 2019-01-11
    • 2012-10-21
    • 1970-01-01
    • 2016-04-29
    • 2018-03-01
    • 1970-01-01
    • 2016-09-21
    • 2016-08-15
    相关资源
    最近更新 更多