`

java 访问wcf

阅读更多

Invoke WCF service from Java Client with Authentication (X.509 Certificate) Java 客户端调用WCF服务 需要安全验证 收藏
最近很多朋友都在问我怎样从java客户端调用带安全验证的WCF服务。 当初我解决这个问题的时候查了很多文献和资料,虽说web service是号称跨平台的吧,可是真跨起来,有时也挺麻烦。现在我把它总结下来,希望能对大家有所帮助。

因为当时给公司(Paragallo Mobile AS)写解决方案的时候就是用的英文,所以还是英文的。同时我配了demo代码,相信大家可以看明白。如果有什么不明白的地方,可以给我留言。

Summary:
We use Message-Level Security authenticating with wsHttpBinding. And the user also needs a username/password. X.509 Certificate is used here.

This article talks about how to call WCF service from the java client with authentication. The server side use X.509 certificate to encrypt the Username and Password. The java client tool is Netbeans and Eclipse with SoapUI plug-in installed.

 

Contents

Invoke WCF service from Java Client with Authentication (X.509 Certificate) 1

Summary: 1

1.Prepare the Certificate: 1

1.1. Create a Certificate to Act as Your Root Certificate Authority. 1

1.2. Install Your Root Certificate Authority Certificate on the Server and Client Machines 2

1.3. Create and Install Your Temporary Service Certificate. 2

2.Create a Demo WCF Service. 2

2.1. New a demo wcf service. 2

2.2. Create Customer Validator 2

2.3. the Server config file. 3

3. Call the Service From Java Client 4

3.1 Create the Java client, the container is Glassfish. 4

3.2 Create the Java Application client, the container is not Glassfish. 9

3.3 Development the Java client code in Eclipse. 11

4. Download Demos 12

Reference. 12

 

1.Prepare the Certificate:
We need to create a self-signed root certificate authority (CA) that will be placed in the Trusted Root Certification Authority store (受信任的根证书颁发机构). The certificate used by WCF is then created from the root self-signed certificate and installed in the LocalMachine store.

1.1. Create a Certificate to Act as Your Root Certificate Authority
Command:  makecert -n "CN=RootCATest" -r -sv RootCATest.pvk RootCATest.cer

 

In this command:

-n – Specifies the subject name for the root CA. The convention is to prefix the subject name with "CN = " for "Common Name".

-r – Specifies that the certificate will be self-signed.

-sv – Specifies the file that contains the private key of the certificate.

RootCATest.cer – Specifies the name of the file containing the public key of the certificate.

 

In the Create Private Key Password dialog box, enter a password, confirm the password, and then click OK. Optionally, you can click None without entering the password, but this is not recommended for security reasons.

In the Enter Private Key Password dialog box, enter the password again and then click OK.

This is the password needed to access the private key file RootCATest.pvk in order to generate the file RootCATest.cer containing the public key.

1.2. Install Your Root Certificate Authority Certificate on the Server and Client Machines
In this step, you will install the certificate in the Trusted Root Certification Authorities (受信任的根证书颁发机构) location on both the server and client machines. All certificates that are signed with this certificate will be trusted by the client machine.

You can do this by run mmc command, then do it in Microsoft Management Console. Import the Certificate to Trusted Root Certification Authorities (受信任的根证书颁发机构).

1.3. Create and Install Your Temporary Service Certificate
Command: makecert -sk MyKeyName -iv RootCATest.pvk -n "CN=tempCert" -ic RootCATest.cer -sr localmachine -ss my -sky exchange -pe tempCert.cer

 

In the Enter Private Key Password dialog box, enter the password for the root CA privatekeyfile specified in STEP 1, and then click OK.

 

2.Create a Demo WCF Service
2.1. New a demo wcf service
2.2. Create Customer Validator
Class MyCustomValidator by inheriting UserNamePasswordValidator, but this should be created in a new class library project.


2.3. the Server config file
<system.serviceModel>

        <bindings>

            <wsHttpBinding>

                <binding name="mySecureBinding">

                    <security mode="Message">

                        <message clientCredentialType="UserName"/>

                    </security>

                </binding>

            </wsHttpBinding>

        </bindings>

        <services>

            <service behaviorConfiguration="ServerWcfService.Services.MySimpleServiceBehavior" name="ServerWcfService.Services.MySimpleService">

                <endpoint address="" binding="wsHttpBinding" contract="ServerWcfService.ServiceContracts.IMySimpleService" bindingConfiguration="mySecureBinding">

                    <identity>

                        <dns value="MyServerCert"/>

                    </identity>

                </endpoint>

                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

            </service>

        </services>

        <behaviors>

            <serviceBehaviors>

                <behavior name="ServerWcfService.Services.MySimpleServiceBehavior">

                    <serviceMetadata httpGetEnabled="true"/>

                    <serviceDebug includeExceptionDetailInFaults="false"/>

                    <serviceCredentials>

                        <serviceCertificate findValue="MyServerCert" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My"/>

                        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="ServerWcfService.CustomValidators.MyCustomValidator,ServerWcfService"/>

                    </serviceCredentials>

                </behavior>

            </serviceBehaviors>

        </behaviors>

    </system.serviceModel>

 

The config file set the clientCredentialType in the <bindings> section and set the bindingConfiguration in <endpoint> section. We set serviceCredentials to do an authentication by Certificate and username authentication.

 

3. Call the Service From Java Client
You can use Web Services Interoperability Technologies (WSIT) do access the WCF Service from java client. And this was integrated in NetBeans V6.1.

 

3.1 Create the Java client, the container is Glassfish
Here the IDE is Netbeans, my version is 6.1.

1 New a Web project

2 add a new web service client.

3 import the server cert to the store “TrustStore”

This the command, you must install JDK if you want use it.

   keytool -import -file MyServer.cer  -keystore TrustStore -alias serverkey

the import file  MyServer.cer is the server's X.509 certificate, serverkey is the alias of the certificate. And you will need to input a keystore password after you execute this command. Please remember your password. The keystore file will in the current forder where you do this command.

   keytool -list -v -keystore TrustStore

using this command to see if the importing is successful.

Note: if you don't do this, it will throw an exception “WSS1511: An Error occurred while locating PEER Entity certificate in TrustStore.”.

4 Edit Web Service attributes

You can Edit it by right click the webservice

 

then you can edit the security attribute, please click the TrustStore button:

 

You can choose your truststore which you import the server Cert at step 3 by browse.please change the File of Type to all files.afte this input the password of the keystore. And the Cert's alias.

 

After this you will get two config file in folder “Source Packages” of Netbeans. The two files are MySimpleService.svc.xml and wsit-client.xml. Please edit the  MySimpleService.svc.xml.
Example:
<wsp:Policy wsu:Id="WSHttpBinding_IMySimpleServicePolicy">

        <wsp:ExactlyOne>

            <wsp:All>

                <sc:TrustStore wspp:visibility="private" peeralias="mykey" storepass="1302821986" type="JKS" location="C:\Documents and Settings\Marvion\TrustStore"/>

            </wsp:All>

        </wsp:ExactlyOne>

    </wsp:Policy>

Note: the type is “JKS”.
 
5. New a Test Servlet.

Add the following code to the servlet.
 


 

Note: the bold code is to put the username and password. Please write this before you call a method.

 

I test successfully.

 

 

 

3.2 Create the Java Application client, the container is not Glassfish
It's very same like above configuration. But if you are running on some other container then the only way for you is to supply your own CallbackHandler :

The config file:

<wsp:Policy wsu:Id="WSHttpBinding_IMySimpleServicePolicy">

        <wsp:ExactlyOne>

            <wsp:All>

                <sc:CallbackHandlerConfiguration xmlns:sc="http://schemas.sun.com/2006/03/wss/client" >

                    <sc:CallbackHandler  name="TrustStoreCallbackHandler"  classname="javaapplication1.TrustStoreCallbackHandler" />

                </sc:CallbackHandlerConfiguration>

            </wsp:All>

        </wsp:ExactlyOne>

    </wsp:Policy>

The class-name is the full name of your own callbackhandler class.

 

Example:

 


 

Note: you need download metro-1_3.jar from https://metro.dev.java.net/1.3/ . After install that add the webservices-rt.jar to the project library reference.

 

3.3 Development the Java client code in Eclipse.
Well, there is no eclipse plug-in for can do it directly. So what I do is develop is in Netbeans first, then I move the code and configuration files to Eclipse. It needs to change some source folder directory here. see my sample code.

 

4. Download Demos
4.1 The Service side code.  http://download.csdn.net/source/1135380

4.2 Web application code http://download.csdn.net/source/1135375

4.3 Java application code  http://download.csdn.net/source/1135382

4.4 Eclipse client code    http://download.csdn.net/source/1135383

4.5 The needed jar file (webservices-rt.jar)  it will generate by netbeans when you add new webservice client.

Reference
01       http://msdn2.microsoft.com/en-us/library/ms733813.aspx

02       http://forums.java.net/jive/thread.jspa?messageID=293406

03       http://forums.java.net/jive/message.jspa?messageID=277883

04       http://forums.java.net/jive/thread.jspa?messageID=262600&#262600

05       https://xwss.dev.java.net/articles/security_config.html

06       https://wsit.dev.java.net/issues/show_bug.cgi?id=844

 


本文来自CSDN博客,转载请标明出处:file:///C:/Users/Administrator/Desktop/abc.htm

分享到:
评论
3 楼 yjsxxgm 2011-11-23  
yjsxxgm 写道
FFFFFFFFFFFFFFF

WWW
2 楼 yjsxxgm 2011-11-23  
FFFFFFFFFFFFFFF
1 楼 CoolJava86 2010-08-11  
这个 请教 是不是访问WCF的java 一定要使用NetBeans开发

相关推荐

    java 访问 wcf 示例(使用的HttpClient)

    Java访问WCF服务是跨平台通信的一个典型场景,其中WCF(Windows Communication Foundation)是微软提供的一个用于构建、部署和管理服务的框架。本示例将重点讲解如何使用Java的HttpClient库来调用WCF服务。 一、WCF...

    java与wcf通信

    3. **Java访问WCF服务**:Java通过使用SOAP客户端库(如Apache CXF、Axis2)可以调用WCF服务。这涉及到理解WSDL(Web Service Description Language),它定义了服务接口和消息格式。 4. **C#客户端代码**:这部分...

    java client客户短调用WCF服务代码1

    本示例中的“java client客户短调用WCF服务代码1”是使用NetBeans开发的一个Web应用程序,展示了如何在Java环境中实现对WCF服务的调用。 首先,我们需要理解WCF服务。WCF是微软推出的一种全面的服务导向架构,用于...

    java 调用wcf

    Java 调用 WCF(Windows Communication Foundation)是一项技术,允许 Java 应用程序与 .NET Framework 开发的 WCF 服务进行通信。WCF 是 Microsoft 提供的一种面向服务的架构,用于构建高度可互操作的分布式应用...

    Android访问WCF服务源码(Android 客户端)

    本篇将深入探讨如何在Android客户端中访问WCF服务,并以源码为例进行解析。 1. **Android与WCF通信基础** Android应用程序通过HTTP或HTTPS协议与WCF服务进行通信。通常使用JSON作为数据交换格式,因为其轻量级且...

    java调用.net wcf服务。

    ### Java调用.NET WCF服务详解 #### 一、前言 随着技术的发展与融合,跨平台服务调用成为越来越常见的需求。其中,Java应用程序如何有效地调用.NET平台下的WCF(Windows Communication Foundation)服务就是一个...

    java与wcf通讯

    本文档详细介绍了如何配置Java环境来访问WCF服务,特别是使用Apache Axis2作为Java客户端调用WCF服务的代理。 首先,进行WCF与Java通讯的环境搭建工作非常重要。为了实现这一目标,我们需要下载Apache Axis2的1.4.1...

    WCFService可以通过web调用和WCF服务访问

    WCF服务可以被多种客户端访问,包括基于Web的和传统的WCF客户端。在标题和描述中提到的“WCFService可以通过web调用和WCF服务访问”,这意味着该服务已经配置为允许两种不同的调用方式。 1. **Web调用**:WCF服务...

    java使用 httpclient 访问wcf 用到的 jar包(包括json jar)

    这些jar包组合起来,构成了一个完整的HTTP请求处理和JSON数据解析的环境,使得Java开发者能够有效地调用WCF服务并处理返回的数据。在实际使用时,需要通过HttpClient创建HTTP请求,配置必要的参数,如URL、方法、头...

    普通java访问c# webService

    在C#中,可以使用.NET框架中的ASMX或WCF服务来创建WebService;而在Java中,我们通常使用JAX-WS或JAX-RS标准来消费这些服务。 在Java中访问C#的WebService,你需要遵循以下步骤: 1. 获取WSDL:首先,你需要获取C#...

    WCF服务编程.doc

    【WCF服务编程】 Windows Communication Foundation (WCF) 是微软推出的一种用于构建分布式应用程序的框架,它整合了.NET Framework中的多种通信技术,如ASP.NET服务、Web服务增强(WSE)、.NET Remoting、消息传输...

    Android Studio调用RestfulWCF接口

    在 Android Studio 中调用 Restful WCF 接口需要使用 Java 的标准类 HttpURLConnection,该类继承自 URLConnection,提供了访问 HTTP 协议的基本功能,能够向指定网站发送 GET 请求和 POST 请求。但是,在 Android ...

    axis2调用WCF

    - 生成服务代理:使用WSDL(Web服务描述语言)和Mex( Metadata Exchange)端点,Apache Axis2可以生成Java客户端代码,这些代码包含了调用WCF服务所需的所有信息。 - 配置Axis2:将生成的Java客户端代码导入到...

    WCF 通讯例子代码

    - **服务宿主**: WCF服务需要一个宿主环境才能运行,常见的宿主有IIS、自承载(如Console应用或Windows服务)等。 - **服务配置**: 使用`.svc`文件或配置文件(如`web.config`或`app.config`)来配置服务的终结点、...

    android客户端调用wcf服务实例

    1. **跨域访问**:由于Android客户端可能与WCF服务不在同一个域,需要确保WCF服务允许跨域请求(CORS)。 2. **错误处理**:处理可能出现的网络错误,如超时、连接失败等,并提供适当的反馈。 3. **安全考虑**:在...

    Android连接IIS部署的WCF服务

    1. **配置WCF服务**:在Visual Studio等开发环境中,创建一个WCF服务项目,定义服务合同、实现服务行为,并选择合适的绑定类型。例如,使用基本HttpBinding或WSHttpBinding,因为它们对跨平台兼容性友好。接着,在...

    android 调用C# WCF开发实例

    在"CallWcf"这个示例中,可能包含了具体的代码实现,包括Android端调用WCF服务的Java代码,以及可能的C# WCF服务端代码。通过学习和理解这些代码,你可以掌握Android调用C# WCF服务的完整流程。同时,要注意跨平台...

    wcf android

    1. **WCF服务基础**:首先需要理解WCF服务的基本概念,包括服务宿主、服务契约、绑定和行为。服务宿主是运行服务的环境,服务契约定义了服务提供的操作,绑定确定服务与客户端之间的通信方式,行为则涉及服务的安全...

    Android WCF调用源码

    本项目"Android WCF调用源码"专注于演示如何在Android应用中调用WCF服务,特别是实现RESTful风格的服务调用。 RESTful服务是一种基于HTTP协议,通过URI来定位资源,使用标准HTTP方法(GET、POST、PUT、DELETE等)...

Global site tag (gtag.js) - Google Analytics