SSL

 /*
 * Created on Jul 30, 2009
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.citigroup.ci.common.util;

import java.io.FileInputStream;
import java.net.URL;
import java.security.KeyStore;
import java.security.Principal;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.security.cert.X509Certificate;
/**
 * @author sr94651
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class HttpsURLConnectionUtil implements HostnameVerifier {
 
    public boolean verify(String hostname, SSLSession session) {
      boolean verified = false;
      try{
       System.out.println(" HttpsURLConnectionUtil | verify | hostname="+hostname);
       X509Certificate[] xcert =  session.getPeerCertificateChain();
       String subjectDN = ((Principal)xcert[0].getSubjectDN()).toString();
       System.out.println(" HttpsURLConnectionUtil | verify | subjectDN="+subjectDN);
       int j = subjectDN.indexOf("CN");
       int k = subjectDN.indexOf(",");      
       String certHostName = subjectDN.substring(j+3,k);
       System.out.println(" HttpsURLConnectionUtil | verify | certHostName="+certHostName);
    if (hostname.equalsIgnoreCase(certHostName))
        verified = true;       
       System.out.println(" HttpsURLConnectionUtil | verify | verified="+verified);
      }catch (Exception e) {
    e.printStackTrace();
    System.err.println("HttpsURLConnectionUtil | verify | GeneralException:"+e.getMessage());
      }
      return verified;
    }
   
    public HttpsURLConnection getHttpsURLConnection(URL url) {  
   HttpsURLConnection httpsConn = null;
   try {
    KeyStore ts = KeyStore.getInstance("JKS");
    ts.load(new FileInputStream(CIConstants.PROPERTY_FILE_PATH+"TrustStore.jks"),CIConstants.DM_TRUSTSTORE_PWD.toCharArray());
    TrustManager[] tm;
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("IbmX509");
    tmf.init(ts);
    tm = tmf.getTrustManagers();
    
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, tm, null);
    SSLSocketFactory sslSocketFactory = sslSocketFactory = sslContext.getSocketFactory();
    httpsConn = (HttpsURLConnection)url.openConnection();
    httpsConn.setSSLSocketFactory(sslSocketFactory);   
   } catch (Exception e) {
     e.printStackTrace();
    System.err.println("HttpsURLConnectionUtil | getHttpsURLConnection | GeneralException:"+e.getMessage());
   }
   return httpsConn;
    }
}

 

 

*****************************************************************************

   if (endpointURL.substring(0,5).equalsIgnoreCase("https")) {
    URL url = new URL(reformBWEndPointUrl(endpointURL, "https"));
    HttpsURLConnectionUtil connUtil = new HttpsURLConnectionUtil();
    HttpsURLConnection conn = connUtil.getHttpsURLConnection(url);
    conn.setHostnameVerifier(connUtil);
    if (conn==null)
     throw new Exception("CIApplicationProxyServlet | processRequest | HttpsURLConnection is null");

    CILogger.printLog(CILogger.DEBUG,null, "CIApplicationProxyServlet | processRequest | HttpsURLConnection conn: "+conn,FRAMEWORK_LOGGER);
   conn.setRequestMethod("POST");
   conn.setAllowUserInteraction(false);
   conn.setDoOutput(true);


   for (Enumeration enu = request.getHeaderNames(); enu.hasMoreElements();) {
    String pName = (String) enu.nextElement();
    String pValu = request.getHeader(pName);
    if (!pName.toUpperCase().startsWith("HOST")) {
     conn.setRequestProperty(pName, pValu);


    } else {
     conn.setRequestProperty(pName, url.getHost() + ":"
       + url.getPort());
    }
   }

   CILogger.printLog(CILogger.DEBUG,null, "CIApplicationProxyServlet | processRequest entered BW Start Request:"+System.currentTimeMillis(),FRAMEWORK_LOGGER);

   OutputStream rawOutStream = conn.getOutputStream();
   PrintStream pw = new PrintStream(rawOutStream,false,"UTF-8");
   pw.print(inputRequest);
   pw.flush();
   pw.close();


   InputStream rawInStream;
   try {
       rawInStream = conn.getInputStream();
   } catch(IOException e) {
       CILogger.printLog(CILogger.ERROR,null,"CIApplicationProxyServlet | processRequest BW IO Exception, code:"
               + conn.getResponseCode() + " message:" + conn.getResponseMessage(),FRAMEWORK_LOGGER);
       rawInStream = conn.getErrorStream();
   }

   CILogger.printLog(CILogger.DEBUG,null, "CIApplicationProxyServlet | processRequest entered BW End:"+System.currentTimeMillis(),FRAMEWORK_LOGGER);

         Reader rdrInputUTF = new InputStreamReader(rawInStream, "UTF-8");

   BufferedReader rdr = new BufferedReader(rdrInputUTF);
   String line = "";
   String sbOutPut = "";
   while ((line = rdr.readLine()) != null) {
    sbOutPut += line;
   }
   rdr.close();

   response.setStatus(conn.getResponseCode());
   response.setCharacterEncoding("UTF-8");
   response.setContentType(conn.getContentType());

 
   PrintStream out = new PrintStream(response.getOutputStream(),false,"UTF-8");
   out.print(sbOutPut.toString());
   CILogger.printLog(CILogger.DEBUG,null, "CIApplicationProxyServlet | processRequest entered sbOutPut length:"+sbOutPut.length(),FRAMEWORK_LOGGER);
   out.flush();
   out.close();
   sbOutPut = null;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值