ITeye博客

Spring,smppapi,apache mina, ssl快速实现安全的smpp(6)

lanmh · 2008-07-21 15:41 · MINA · Java · Apache · Spring · Security · Mina · Markdown

接上一篇: http://618119.com/archives/2007/12/13/45.html

使用 commons ssl生成 SSLContext :

 

view plaincopy to clipboardprint?

  1. package com.lizongbo.ssl;
  2. import javax.net.ssl.SSLContext;
  3. import java.security.GeneralSecurityException;
  4. import java.io.IOException;
  5. import javax.net.ssl.KeyManager;
  6. import org.apache.commons.ssl.KeyMaterial;
  7. public class SMPPSSLContextFactory {
  8. private static final String PROTOCOL = “TLS”;
  9. 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 plaincopy to clipboardprint?

  1. package com.lizongbo.ssl;
  2. import java.security.InvalidAlgorithmParameterException;
  3. import java.security.KeyStore;
  4. import java.security.KeyStoreException;
  5. import java.security.cert.CertificateException;
  6. import java.security.cert.X509Certificate;
  7. import javax.net.ssl.ManagerFactoryParameters;
  8. import javax.net.ssl.TrustManager;
  9. 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. }

评论(2)

  1. #2 楼 · lanmh · 2008-08-05 16:19

    我也是初学者,从别人那里转来的。

  2. #1 楼 · rxiaoliang · 2008-08-05 16:13

    能不能写一个用smppapi相关函数连接短信中心收发短信的多线程程序啊。 我大概看了一下你写的这6篇文章,好像是用smppapi做短信中心的代码,不是客户端的。

评论功能维护中,当前仅展示历史评论。