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

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

使用 commons ssl生成 SSLContext :

 

  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;
}
}
}

 

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

 

  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. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值