Apache CXF Combat nine release Web service using SSL

Source: Internet
Author: User

This article link: http://blog.csdn.net/kongxx/article/details/7738717

Apache CXF One of the actual combat Hello World Web Service

Apache CXF Combat II Integrated sping and Web container

Apache CXF Three-combat Transfer Java objects

Apache CXF Real-combat four build restful Web Service

Apache CXF Combat Five compressed Web service data

Apache CXF Combat Six Create a secure Web Service

Apache CXF Combat Seven use the Web service to transfer files

Apache CXF Combat eight map type binding

When using Web service, in many cases we will be asked to publish SSL Web service, if the Web service is deployed as a war package in a Web container such as Tomcat, We can easily deploy an SSL-enabled Web service by modifying the Tomcat configuration, and when it comes to running stand-alone programs book, it takes some action to publish the Web service at this time, and see how to publish and invoke SSL Web service in CXF.

1. The first is a Pojo entity class

Package Com.googlecode.garbagecan.cxfstudy.ssl;

public class User {
	private String ID;
	private String name;
	private String password;
	Public String GetId () {return
		ID;
	}
	public void SetId (String id) {
		this.id = ID;
	}
	Public String GetName () {return
		name;
	}
	public void SetName (String name) {
		this.name = name;
	}
	Public String GetPassword () {return
		password;
	}
	public void SetPassword (String password) {
		this.password = password;
	}
}
2. The following is the interface and implementation class for Web service, which is no different from the two classes described in the previous article

Package Com.googlecode.garbagecan.cxfstudy.ssl;

Import java.util.List;

Import Javax.jws.WebMethod;
Import Javax.jws.WebResult;
Import Javax.jws.WebService;

@WebService Public
interface UserService {
	@WebMethod
	@WebResult list<user> List ();

}

Package Com.googlecode.garbagecan.cxfstudy.ssl;

Import java.util.ArrayList;
Import java.util.List;

public class Userserviceimpl implements UserService {public

	list<user> List () {
		list<user> users = new arraylist<user> ();
		for (int i = 0; i < i++) {
			User user = new user ();
			User.setid ("" + i);
			User.setname ("User_" + i);
			User.setpassword ("Password_" + i);
			Users.add (user);
		}
		return users;
	}


3. Look at the server-side code below
Package Com.googlecode.garbagecan.cxfstudy.ssl;
Import Java.io.File;
Import Java.io.FileInputStream;

Import Java.security.KeyStore;
Import Javax.net.ssl.KeyManager;
Import Javax.net.ssl.KeyManagerFactory;
Import Javax.net.ssl.TrustManager;

Import Javax.net.ssl.TrustManagerFactory;
Import Org.apache.cxf.configuration.jsse.TLSServerParameters;
Import org.apache.cxf.configuration.security.ClientAuthentication;
Import Org.apache.cxf.configuration.security.FiltersType;
Import Org.apache.cxf.endpoint.Server;
Import Org.apache.cxf.jaxws.JaxWsServerFactoryBean; Import Org.apache.cxf.transport.http_jetty.

Jettyhttpserverenginefactory;
	
	public class MyServer {private static final int port = 12345;

	Private static final String address = "https://0.0.0.0:" +port+ "/ws/ssl/userservice";
		
