SSL one-way authentication to intercept the network channel layer data is intercepted, so when the client is called to click on the trust, the program calls the same
1. Generate Certificate
Keytool-genkey-v-alias jifubao-keyalg rsa-keystore D:\jifubao.keystore-validity 36500
2. Configure Tomcat (preferably in tomcat below conf)
<connector port= "8443" protocol= "http/1.1" sslenabled= "true"
maxthreads= "Scheme=" "https" secure= "true"
Keystorefile= "Conf/jifubao.keystore" keystorepass= "123456"
Clientauth= "false" sslprotocol= "TLS"/>
3. Browser access
Direct access to HTTPS
4. Program Access:
Package Com.elephant.car.common;
Import java.io.*;
Import java.net.*;
Import java.security.cert.CertificateException;
Import Java.security.cert.X509Certificate;
Import javax.net.ssl.*;
/**
* HTTPS call Test
* SSL Single use
* @author Xuanxy
*
*/
public class Trustssl {
private static class Trustanytrustmanager implements X509trustmanager {
public void checkclienttrusted (x509certificate[] chain, String authtype)
Throws Certificateexception {
}
public void checkservertrusted (x509certificate[] chain, String authtype)
Throws Certificateexception {
}
Public x509certificate[] Getacceptedissuers () {
return new x509certificate[] {};
}
}
private static class Trustanyhostnameverifier implements Hostnameverifier {
public boolean verify (String hostname, sslsession session) {
return true;
}
}
public static void Main (string[] args) throws Exception {
InputStream in = null;
OutputStream out = null;
byte[] buffer = new byte[4096];
String Str_return = "";
try {
Sslcontext sc = sslcontext.getinstance ("SSL");
Sc.init (NULL, new trustmanager[] {new Trustanytrustmanager ()},
New Java.security.SecureRandom ());
URL console = new URL (
"Https://192.168.1.154:8443/jifubao/user/login.json?phone_num=13212324322&password=123456&device_id= 4444444&os_ver=ios22&os_name=iphone2&os_type=ios ");
Httpsurlconnection conn = (httpsurlconnection) Console
. OpenConnection ();
Conn.setsslsocketfactory (Sc.getsocketfactory ());
Conn.sethostnameverifier (New Trustanyhostnameverifier ());
Conn.connect ();
InputStream is = Conn.getinputstream ();
DataInputStream indata = new DataInputStream (IS);
String ret = "";
while (ret! = null) {
ret = Indata.readline ();
if (ret! = null &&!ret.trim (). Equals ("")) {
Str_return = Str_return
+ New String (Ret.getbytes ("iso-8859-1"), "GBK");
}
}
Conn.disconnect ();
} catch (Connectexception e) {
System.out.println ("Connectexception");
System.out.println (e);
Throw e;
} catch (IOException e) {
System.out.println ("IOException");
System.out.println (e);
Throw e;
} finally {
try {
In.close ();
} catch (Exception e) {
}
try {
Out.close ();
} catch (Exception e) {
}
}
System.out.println (Str_return);
}
}
SSL one-way verification