0

I have one Java Web Project (Demo 1) that is running on Apache Tomcat Version 8.0.33 which is installed on Ubuntu 14.04 Server.

Now I am creating a new Java Web Project (Demo 2) and I have to call a Servlet (doPost) with Request Parameter of my already running project (Demo 1) from my project (Demo 2).

And my Java Web Project (Demo 1) is running on https So the Servlet URL is https://shibbolethidp.demo.local/idp/j_security_check

I have also export cert using below command of server where Demo 1 App is running into my local jdk where I am trying to run my Demo 2 App.

C:\Program Files\Java\jdk1.8.0_60\bin\keytool.exe -import -alias shib-idp -file C:\Users\guest\Desktop\Desktop\shib_exportedCert.der -keystore "C:\Program Files\Java\jdk1.8.0_60\jre\lib\security\cacerts" -storepass changeit

My Demo 2 Project code is given below which I am using to call Servlet of Demo 1 Project

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

@SuppressWarnings("serial")
public class Authn extends HttpServlet {
    Logger log = Logger.getLogger(Authn.class.getName());

    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        try {
            URL url = new URL("https://shibbolethidp.demo.local/idp/j_security_check");
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
            out.write("xml=xmltest\r\n");
            out.flush();
            out.close();
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String response1;
            while ((response1 = in.readLine()) != null) {
                System.out.println(response1);
            }
            in.close();
        } catch (Exception e) {
        }
    }
}

But when I run this I am getting the below error

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching shibbolethidp.demo.local found

I am using jdk1.8.0_60 and Tomcat 8.

user3441151
  • 1,880
  • 6
  • 35
  • 79

1 Answers1

0

You can use a Transport Security (SSL) Workaround for your localhost development environment.

Try adding this:

public class Authn extends HttpServlet {
    Logger log = Logger.getLogger(Authn.class.getName());

    static {
        //for localhost testing only
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
        new javax.net.ssl.HostnameVerifier(){

            public boolean verify(String hostname,
                    javax.net.ssl.SSLSession sslSession) {
                if (hostname.equals("localhost")) {
                    return true;
                }
                return false;
            }
        });
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
      // your code here
    }
}
Bishan
  • 15,211
  • 52
  • 164
  • 258