Solved

problem with jsse?????

Posted on 2002-03-25
4
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

734 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