---
title: "Spring,smppapi,apache mina, ssl快速实现安全的smpp(6)"
author: "lanmh"
date: "2008-07-21T15:41:59Z"
tags: ["Apache", "Java", "Mina", "Security", "Spring"]
slug: "spring-smppapi-apache-mina-ssl-smpp-6"
draft: false
lastmod: "2008-07-21T15:41:59Z"
id: 216958
url: "https://lanmh.iteye.com/blog/216958"
---

接上一篇： [http://618119.com/archives/2007/12/13/45.html](http://618119.com/archives/2007/12/13/45.html)

使用 [commons ssl](http://618119.com/tag/commons-ssl "标签 commons ssl 下的日志")生成 SSLContext ：

 

[view plain](http://618119.com/archives/2007/12/14/46.html)[copy to clipboard](http://618119.com/archives/2007/12/14/46.html)[print](http://618119.com/archives/2007/12/14/46.html)[?](http://618119.com/archives/2007/12/14/46.html)

01. package com.lizongbo.ssl;
02. import javax.net.ssl.SSLContext;
03. import java.security.GeneralSecurityException;
04. import java.io.IOException;
05. import javax.net.ssl.KeyManager;
06. import org.apache.commons.ssl.KeyMaterial;
07. public class SMPPSSLContextFactory {
08. private static final String PROTOCOL = “TLS”;
09. private static final String CA\_FILE = “ca.crt.properties”;
10. private static final String CERT\_FILE = “server.crt.properties”;
11. private static final String KEY\_FILE = “server.key.properties”;
12. private static final String CILENT\_FILE = “client.crt.properties”;;//”client.p12.properties”;
13. private static final String CILENT\_KEY\_FILE = “client.key.properties”;
14. private static final char\[] password =new char\[0] ;//”lizongbo”.toCharArray();
15. private static SSLContext serverInstance = null;
16. private static SSLContext clientInstance = null;
17. /\**
18. * Get SSLContext singleton.
19. \*
20. * @return SSLContext
21. * @throws java.security.GeneralSecurityException
22. \*
23. \*/
24. public static SSLContext getInstance(boolean server) throws
25. GeneralSecurityException, IOException {
26. SSLContext retInstance = null;
27. if (server) {
28. if (serverInstance == null) {
29. synchronized (SMPPSSLContextFactory.class) {
30. if (serverInstance == null) {
31. try {
32. serverInstance = createSMPPServerSSLContext();
33. }
34. catch (Exception ioe) {
35. throw new GeneralSecurityException(
36. “Can’t create Server SSLContext:” + ioe);
37. }
38. }
39. }
40. }
41. retInstance = serverInstance;
42. }
43. else {
44. if (clientInstance == null) {
45. synchronized (SMPPSSLContextFactory.class) {
46. if (clientInstance == null) {
47. clientInstance = createSMPPClientSSLContext();
48. }
49. }
50. }
51. retInstance = clientInstance;
52. }
53. return retInstance;
54. }
55. private static SSLContext createSMPPServerSSLContext() throws
56. GeneralSecurityException, IOException {
57. // ssl.setCheckHostname(false); // default setting is “false” for SSLServer
58. // ssl.setCheckExpiry(true); // default setting is “true” for SSLServer
59. // ssl.setCheckCRL(true); // default setting is “true” for SSLServer
60. // ssl.useStrongCiphers();
61. // return ssl.getSSLContext();
62. SSLContext sslContext = SSLContext.getInstance(PROTOCOL);
63. KeyMaterial km = new KeyMaterial(SMPPSSLContextFactory.class
64. .getResourceAsStream(CERT\_FILE),
65. SMPPSSLContextFactory.class
66. .getResourceAsStream(KEY\_FILE),
67. password);
68. sslContext.init( (KeyManager\[]) km.getKeyManagers(),
69. SMPPTrustManagerFactory.X509\_MANAGERS, null);
70. // System.out.println(”getCipherSuites ==” +
71. // java.util.Arrays.toString(sslContext.getServerSessionContext().
72. // getSupportedSSLParameters().
73. // getCipherSuites()));
74. return sslContext;
75. }
76. private static SSLContext createSMPPClientSSLContext() throws
77. GeneralSecurityException, IOException {
78. {
79. SSLContext context = SSLContext.getInstance(PROTOCOL);
80. KeyMaterial km = new KeyMaterial(SMPPSSLContextFactory.class
81. .getResourceAsStream(CILENT\_FILE),
82. SMPPSSLContextFactory.class
83. .getResourceAsStream(CILENT\_KEY\_FILE),
84. password);
85. context.init( (KeyManager\[]) km.getKeyManagers(),
86. SMPPTrustManagerFactory.X509\_MANAGERS, null);
87. return context;
88. }
89. }
90. }

```
package com.lizongbo.ssl;
import javax.net.ssl.SSLContext;
import java.security.GeneralSecurityException;
import java.io.IOException;
import javax.net.ssl.KeyManager;
import org.apache.commons.ssl.KeyMaterial;
public class SMPPSSLContextFactory {
private static final String PROTOCOL = “TLS”;
private static final String CA_FILE = “ca.crt.properties”;
private static final String CERT_FILE = “server.crt.properties”;
private static final String KEY_FILE = “server.key.properties”;
private static final String CILENT_FILE = “client.crt.properties”;;//”client.p12.properties”;
private static final String CILENT_KEY_FILE = “client.key.properties”;
private static final char[] password =new char[0] ;//”lizongbo”.toCharArray();
private static SSLContext serverInstance = null;
private static SSLContext clientInstance = null;
/**
* Get SSLContext singleton.
*
* @return SSLContext
* @throws java.security.GeneralSecurityException
*
*/
public static SSLContext getInstance(boolean server) throws
GeneralSecurityException, IOException {
SSLContext retInstance = null;
if (server) {
if (serverInstance == null) {
synchronized (SMPPSSLContextFactory.class) {
if (serverInstance == null) {
try {
serverInstance = createSMPPServerSSLContext();
}
catch (Exception ioe) {
throw new GeneralSecurityException(
“Can’t create Server SSLContext:” + ioe);
}
}
}
}
retInstance = serverInstance;
}
else {
if (clientInstance == null) {
synchronized (SMPPSSLContextFactory.class) {
if (clientInstance == null) {
clientInstance = createSMPPClientSSLContext();
}
}
}
retInstance = clientInstance;
}
return retInstance;
}
private static SSLContext createSMPPServerSSLContext() throws
GeneralSecurityException, IOException {
// ssl.setCheckHostname(false); // default setting is “false” for SSLServer
// ssl.setCheckExpiry(true); // default setting is “true” for SSLServer
// ssl.setCheckCRL(true); // default setting is “true” for SSLServer
// ssl.useStrongCiphers();
// return ssl.getSSLContext();
SSLContext sslContext = SSLContext.getInstance(PROTOCOL);
KeyMaterial km = new KeyMaterial(SMPPSSLContextFactory.class
.getResourceAsStream(CERT_FILE),
SMPPSSLContextFactory.class
.getResourceAsStream(KEY_FILE),
password);
sslContext.init( (KeyManager[]) km.getKeyManagers(),
SMPPTrustManagerFactory.X509_MANAGERS, null);
// System.out.println(”getCipherSuites ==” +
// java.util.Arrays.toString(sslContext.getServerSessionContext().
// getSupportedSSLParameters().
// getCipherSuites()));
return sslContext;
}
private static SSLContext createSMPPClientSSLContext() throws
GeneralSecurityException, IOException {
{
SSLContext context = SSLContext.getInstance(PROTOCOL);
KeyMaterial km = new KeyMaterial(SMPPSSLContextFactory.class
.getResourceAsStream(CILENT_FILE),
SMPPSSLContextFactory.class
.getResourceAsStream(CILENT_KEY_FILE),
password);
context.init( (KeyManager[]) km.getKeyManagers(),
SMPPTrustManagerFactory.X509_MANAGERS, null);
return context;
}
}
}
```

 

实现证书检查认证的代码：

 

[view plain](http://618119.com/archives/2007/12/14/46.html)[copy to clipboard](http://618119.com/archives/2007/12/14/46.html)[print](http://618119.com/archives/2007/12/14/46.html)[?](http://618119.com/archives/2007/12/14/46.html)

01. package com.lizongbo.ssl;
02. import java.security.InvalidAlgorithmParameterException;
03. import java.security.KeyStore;
04. import java.security.KeyStoreException;
05. import java.security.cert.CertificateException;
06. import java.security.cert.X509Certificate;
07. import javax.net.ssl.ManagerFactoryParameters;
08. import javax.net.ssl.TrustManager;
09. import javax.net.ssl.TrustManagerFactorySpi;
10. import javax.net.ssl.X509TrustManager;
11. public class SMPPTrustManagerFactory
12. extends TrustManagerFactorySpi {
13. static final X509TrustManager X509 = new X509TrustManager() {
14. public void checkClientTrusted(X509Certificate\[] x509Certificates,
15. String s) throws CertificateException {
16. if (x509Certificates != null) {
17. for (X509Certificate elem : x509Certificates) {
18. elem.checkValidity();
19. //System.out.println(”checkClientTrusted elem ==” + elem);
20. }
21. }
22. // System.out.println(”checkClientTrusted s ==” + s);
23. }
24. public void checkServerTrusted(X509Certificate\[] x509Certificates,
25. String s) throws CertificateException {
26. if (x509Certificates != null) {
27. for (X509Certificate elem : x509Certificates) {
28. // System.out.println(”checkServerTrusted elem ==” + elem);
29. }
30. }
31. // System.out.println(”checkServerTrusted s ==” + s);
32. }
33. public X509Certificate\[] getAcceptedIssuers() {
34. return new X509Certificate\[0];
35. }
36. };
37. static final TrustManager\[] X509\_MANAGERS = new TrustManager\[] {
38. X509};
39. public SMPPTrustManagerFactory() {
40. }
41. protected TrustManager\[] engineGetTrustManagers() {
42. return X509\_MANAGERS;
43. }
44. protected void engineInit(KeyStore keystore) throws KeyStoreException {
45. // noop
46. }
47. protected void engineInit(
48. ManagerFactoryParameters managerFactoryParameters) throws
49. InvalidAlgorithmParameterException {
50. // noop
51. }
52. }