Java calls the Axis WebService service that is transmitted using the SSL/HTTPS protocol

Source: Internet
Author: User
Tags dname

Use the SSL/HTTPS protocol to transfer
The Web service can also use SSL as the transport protocol. Although Jax-RPC does not enforce the use of the SSL protocol, it uses the HTTPS protocol under Tomcat.
1. Use the JDK's own tools to create key libraries and truststore.

1) Create the server-side key library by using the following command:

Keytool-genkey-alias Server-keystore Server.keystore-keyalg RSA
Enter KeyStore Password: Changeit
What is your first and last name?
[Unknown]: Server
What is the name of your organizational unit?
[Unknown]: EC
What is your organization's name?
[Unknown]: EC
What is the name of your city or region?
[Unknown]: Beijing
What is the name of your state or province?
[Unknown]: Beijing
What is the two-letter country code for this unit?
[Unknown]: CN
Cn=server, Ou=ec, O=ec, l=beijing, st=beijing, C=CN right?
[No]: Y

Enter the master password for <Server>
(If the KeyStore password is the same, press ENTER):
Once the above command is completed, a keystore named Server.keystore will be obtained.

2) Build the Trust library for the client. First output the RSA certificate:

Keytool-export-alias server-file test_axis.cer-storepass Changeit-keystore server.keystore
Then enter the RSA certificate into a new Truststore file. This truststore is used by clients and is used to authenticate server-side identities.

Keytool-import-file test_axis.cer-storepass Changeit-keystore Client.truststore-alias serverkey-noprompt
When the above command finishes executing, you will get a truststore named Client.truststore.

3) similarly generate the client's KeyStore Client.keystore and the server-side Truststore Server.truststore. For convenience, give. bat files
Gen-cer-store.bat content is as follows:

Set server_dn= "Cn=server, Ou=ec, O=ec, L=BEIJINGC, s=beijing, C=CN"
Set client_dn= "Cn=client, Ou=ec, O=ec, l=beijing, s=beijing, C=CN"
Set Ks_pass=-storepass Changeit
Set Keyinfo=-keyalg RSA

Keytool-genkey-alias server-dname%server_dn%%ks_pass%-keystore server.keystore%KEYINFO%-keypass Changeit
Keytool-export-alias server-file test_axis.cer%ks_pass%-keystore server.keystore
Keytool-import-file test_axis.cer%ks_pass%-keystore client.truststore-alias serverkey-noprompt

Keytool-genkey-alias client-dname%client_dn%%ks_pass%-keystore client.keystore%KEYINFO%-keypass Changeit
Keytool-export-alias client-file test_axis.cer%ks_pass%-keystore client.keystore
Keytool-import-file test_axis.cer%ks_pass%-keystore server.truststore-alias clientkey-noprompt

Okay, now we have four files: Server.keystore,server.truststore,client.keystore,client.truststore

2. Change the Tomcat configuration file (Server.xml) and add the following deployment descriptor: (In fact, there is, just commented out)

<connector port= "8440"
maxthreads= "minsparethreads=" maxsparethreads= "75"
Enablelookups= "false" disableuploadtimeout= "true"
Acceptcount= "Scheme=" "https" secure= "true"
Clientauth= "true" keystorefile= "F:\server.keystore" keystorepass= "Changeit"
Truststorefile= "F:\server.truststore" truststorepass= "Changeit"
Sslprotocol= "TLS"/>

3. Redeploy the HelloWorld once and modify the following deployment code in SERVER-CONFIG.WSDD. (restored only)

<service name= "HelloWorld" provider= "Java:rpc" >
<requestFlow>
</requestFlow>
<responseFlow>
</responseFlow>
<parameter name= "Allowedmethods" value= "SayHello"/>
<parameter name= "ClassName" value= "HelloWorld"/>
</service>

4, modify the client program Testclient.java (the modified part has been marked)


public class TestClient
{
public static void Main (String [] args) throws Exception {
String endpoint = "Https://localhost:" + "8440" + "/axis/helloworld";//Pay attention to the difference here! https!

System.setproperty ("Javax.net.ssl.keyStore", "E:\\client.keystore");
System.setproperty ("Javax.net.ssl.keyStorePassword", "password");
System.setproperty ("Javax.net.ssl.trustStore", "E:\\client.truststore");
System.setproperty ("Javax.net.ssl.trustStorePassword", "password");

Service service = new service ();
Call Call Service.createcall ();
Call.settargetendpointaddress (new Java.net.URL (endpoint));

Call.setoperationname ("SayHello");
string res = (string) call.invoke (new object[] {});

System.out.println (RES);
}
}

Java calls the Axis WebService service that is transmitted using the SSL/HTTPS protocol

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.