Tuesday 16 April 2013

PKIX path building failed,Here is the solution

You have to created a trustServerCertificate() which basically trust the certificate of the requested server at runtime.
For that you should do the following step

1)Create a keystore and import the certificate into this keystore file (say test.keystore)
2)put it into your project class path
3)Write a method trustServerCertificate() which trust the keystore file

keystore.properties
KeyStoreLocation=/test.keystore


TrustClass.java

class TrustClass{
static Properties properties = new Properties();
void trustServerCertificate(){
try {
               
                properties.load(TrustClass.class.getClassLoader().getResourceAsStream("keystore.properties"));
                System.out.println("Properties loaded successfully");
            } catch (IOException e) {
                properties = null;
                System.out.println("Properties not loaded: " + e.getMessage());
                e.printStackTrace();
            }
            String keyStore = Util.class.getClassLoader().getResource(TrustClass.getProperties("KeyStoreLocation")).getFile();
            System.out.println("Keystore  path :" + keyStore);
            System.setProperty("javax.net.ssl.trustStore", keyStore);
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

}
 public static String getProperties(String key) {
            return properties.getProperty(key);
        }
public static void main(String arg[]){
TrustClass obj=new TrustClass();
obj. trustServerCertificate
}

}

No comments:

Post a Comment