I'm building a Java webservice (wsdl) that will be consumed via SOAP. I need to implement a form of authentication in each webservice method so that every request is tested if the source is someone with permission.
I tried to follow this tutorial , but I did not get good results . My test code looks like this:
@WebMethod(operationName = "autentica")
public String autentica() {
MessageContext mContext = wsContext.getMessageContext();
Map http_headers = (Map) mContext.get(MessageContext.HTTP_REQUEST_HEADERS);
List userList = (List) http_headers.get("Username");
List passList = (List) http_headers.get("Password");
String username = "";
String password = "";
if(userList != null) {
username = userList.get(0).toString();
System.out.println("User: " + userList.get(0).toString());
}
if(passList != null) {
password = passList.get(0).toString();
System.out.println("Pass: " + passList.get(0).toString());
}
if (username.equals("admin") && password.equals("admin")){
return "Hello World JAX-WS - Valid User!";
}else{
return "Unknown User!";
}
}
To consume webservice I tested with SOAPUI and an ionic app using angular soap 3.0 and both the headers arrived as null, causing it to be returned "Unknown User!"
Could someone tell me what I'm doing wrong or another form of authentication that I can use?
EDIT
IONIC App
Code used in the ionic app to make the request for the webservice:
$soap.setCredentials("admin","admin");
$soap.post(url, "autentica").then(
function(response) {
console.log(response);
}
);
Console output: Unknown User!
SoapUI
Setting authentication settings:
Returnofrequest:
<S:Envelopexmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:autenticaResponse xmlns:ns2="http://service.natal.rn.gov.br/">
<return>Unknown User!</return>
</ns2:autenticaResponse>
</S:Body>
</S:Envelope>