SSL one-way verification

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.