Java client calls HTTPS WebService

Source: Internet
Author: User
Tags dname
Java must use the client trust book and key provided by the remote server to call the remote HTTPS WebService. set client. keystore and client. copy truststore to the classes \ test directory. package test; import Org. apache. axis. client. call; import Org. apache. axis. client. service; public class testecvotenotice {public static void main (string [] ARGs) throws exception {system. setproperty ("javax.net. SSL. keystore "," test \ Client. keystore "); system. setproperty ("javax.net. SSL. keystorepassword "," ABC "); system. setproperty ("javax.net. SSL. truststore "," test \ Client. truststore "); system. setproperty ("javax.net. SSL. truststorepassword "," ABC "); // system. setproperty ("javax.net. debug "," SSL "); // system. setproperty ("HTTPS. protocols "," tlsv1 "); // system. setproperty ("Java. protocol. handler. pkgs "," javax.net. SSL "); string endpoint =" https: // localhost: "+" 8443 "+"/axis/services/ecvotenotice "; // string endpoint =" http: // localhost: "+" 8080 "+"/axis/services/ecvotenotice "; Service = new service (); call = (CALL) service. createcall (); call. settargetendpointaddress (New java.net. URL (endpoint); call. setoperationname ("tostringp"); string res = (string) Call. invoke (new object [] {"box"}); call. setoperationname ("tostring"); string RES2 = (string) Call. invoke (new object [] {}); system. out. println (RES + "/" + RES2) ;}} run in the classes directory. java-CP % axisclasspath % test. testecvotenotice

WebService is transmitted Based on the SOAP protocol. Soap is transmitted in plain text as an XML file, which is insecure. Therefore, we can add SSL/HTTPS protocol to WebService for data transmission.

The Axis-based WebService can be well implemented. Here we use the Tomcat server
Use the built-in JDK tool to create the key library and trust library.

1) use the following command to create the server-side key database:
Keytool-genkey-alias server-keystore server. keystore-keyalg RSA
Enter the keystore password: strongit
What is your first name and last name?
[UNKNOWN]: Server
What is the name of your organization?
[UNKNOWN]: EC
What is your organization name?
[UNKNOWN]: EC
What is the name of your city or region?
[UNKNOWN]: Nanchang
What is the name of your state or province?
[UNKNOWN]: Jiangxi
What is the two-letter country code for this unit?
[UNKNOWN]: CN
CN = server, ou = EC, O = EC, L = Beijing, St = Beijing, c = Cn is it correct?
[No]: Y

Enter the master password of <Server>
(If the password is the same as the keystore password, press Enter ):
After the preceding command is executed, a key library named server. keystore is obtained.

2) generate the trust database of the client. First, output the RSA certificate:
Keytool-export-alias server-file test_axis.cer-storepass strongit-keystore server. keystore

Enter the RSA certificate to a new trusted library file. This trust library is used by the client to verify the identity of the server.
Keytool-import-file test_axis.cer-storepass changeit-keystore client. truststore-alias serverkey-noprompt

After the preceding command is executed, a trusted database named client. truststore is obtained.

3) generate the client's key library client. keystore and the server's trust database server. truststore. The. BAT file is provided for convenience.
The gen-cer-store.bat content is as follows:

1 set SERVER_DN="CN=Server, OU=ec, O=ec, L=nanchang, S=jiangxi, C=CN"
2     set CLIENT_DN="CN=Client, OU=ec, O=ec, L=nanchang, S=jiangxi, C=CN"
3     set KS_PASS=-storepass strongit
4     set KEYINFO=-keyalg RSA
1 keytool -genkey -alias Server -dname %SERVER_DN% %KS_PASS% -keystore server.keystore %KEYINFO% -keypass strongit <br>     keytool -export -alias Server -file test_axis.cer %KS_PASS% -keystore server.keystore <br>     keytool -import -file test_axis.cer %KS_PASS% -keystore client.truststore -alias serverkey -noprompt
<br><br>     keytool -genkey -alias Client -dname %CLIENT_DN% %KS_PASS% -keystore client.keystore %KEYINFO% -keypass strongit <br>     keytool -export -alias Client -file test_axis.cer %KS_PASS% -keystore client.keystore <br>     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

Modify the tomcat configuration file (server. XML) and add the following deployment descriptor: (in fact, it is only commented out)

1 <Connector
port="8440" 
2                maxThreads="150"
minSpareThreads="25"
maxSpareThreads="75"
3                enableLookups="false"
disableUploadTimeout="true"
4                acceptCount="100"
scheme="https"
secure="true"
5                clientAuth="true"
keystoreFile="f:\server.keystore"
keystorePass="changeit"
6                  truststoreFile="f:\server.truststore"
truststorePass="changeit"
7                sslProtocol="TLS"
/>

This section describes how to use the built-in JDK tool to create a key library and a trust library.

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.