Xfire SOAP header-based WebService security verification tutorial

Source: Internet
Author: User

Webserice is an open web service that anyone can access, but sometimes we need to consider that only paying users can use WS. Therefore, we need to add a security authentication mechanism to ws. Of course, you can use the IP address filtering function of the firewall. The web application configuration isolates illegal users from the outermost layer. However, at the internal layer, we can also use the SOAP header to send verification data from the client, server access permission after passing Verification

First, configure ws server and WS client. The test code class name of the client is changed from client to clienttest, because we need to use a class named client of xfire.

 

First, we write the server verification class to inherit the abstracthandler

 

Package test;

Import org. codehaus. xfire. messagecontext;
Import org. codehaus. xfire. handler. abstracthandler;
Import org. JDOM. element;

Public class authenticationhandler extends acthandler {

Public void invoke (messagecontext CFX) throws exception {
If (CFX. getinmessage (). getheader () = NULL ){
Throw new org. codehaus. xfire. fault. xfirefault ("the request must contain verification information", org. codehaus. xfire. fault. xfirefault. Sender );
}
Element token = CFX. getinmessage (). getheader (). getchild ("authenticationtoken ");
If (token = NULL)
{
Throw new org. codehaus. xfire. fault. xfirefault ("the request must contain authentication information", org. codehaus. xfire. fault. xfirefault. Sender );
}

String username = token. getchild ("username"). getvalue ();
String Password = token. getchild ("password"). getvalue ();
Try
{
// Perform identity verification. Only authorized users of ABCD @ 1234 are allowed.
If (username. Equals ("ABCD") & password. Equals ("1234 "))
// This statement is not displayed
System. Out. println ("authenticated ");
Else throw new exception ();
}
Catch (exception E)
{
Throw new org. codehaus. xfire. fault. xfirefault ("invalid user name and password", org. codehaus. xfire. fault. xfirefault. Sender );
}

}

}

 

The following is the authorization information sent by the client.

 

Add the following code to clienttest. Java:

Xfireproxy proxy = (xfireproxy) proxy. getinvocationhandler (service );
Client client = proxy. getclient ();
Client. addouthandler (New clientauthenticationhandler ("ABCD1", "1234 "));

Wait a moment. Modify services. XM to bind handler to ws.

<? XML version = "1.0" encoding = "UTF-8"?>

<Beans>
<Service xmlns = "http://xfire.codehaus.org/config/1.0">
<Name> helloservice </Name>
<Namespace> http: // test/helloservice </namespace>
<Serviceclass> test. ihelloservice </serviceclass>
<Implementationclass> test. helloserviceimpl </implementationclass>
<Inhandlers>
<Handler handlerclass = "test. authenticationhandler"> </Inhandlers>
</Service>
</Beans>

In this way, we complete the encoding. start Tomcat and run the client code. This article uses ABCD @ 1234 to authorize users and use ABCD @ 1234 to access ws normally. If an incorrect account is used, the following exceptions occur:

 

Exception in thread "Main" org. codehaus. xfire. xfireruntimeexception: cocould not invoke service... nested exception is org. codehaus. xfire. fault. xfirefault: invalid user name and password
Org. codehaus. xfire. fault. xfirefault: invalid user name and password
At org. codehaus. xfire. fault. soap11faultserializer. readmessage (soap11faultserializer. Java: 31)
At org. codehaus. xfire. fault. soapfaultserializer. readmessage (soapfaultserializer. Java: 28)
At org. codehaus. xfire. Soap. handler. readheadershandler. checkforfault (readheadershandler. Java: 111)
At org. codehaus. xfire. Soap. handler. readheadershandler. Invoke (readheadershandler. Java: 67)
At org. codehaus. xfire. handler. handlerpipeline. Invoke (handlerpipeline. Java: 131)
At org. codehaus. xfire. Client. Client. onreceive (client. Java: 406)
At org. codehaus. xfire. Transport. http. httpchannel. sendviaclient (httpchannel. Java: 139)
At org. codehaus. xfire. Transport. http. httpchannel. Send (httpchannel. Java: 48)
At org. codehaus. xfire. handler. outmessagesender. Invoke (outmessagesender. Java: 26)
At org. codehaus. xfire. handler. handlerpipeline. Invoke (handlerpipeline. Java: 131)
At org. codehaus. xfire. Client. Invocation. Invoke (Invocation. Java: 79)
At org. codehaus. xfire. Client. Invocation. Invoke (Invocation. Java: 114)
At org. codehaus. xfire. Client. Client. Invoke (client. Java: 336)
At org. codehaus. xfire. Client. xfireproxy. handlerequest (xfireproxy. Java: 77)
At org. codehaus. xfire. Client. xfireproxy. Invoke (xfireproxy. Java: 57)
At $ proxy0.getuser (unknown source)
At test. clienttest. Main (clienttest. Java: 39)

 

If heade is not added in cienttest, the following exceptions occur:

Xfireproxy proxy = (xfireproxy) proxy. getinvocationhandler (service );
Client client = proxy. getclient ();
Client. addouthandler (New clientauthenticationhandler ("ABCD1", "1234 "));

Exception in thread "Main" org. codehaus. xfire. xfireruntimeexception: cocould not invoke service... nested exception is org. codehaus. xfire. fault. xfirefault: the request must contain verification information
Org. codehaus. xfire. fault. xfirefault: the request must contain verification information.
At org. codehaus. xfire. fault. soap11faultserializer. readmessage (soap11faultserializer. Java: 31)
At org. codehaus. xfire. fault. soapfaultserializer. readmessage (soapfaultserializer. Java: 28)
At org. codehaus. xfire. Soap. handler. readheadershandler. checkforfault (readheadershandler. Java: 111)
At org. codehaus. xfire. Soap. handler. readheadershandler. Invoke (readheadershandler. Java: 67)
At org. codehaus. xfire. handler. handlerpipeline. Invoke (handlerpipeline. Java: 131)
At org. codehaus. xfire. Client. Client. onreceive (client. Java: 406)
At org. codehaus. xfire. Transport. http. httpchannel. sendviaclient (httpchannel. Java: 139)
At org. codehaus. xfire. Transport. http. httpchannel. Send (httpchannel. Java: 48)
At org. codehaus. xfire. handler. outmessagesender. Invoke (outmessagesender. Java: 26)
At org. codehaus. xfire. handler. handlerpipeline. Invoke (handlerpipeline. Java: 131)
At org. codehaus. xfire. Client. Invocation. Invoke (Invocation. Java: 79)
At org. codehaus. xfire. Client. Invocation. Invoke (Invocation. Java: 114)
At org. codehaus. xfire. Client. Client. Invoke (client. Java: 336)
At org. codehaus. xfire. Client. xfireproxy. handlerequest (xfireproxy. Java: 77)
At org. codehaus. xfire. Client. xfireproxy. Invoke (xfireproxy. Java: 57)
At $ proxy0.getuser (unknown source)
At test. clienttest. Main (clienttest. Java: 35)

 

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.