Xfire implements identity authentication (xfire SOAP header-based WebService Security Authentication)

Source: Internet
Author: User

 

1. Create a Web Services Project (xfire.
Ii. Add the authentication function
1. First, write the server verification class to inherit the abstracthandler class.
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 );
}
}
}
2. The client constructs the authorization information
Package test;
Import org. codehaus. xfire. messagecontext;
Import org. codehaus. xfire. handler. abstracthandler;
Import org. JDOM. element;

Public class clientauthenticationhandler extends acthandler {

Private string username = NULL;

Private string Password = NULL;

Public clientauthenticationhandler (){
}

Public clientauthenticationhandler (string username, string password ){
This. Username = username;
This. Password = password;
}

Public void setusername (string username ){
This. Username = username;
}

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

Public void invoke (messagecontext context) throws exception {

// Construct verification information for the SOAP Header
Element El = new element ("Header ");
Context. getoutmessage (). setheader (EL );
Element auth = new element ("authenticationtoken ");
Element username_el = new element ("username ");
Username_el.addcontent (username );
Element password_el = new element ("password ");
Password_el.addcontent (password );
Auth. addcontent (username_el );
Auth. addcontent (password_el );
El. addcontent (auth );
}
}
3. Modify services. XML to bind handler to Web Services
<? XML version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://xfire.codehaus.org/config/1.0">
<Service xmlns = "http://xfire.codehaus.org/config/1.0">
<Name> Hello </Name>
<Namespace> http: // test/helloservice </namespace>
<Serviceclass> test. ihello </serviceclass>
<Implementationclass> test. helloimpl </implementationclass>
<Inhandlers>
<Handler handlerclass = "test. authenticationhandler"> </Inhandlers>
<Style> wrapped </style>
<Use> literal </use>
<Scope> application </scope>
</Service>
</Beans>
4. Create a clienttest class for testing.
Package test;
Import java. Lang. Reflect. proxy;
Import java.net. malformedurlexception;
Import org. codehaus. xfire. Client .*;
Import org. codehaus. xfire. Service. Service;
Import org. codehaus. xfire. Service. Binding. objectservicefactory;

Public class clienttest {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Try {
Service servicemodel = new objectservicefactory (). Create (ihello. Class );
Ihello service = (ihello) New xfireproxyfactory (). Create (servicemodel,
"Http: // dracom-d1514b82: 8080/web_services3/services/Hello ");
Xfireproxy proxy = (xfireproxy) proxy. getinvocationhandler (service );
Client client = proxy. getclient ();
// Send authorization information
Client. addouthandler (New clientauthenticationhandler ("ABCD", "1234 "));
// Output the returned information for calling the Web Services Method
System. Out. println (service. getmessage ("Hello AAA "));
} Catch (malformedurlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (illegalargumentexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
3. In this way, the code is complete. Start Web Services and run the client code. This article uses ABCD @ 1234-bit authorized users,
With ABCD @ 1234, you can access web services normally. If an incorrect account is used, the following exception occurs:
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.getmessage (unknown source)
At test. clienttest. Main (clienttest. Java: 24)

If heade is not stored in cienttest, the following exception occurs:
Xfireproxy proxy = (xfireproxy) proxy. getinvocationhandler (service );
Client client = proxy. getclient ();
// Send authorization information
Client. addouthandler (New clientauthenticationhandler ("ABCD1", "1234 "));

Information: fault occurred!
Org. codehaus. xfire. fault. xfirefault: the request must contain verification information.
At test. authenticationhandler. Invoke (authenticationhandler. Java: 11)
At org. codehaus. xfire. handler. handlerpipeline. Invoke (handlerpipeline. Java: 131)
At org. codehaus. xfire. Transport. defaultendpoint. onreceive (defaultendpoint. Java: 64)
At org. codehaus. xfire. Transport. abstractchannel. Receive (abstractchannel. Java: 38)
At org. codehaus. xfire. Transport. http. xfireservletcontroller. Invoke (xfireservletcontroller. Java: 304)
At org. codehaus. xfire. Transport. http. xfireservletcontroller. doservice (xfireservletcontroller. Java: 129)
At org. codehaus. xfire. Transport. http. xfireservlet. dopost (xfireservlet. Java: 116)
At javax. servlet. http. httpservlet. Service (httpservlet. Java: 710)
At javax. servlet. http. httpservlet. Service (httpservlet. Java: 803)
At org. Apache. Catalina. Core. applicationfilterchain. internaldofilter (applicationfilterchain. Java: 290)
At org. Apache. Catalina. Core. applicationfilterchain. dofilter (applicationfilterchain. Java: 206)
At org. Apache. Catalina. Core. standardwrappervalve. Invoke (standardwrappervalve. Java: 230)
At org. Apache. Catalina. Core. standardcontextvalve. Invoke (standardcontextvalve. Java: 175)
At org. Apache. Catalina. Core. standardhostvalve. Invoke (standardhostvalve. Java: 128)
At org. Apache. Catalina. Valves. errorreportvalve. Invoke (errorreportvalve. Java: 104)
At org. Apache. Catalina. Core. standardenginevalve. Invoke (standardenginevalve. Java: 109)
At org. Apache. Catalina. connector. coyoteadapter. Service (coyoteadapter. Java: 261)
At org. Apache. Coyote. http11.http11processor. Process (http11processor. Java: 844)
At org. Apache. Coyote. http11.http11protocol $ http11connectionhandler. Process (http11protocol. Java: 581)
At org.apache.tomcat.util.net. jioendpoint $ worker. Run (jioendpoint. Java: 447)
At java. Lang. thread. Run (thread. Java: 619)

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.