		public static void Main (string[] args) throws Exception {System.out.println ("starting Server");
		
		Configuresslontheserver ();
		Jaxwsserverfactorybean Factorybean = new Jaxwsserverfactorybean (); FacTorybean.setserviceclass (Userserviceimpl.class);
		
		Factorybean.setaddress (address);
		Server server = Factorybean.create ();

		String endpoint = Server.getendpoint (). Getendpointinfo (). getaddress ();
	System.out.println ("Server started at" + endpoint); The public static void Configuresslontheserver () {File File = new file (MyServer.class.getResource) ("/com/googlecode/garb
		
		Agecan/cxfstudy/ssl/test.jks "). GetFile ());
			try {tlsserverparameters tlsparams = new Tlsserverparameters ();
			KeyStore KeyStore = keystore.getinstance ("JKS");
			String password = "MyPassword";
			
			String Storepassword = "MyPassword";
			Keystore.load (new FileInputStream (file), Storepassword.tochararray ());
			Keymanagerfactory keyfactory = keymanagerfactory.getinstance (Keymanagerfactory.getdefaultalgorithm ());
			Keyfactory.init (KeyStore, Password.tochararray ());
			keymanager[] keymanagers = Keyfactory.getkeymanagers ();

			Tlsparams.setkeymanagers (keymanagers); Keystore.load (New FileInputStream (file), Storepassword.tochararray ());
			Trustmanagerfactory trustfactory = trustmanagerfactory.getinstance (Trustmanagerfactory.getdefaultalgorithm ());
			Trustfactory.init (KeyStore);
			trustmanager[] trustmanagers = Trustfactory.gettrustmanagers ();
			
			Tlsparams.settrustmanagers (trustmanagers);
			Filterstype filterstypes = new Filterstype ();
			Filterstypes.getinclude (). Add (". *_export_.*");
			Filterstypes.getinclude (). Add (". *_export1024_.*");
			Filterstypes.getinclude (). Add (". *_with_des_.*");
			Filterstypes.getinclude (). Add (". *_with_null_.*");
			Filterstypes.getexclude (). Add (". *_dh_anon_.*");
			
			Tlsparams.setciphersuitesfilter (filterstypes);
			Clientauthentication CA = new Clientauthentication ();
			Ca.setrequired (TRUE);
			Ca.setwant (TRUE);
			
			Tlsparams.setclientauthentication (CA);
			Jettyhttpserverenginefactory factory = new Jettyhttpserverenginefactory ();
		Factory.settlsserverparametersforport (port, Tlsparams);
		catch (Exception e) {e.printstacktrace ();}
	}

}
 
4. Look at the client side code below

Package Com.googlecode.garbagecan.cxfstudy.ssl;
Import Java.io.File;
Import Java.io.FileInputStream;

Import Java.security.KeyStore;
Import Javax.net.ssl.KeyManager;
Import Javax.net.ssl.KeyManagerFactory;
Import Javax.net.ssl.TrustManager;

Import Javax.net.ssl.TrustManagerFactory;
Import Org.apache.cxf.configuration.jsse.TLSClientParameters;
Import Org.apache.cxf.configuration.security.FiltersType;
Import org.apache.cxf.endpoint.Client;
Import Org.apache.cxf.frontend.ClientProxy;
Import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

Import Org.apache.cxf.transport.http.HTTPConduit;

	public class MyClient {private static final String address = "Https://localhost:12345/ws/ssl/userService"; public static void Main (string[] args) throws Exception {Jaxwsproxyfactorybean Factorybean = new Jaxwsproxyfactorybean (
		);
		Factorybean.setaddress (address);
		Factorybean.setserviceclass (Userservice.class);
		Object obj = Factorybean.create ();
		
	UserService UserService = (userservice) obj;	Configuresslontheclient (UserService);
	System.out.println (Userservice.list ()); The private static void Configuresslontheclient (Object obj) {File File = new file (MyServer.class.getResource ("/com/goog
		
		Lecode/garbagecan/cxfstudy/ssl/test.jks "). GetFile ());
		Client client = clientproxy.getclient (obj);

		Httpconduit httpconduit = (httpconduit) client.getconduit ();
			try {tlsclientparameters tlsparams = new Tlsclientparameters ();

			Tlsparams.setdisablecncheck (TRUE);
			KeyStore KeyStore = keystore.getinstance ("JKS");
			String password = "MyPassword";
			
			String Storepassword = "MyPassword";
			Keystore.load (new FileInputStream (file), Storepassword.tochararray ());
			Trustmanagerfactory trustfactory = trustmanagerfactory.getinstance (Trustmanagerfactory.getdefaultalgorithm ());
			Trustfactory.init (KeyStore);
			trustmanager[] trustmanagers = Trustfactory.gettrustmanagers ();

			Tlsparams.settrustmanagers (trustmanagers); Keystore.load (new FileInputStream (file), STOREPASSWOrd.tochararray ());
			Keymanagerfactory keyfactory = keymanagerfactory.getinstance (Keymanagerfactory.getdefaultalgorithm ());
			Keyfactory.init (KeyStore, Password.tochararray ());
			keymanager[] keymanagers = Keyfactory.getkeymanagers ();
			
			Tlsparams.setkeymanagers (keymanagers);
			Filterstype filterstypes = new Filterstype ();
			Filterstypes.getinclude (). Add (". *_export_.*");
			Filterstypes.getinclude (). Add (". *_export1024_.*");
			Filterstypes.getinclude (). Add (". *_with_des_.*");
			Filterstypes.getinclude (). Add (". *_with_null_.*");
			Filterstypes.getexclude (). Add (". *_dh_anon_.*");

			Tlsparams.setciphersuitesfilter (filterstypes);
		Httpconduit.settlsclientparameters (Tlsparams);
		catch (Exception e) {e.printstacktrace ();
 }
	}
}
5. We need to manually generate the JKs file and place it in the/com/googlecode/garbagecan/cxfstudy/ssl/directory of MAVEN Engineering resources, which is the command to use when generating manually

Keytool-genkey-alias test-keyalg rsa-keypass mypassword-storepass mypassword-dname "CN=, OU=, O=, L=, ST=, C="-vali Dity 3650-keystore Test.jks
6. Finally, we can verify our tests by starting MyServer and myclient.





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.