Spring的'Environment'变量忽略了.proprties文件中的反斜杠(\)

时间:2016-06-16 11:44:14

标签: java spring spring-annotations spring-web

我正在尝试使用@Configuration@PropertySource变量在Spring Environment java类中加载config.proprties文件数据。

示例:http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html 问题是,我有一个有价值的财产:

     serverName = abc\xyz

当我使用方法读取此属性时,

     String server= env.getProprty("serverName");
     System.out.print(server);

值打印为“abcxyz”。

请注意,我尝试使用双反斜杠,

     serverName = abc\\xyz

但它仍然只是从值字符串中忽略\。 我也不能使用正斜杠代替反斜杠。

你帮我解决了吗?在此先感谢!!

3 个答案:

答案 0 :(得分:0)

这是一个真正丑陋的黑客攻击,但您可以尝试使用unicode转义序列作为符号“\”,即“\ u005c”,而不是字符串值“abc \ xyz”使用“abc \ u005cxyz”。但话又将它转换为“abc \ xyz”,然后将“\”视为逃脱符号的开始。因此,如果第一个不起作用,您可以尝试将“abc \\ xyz”替换为“abc \ u005c \ u005cxyz”。看看第一个或第二个选项是否适合您。但事实上,我很惊讶简单的逃避“\\”并没有解决你的问题。如果全部失败,请尝试这个“abc \\\\ xyz” - 这是双重转义。

答案 1 :(得分:0)

我使用了spring 3.1.4-RELEASE,如果属性文件中的值包含' \\'则它可以使用。与serverName = abc \\ xyz

一样
/*
 * This class is part of the white paper entitled
 * "Digital Signatures for PDF documents"
 * written by Bruno Lowagie
 * 
 * For more info, go to: http://itextpdf.com/learn
 */
package signatures.chapter3;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;
import java.util.Properties;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.security.DigestAlgorithms;
import com.itextpdf.text.pdf.security.MakeSignature.CryptoStandard;
import com.itextpdf.text.pdf.security.OcspClient;
import com.itextpdf.text.pdf.security.OcspClientBouncyCastle;

public class C3_07_SignWithOCSP extends C3_01_SignWithCAcert {
    public static final String SRC = "src/main/resources/hello.pdf";
    public static final String DEST = "results/chapter3/hello_cacert_ocsp.pdf";

    public static void main(String[] args) throws IOException, GeneralSecurityException, DocumentException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("c:/home/blowagie/key.properties"));
        String path = properties.getProperty("PRIVATE");
        char[] pass = properties.getProperty("PASSWORD").toCharArray();

        BouncyCastleProvider provider = new BouncyCastleProvider();
        Security.addProvider(provider);
        KeyStore ks = KeyStore.getInstance("pkcs12", provider.getName());
        ks.load(new FileInputStream(path), pass);
        String alias = (String)ks.aliases().nextElement();
        PrivateKey pk = (PrivateKey) ks.getKey(alias, pass);
        Certificate[] chain = ks.getCertificateChain(alias);
        OcspClient ocspClient = new OcspClientBouncyCastle();
        C3_07_SignWithOCSP app = new C3_07_SignWithOCSP();
        app.sign(SRC, DEST, chain, pk, DigestAlgorithms.SHA256, provider.getName(), CryptoStandard.CMS, "Test", "Ghent", 
                null, ocspClient, null, 0);
    }

} 
C3_08_GetTsaUrl.java 
/*
 * This class is part of the white paper entitled
 * "Digital Signatures for PDF documents"
 * written by Bruno Lowagie
 * 
 * For more info, go to: http://itextpdf.com/learn
 */
package signatures.chapter3;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.Properties;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import com.itextpdf.text.pdf.security.CertificateUtil;

public class C3_08_GetTsaUrl {
    public static void main(String[] args) throws IOException, GeneralSecurityException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("c:/home/blowagie/key.properties"));
        String path = properties.getProperty("PRIVATE");
        char[] pass = properties.getProperty("PASSWORD").toCharArray();

        BouncyCastleProvider provider = new BouncyCastleProvider();
        Security.addProvider(provider);
        KeyStore ks = KeyStore.getInstance("pkcs12", provider.getName());
        ks.load(new FileInputStream(path), pass);
        String alias = (String)ks.aliases().nextElement();
        Certificate[] chain = ks.getCertificateChain(alias);        
        for (int i = 0; i < chain.length; i++) {
            X509Certificate cert = (X509Certificate)chain[i];
            System.out.println(String.format("[%s] %s", i, cert.getSubjectDN()));
            System.out.println(CertificateUtil.getTSAURL(cert));
        }
    }
}

答案 2 :(得分:0)

我将反斜杠而不是反斜杠存储在配置文件中。

在阅读时,我将其替换为双反斜杠。

SourcePath = C:/Users/Barani/Documents/SampleData/MyInputFile.txt

iTunes Store operation failed.
No suitable application records were found. Verify your bundle identifier 'org.cocoapods.GoogleToolboxForMac' is correct.

这对我来说正常工作。