Solved

problem with jsse?????

Posted on 2002-03-25
4
296 Views
Last Modified: 2010-03-31
can somebody provide a sample code for establishing a HTTPURLconnection with a server using jsse.i need to capture the sesssion id's and cookies as well.As i am working with jsse for the first time i am not sure how the to initialise the values for trust manager and key manager.I don't want a thrid party  tool.i want the SUN jsse implementation...
0
Comment
Question by:anuj_roy
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
msterjev earned 125 total points
ID: 6894853
Establishing HttpsURLConnection is the same as HttpURLConnection (with proper cast).So, the problem isn't the code. You must specify some system properties.First at all you need to specify https protocol handler class. In the Sun's implementation you must start you java interpreter with the System property:

-Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol. As always,the server will present its certificate to the client, which must verify it using its trustore. In this case, we can use the default trustore $JREHOME/lib/security/cacerts.Also, you can set system property javax.net.ssl.trustStore to your own trustore. The same is valid for keystore. The system property is javax.net.ssl.keyStore. In order to establish connection this way, the server's name in the URL must be the same with the server name in it's certificate. If you want to avoid this constraint you must write implementation of interface HostnameVerifier. You can contact me for further examples of HttpsURLConnection and row SSL socket communication.
0
 

Author Comment

by:anuj_roy
ID: 6895641
thanx for the reply ,but i have a problem when i am initialising SSLContext ,i have to pass keystore and Truststore values dynamically .i can't put it in the system properties.SO how to do that from the code.that in the constructor of SSLContext when i am passing this values i am  at present initialising it with null and passing it.is it fine or just tell me how to initialise it properly using sun's JSSE.
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6895702
private boolean initializeSSL()
     {
          String sslProvider;
          String keyStore;
          String sslVer;
          String certType;
          String storeType;
          String storePass;
          char[] storePassChars=null;
          SSLContext sslcxt;
          KeyManagerFactory kmf;
          KeyStore ks;
         
          Properties sslconfig=null;
          try
          {
               sslconfig=new Properties();
               sslconfig.load(new FileInputStream("sslconfig.properties"));
          }
          catch(Exception e)
          {
               showException(e);
               System.exit(1);
          }
          try
          {
               sslProvider=sslconfig.getProperty("sslProvider");
               if(sslProvider==null) sslProvider="SunJSSE";
               keyStore=sslconfig.getProperty("keyStore");
               if(keyStore==null) keyStore="mystore";
               sslVer=sslconfig.getProperty("sslVer");
               if(sslVer==null) sslVer="SSL";
               certType=sslconfig.getProperty("certType");
               if(certType==null) certType="SunX509";
               storeType=sslconfig.getProperty("storeType");
               if(storeType==null) storeType="JKS";
               storePass=sslconfig.getProperty("storePass");
               if(storePass!=null)
               {
                    storePassChars=new char[storePass.length()];
                    storePass.getChars(0,storePass.length(),storePassChars,0);
               }
               sslcxt=SSLContext.getInstance(sslVer,sslProvider);
               kmf=KeyManagerFactory.getInstance(certType,sslProvider);
               ks=KeyStore.getInstance(storeType);
               ks.load(new FileInputStream(keyStore),storePassChars);
               kmf.init(ks,storePassChars);
               sslcxt.init(kmf.getKeyManagers(),null,new SecureRandom());
               sslServerSocketFactory=(SSLServerSocketFactory)sslcxt.getServerSocketFactory();
               System.out.println("SSL Server Socket Factory created.");                            
               return true;          
          }catch(Exception e)
          {
               e.printStackTrace();
               return false;
          }
     }
0
 

Author Comment

by:anuj_roy
ID: 6895845
thanx a lot for the reply i will try this out...i want one more help ,i want to get the session id using the http url connection.can u tell me how to go about it????
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 80
JDeveloper 12c for 32 bit 4 84
session migration servlets 2 36
servlet example 17 32
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question