简体   繁体   中英

Secure WS client with UsernameToken(SOAP security header)

I'm trying to secure my WS client to be able to call the WS.
My code looks like this:

            SendSmsService smsService = new SendSmsService();
SendSms sendSMS = smsService.getSendSms();  
BindingProvider stub = (BindingProvider)sendSMS;

//Override endpoint with local copy of wsdl.
String URL ="";//here is the wsdl url
Map<String,Object> requestContext = stub.getRequestContext();
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, URL);

//Set usernametoken
URL fileURL = loader.getResource("client-config.xml");
File file = new File(fileURL.getFile());

FileInputStream clientConfig = null;
try {
 clientConfig = new FileInputStream(file);
} catch (FileNotFoundException e) {
 e.printStackTrace();
}

XWSSecurityConfiguration config = null;
try {
 config = SecurityConfigurationFactory.newXWSSecurityConfiguration(clientConfig);
} catch (Exception e) {
 e.printStackTrace();
 log.warn("Exception: "+e.getMessage());
}
requestContext.put(XWSSecurityConfiguration.MESSAGE_SECURITY_CONFIGURATION, config);

//Invoke the web service

 String requestId = null;
 try {
  requestId = sendSMS.sendSms(addresses, senderName, charging, message,   receiptRequest);
 } catch (PolicyException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (ServiceException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }

and the config file looks like this:

<xwss:JAXRPCSecurity xmlns:xwss="http://java.sun.com/xml/ns/xwss/config"   optimize="true">
 <xwss:Service>
  <xwss:SecurityConfiguration dumpMessages="true"
   xmlns:xwss="http://java.sun.com/xml/ns/xwss/config">
    <xwss:UsernameToken name="username" password="password>
  </xwss:SecurityConfiguration>
 </xwss:Service>
 <xwss:SecurityEnvironmentHandler>
  util.SecurityEnvironmentHandler
</xwss:SecurityEnvironmentHandler>
</xwss:JAXRPCSecurity>

The SecurityEnviromentHandler is a dummy class that implements javax.security.auth.callback.CallbackHandler.

Authentication must be in compliance with Oasis Web Services Security Username Token Profile 1.0.
But I'm constantly getting "Security header not valid" error.
Where am I going wrong, can anyone tell me.
I used wsimport(JAX_WS 2.1 to generate classes for my client)
Note:Only thing I know about this WS is WSDL URL and user&pass for authentication

SOLUTION
I solved the problem. The thing that was going wrong is that client-config.xml file cause I didn't know how to set it properly. I ran into this example and used it:
http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client
Just copied those 2 classes on the link into my projects structure and called them, something like this:

SendSmsService smsService = new SendSmsService();
HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver();
smsService.setHandlerResolver(handlerResolver);
SendSms sendSMS = smsService.getSendSms();

Now it works perfectly!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM