Xfire WebService WS-Security

Source: Internet
Author: User

Xfire is an open-source framework provided by codehaus. It builds a bridge between pojo and SOA. Its main feature is to support publishing pojo into a web service in a very simple way, this processing method not only gives full play to the role of pojo, simplifies the steps and processes for converting Java applications into web services, but also directly reduces the difficulty of SOA implementation, friends who have used xfire will surely feel this way. There have been many articles about the basic configuration and calling method of xfire. Here I will explain in detail the implementation of xfire permission management.

Xfire uses wss4j as the Implementation of Web Services Security (WS-Security). wss4j is a Java jar package, it can be used to mark the access password and verify the password when calling the service. This package's for http://www.apache.org/dyn/closer.cgi/ws/wss4j,

Before using WS-Security, we need to install a JDK encryption package (unlimited strength jurisdiction policy files), which is the http://java.sun.com/j2se/1.5.0/download.jsp plugin. After decompression, there are two jar files, local_policy.jar and us_export_policy.jar, put the two files in the java_home/JRE/lib/security directory to overwrite the original files. You also need to install a bcprov-jdk15-141.jar for the http://BouncyCastle.org, download and put it under the java_home/JRE/lib/EXT directory. Both packages are required.

Now, the preparations are as follows. Let's set access permissions for the service!

1. First write a class called passwordhandler. The Code is as follows:

Package org. codehaus. xfire. Demo;

Import java. Io. ioexception;
Import java. util. hashmap;
Import java. util. Map;

Import javax. Security. Auth. Callback. Callback;
Import javax. Security. Auth. Callback. callbackhandler;
Import javax. Security. Auth. Callback. unsupportedcallbackexception;

Import org. Apache. ws. Security. wspasswordcallback;
Public class passwordhandler implements callbackhandler {

Private map passwords = new hashmap ();
Public passwordhandler (){
Passwords. Put ("user1", "pwd1 ");
Passwords. Put ("user2", "pwd2 ");
}
Public void handle (callback [] callbacks) throws ioexception,
Unsupportedcallbackexception {

Wspasswordcallback Pc = (wspasswordcallback) callbacks [0];
String id = pc. getidentifer ();
PC. setpassword (string) passwords. Get (ID ));
}
}

User1 and user2 indicate that multiple user names and passwords can be provided.

2. Add the following code to the services. xml configuration:

<Service>
<Name> bookserviceuthp </Name>
<Namespace> http://xfire.codehaus.org/BookService </namespace>
<Serviceclass> example. WebService. Service. bookservice </serviceclass>
<Inhandlers>
<Handler handlerclass = "org. codehaus. xfire. util. Dom. dominhandler"/>
<Bean class = "org. codehaus. xfire. Security. wss4j. wss4jinhandler" xmlns = "">
<Property name = "properties">
<Props>
<Prop key = "action"> userNameToken </prop>
<Prop key = "passwordcallbackclass">
Org. codehaus. xfire. demopasswordhandler
</Prop>
</Props>
</Property>
</Bean>

</Inhandlers>
</Service>

The Org. codehaus. xfire. demopasswordhandler is the class we just wrote, so that when someone calls the service, they will call the class to verify the password, the two packages installed will be used for encryption and decryption during password transmission. OK. The release is complete.

3. Call the service, write a main method and add

Service servicemodel = new objectservicefactory (). Create (iBook. Class); // Interface Class
Object Service = new xfireproxyfactory (). Create (servicemodel, geturl ()
+ Getservicename ());
Client client = (xfireproxy) proxy. getinvocationhandler (Service). getclient ();
Client. addouthandler (New domouthandler ());
Properties Properties = new properties ();
Properties. setproperty (wshandlerconstants. Action, wshandlerconstants. username_token );
Properties. setproperty (wshandlerconstants. password_type, wsconstants. pw_digest );
Client. addouthandler (New wss4jouthandler (properties ));
Client. setproperty (wshandlerconstants. User, "user1 ");
Client. setproperty (wshandlerconstants. pw_callback_ref, new passwordhandler ("pwd1"); // passwordhandler is the client's password processing class. The Code is as follows:

Import java. Io. ioexception;

Import javax. Security. Auth. Callback. Callback;
Import javax. Security. Auth. Callback. callbackhandler;
Import javax. Security. Auth. Callback. unsupportedcallbackexception;

Import org. Apache. ws. Security. wspasswordcallback;

Public class passwordhandler implements callbackhandler {

String password;

Public passwordhandler (string password ){
This. Password = password;
}

Public void handle (callback [] callbacks) throws ioexception,
Unsupportedcallbackexception {

Wspasswordcallback Pc = (wspasswordcallback) callbacks [0];
PC. setpassword (password );

}

}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/enxiuwang/archive/2009/03/17/3997757.aspx

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.