1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.security.support;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.security.GeneralSecurityException;
22 import java.security.KeyStore;
23
24 import org.springframework.core.io.FileSystemResource;
25 import org.springframework.core.io.Resource;
26 import org.springframework.util.StringUtils;
27
28
29
30
31
32
33
34 public abstract class KeyStoreUtils {
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public static KeyStore loadDefaultKeyStore() throws GeneralSecurityException, IOException {
50 Resource location = null;
51 String type = null;
52 String password = null;
53 String locationProperty = System.getProperty("javax.net.ssl.keyStore");
54 if (StringUtils.hasLength(locationProperty)) {
55 File f = new File(locationProperty);
56 if (f.exists() && f.isFile() && f.canRead()) {
57 location = new FileSystemResource(f);
58 }
59 String passwordProperty = System.getProperty("javax.net.ssl.keyStorePassword");
60 if (StringUtils.hasLength(passwordProperty)) {
61 password = passwordProperty;
62 }
63 type = System.getProperty("javax.net.ssl.keyStoreType");
64 }
65
66 KeyStoreFactoryBean factoryBean = new KeyStoreFactoryBean();
67 factoryBean.setLocation(location);
68 factoryBean.setPassword(password);
69 factoryBean.setType(type);
70 factoryBean.afterPropertiesSet();
71 return factoryBean.getObject();
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public static KeyStore loadDefaultTrustStore() throws GeneralSecurityException, IOException {
92 Resource location = null;
93 String type = null;
94 String password = null;
95 String locationProperty = System.getProperty("javax.net.ssl.trustStore");
96 if (StringUtils.hasLength(locationProperty)) {
97 File f = new File(locationProperty);
98 if (f.exists() && f.isFile() && f.canRead()) {
99 location = new FileSystemResource(f);
100 }
101 String passwordProperty = System.getProperty("javax.net.ssl.trustStorePassword");
102 if (StringUtils.hasLength(passwordProperty)) {
103 password = passwordProperty;
104 }
105 type = System.getProperty("javax.net.ssl.trustStoreType");
106 }
107 else {
108 String javaHome = System.getProperty("java.home");
109 location = new FileSystemResource(javaHome + "/lib/security/jssecacerts");
110 if (!location.exists()) {
111 location = new FileSystemResource(javaHome + "/lib/security/cacerts");
112 }
113 }
114
115 KeyStoreFactoryBean factoryBean = new KeyStoreFactoryBean();
116 factoryBean.setLocation(location);
117 factoryBean.setPassword(password);
118 factoryBean.setType(type);
119 factoryBean.afterPropertiesSet();
120 return factoryBean.getObject();
121 }
122
123 }