Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring MVC中设置系统属性_Java_Spring_Spring Mvc_Truststore - Fatal编程技术网

Java 如何在Spring MVC中设置系统属性

Java 如何在Spring MVC中设置系统属性,java,spring,spring-mvc,truststore,Java,Spring,Spring Mvc,Truststore,我正在尝试按如下方式设置信任库和密钥库的系统属性: @WebListener public abstract class ContextListenerExample implements ServletContextListener { public void contextInitialized(ServletContextEvent e){ System.setProperty("javax.net.ssl.trustStore", "C:\\Users\\trus

我正在尝试按如下方式设置信任库和密钥库的系统属性:

@WebListener
public abstract class ContextListenerExample implements ServletContextListener {
    public void contextInitialized(ServletContextEvent e){
        System.setProperty("javax.net.ssl.trustStore", "C:\\Users\\trustCert.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "test123");
        System.setProperty("javax.net.ssl.trustStoreType", "jks");
        System.setProperty("javax.net.ssl.keyStore", "C:\\Users\\keyCert.p12");
        System.setProperty("javax.net.ssl.keyStorePassword", "keystore");
        System.setProperty("javax.net.ssl.keyStoreType", "keystoreType");
  }
}
我遵循了这个示例,但当我运行我的应用程序时,它从未到达上下文化方法。此外,我必须将ContextListenerExample类更改为抽象类。是否有其他方法设置系统属性,或者我是否缺少需要修改的其他文件

我添加了一个新的文件SslConfiguration类:

@Configuration
public class SslConfiguration {
    @Value("${C:\\Users\\A21\\src\\main\\java\\org\\test\\certificates\\test.jks}")
    private Resource trustStore;

    @Value("test123")
    private String trustStorePassword;

    @Value("${C:\\Users\\A21\\src\\main\\java\\org\\test\\certificates\\test.p12}")
    private Resource keyStore;

    @Value("teststore")
    private String keyStorePassword;

    @Bean
    RestTemplate restTemplate() throws Exception {
        SSLContext sslContext = new SSLContextBuilder()
                .loadKeyMaterial(
                        keyStore.getFile(),
                        keyStorePassword.toCharArray(),
                        keyStorePassword.toCharArray())
                .loadTrustMaterial(
                        trustStore.getURL(),
                        trustStorePassword.toCharArray(),
                        // use this for self-signed certificates only:
                        new TrustSelfSignedStrategy())
                .build();

        SSLConnectionSocketFactory socketFactory =
                new SSLConnectionSocketFactory(sslContext);
        HttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(socketFactory).build();
        HttpComponentsClientHttpRequestFactory factory =
                new HttpComponentsClientHttpRequestFactory(httpClient);
        return new RestTemplate(factory);
    }
}

抽象类不能被实例化,所以这可能是从未调用此代码的原因。不要使
ContextListenerExample
类抽象,而是尝试实现
ServletContextListener
接口中声明的另一个方法:

public void contextDestroyed(ServletContextEvent e) {
  // you can just leave it empty
}

这确实允许我在实现contextDestroyed后访问ContextInitialized中包含的代码,但它似乎没有注册trustStore。这是另一个问题。可能在您指定这些属性时,Spring已经初始化了密钥库/信任库。例如,您可以尝试所描述的不同方法。不,我尝试将trustStore和keyStore设置为资源元素,正如您链接到我的回答页面中所提供的那样,但创建“RestTemplate”bean没有成功。我已经编辑了我的原始问题,以合并我添加到项目中的新类。