4
I am building a Java webservice (wsdl) that will be consumed via SOAP. I need to implement a form of authentication in each method of the webservice so that each request is tested if the source is someone with permission.
I tried to follow this tutorial, but I didn’t get good results. My test code looked 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 the tested webservice with SOAPUI and an Ionic app using angular Soap 3.0 and in both headings came 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 request for the webservice:
$soap.setCredentials("admin","admin");
$soap.post(url, "autentica").then(
function(response) {
console.log(response);
}
);
Console output: Unknown User!
Soapui
Setting the authentication settings:
Return of the requisition:
<S:Envelope xmlns: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>
Is the client code the same as in the tutorial? If not, post it as well...
– Genos
Why not create a "token" tag? And if this token matches your registration, it is allowed
– Lucas Torres
To enable BASIC authentication in a Webservice created with JAX-WS, the easiest way is to place a security restriction on the web.xml of the War package where it is. With this done, your IONIC or SOAPUI client will normally process the "Authorization" header and validate the user/password in the Resell that is configured for your application using JAAS. In your code, vc. will have access to the username using a type variable Webservicecontext annotated with @Resource and calling getUserPrincipal() method from it.
– Philippe Sevestre
@Lucastorres The token strategy is valid, but the problem lies in receiving or sending data between endpoints.
– Hamurabi Araujo
@Philippesevestre I will review the code and try to do as you suggested. Soon I will return.
– Hamurabi Araujo
@Hamurabiaraujo An extra advantage of doing "by the spec": Depending on your application server, the change to support other authentication mechanisms (e.g., WS-Security) is restricted to configuration, without changing the code.
– Philippe Sevestre