Reputation: 21
EDIT : Message cleared and code added
I develop a jax-ws based web service with a basic Java client.
I use SOAP handlers to authenticate the user. One on the client side which add the userId and the token to the SOAP header and another one on the server side which get these informations and authenticate the user with the database.
Client side (simplified) :
import static modules.auth.AuthClient.userID;
import static modules.auth.AuthClient.token;
public boolean handleMessage(SOAPMessageContext context) {
//Getting SOAP headers
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
QName qname;
SOAPHeaderElement soapHeaderElement;
//Add userID in SOAP header
qname = new QName("****","UserID");
soapHeaderElement = soapHeader.addHeaderElement(qname);
soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
soapHeaderElement.addTextNode(userID);
//Add token in SOAP header
qname = new QName("****","Token");
soapHeaderElement = soapHeader.addHeaderElement(qname);
soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
soapHeaderElement.addTextNode(token);
soapMsg.saveChanges();
return true;
}
Server side (simplified) :
public boolean handleMessage(SOAPMessageContext context) {
Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (!isRequest) {
//Getting SOAP headers
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
Iterator it = soapHeader.extractHeaderElements(SOAPConstants.URI_SOAP_ACTOR_NEXT);
Node userIDNode = (Node) it.next();
String userID = (userIDNode == null) ? null : userIDNode.getValue();
Node tokenNode = (Node) it.next();
String token = (tokenNode == null) ? null : tokenNode.getValue();
//Return if the user is connected
User u = AuthWS.validToken(userID, token);
//Here I have my user but I don't know how to get it in my requested method
}
return true;
}
In order to manage users rights I would like to access my user directly in the method requested.
Example of method that can be requested :
public Project getProject(@WebParam(name = "name") String name) throws WebServiceFailure, EntityNotFoundException {
//Here I would like to verify the user's rights
try {
PreparedStatement ps = DBConnect.getStatement("SELECT name FROM projects WHERE name = '" + name + "'");
ResultSet res = ps.executeQuery();
if(res.next()){
Project p = new Project(res.getString("name"));
ps.close();
return p;
} else {
ps.close();
throw new EntityNotFoundException("Can't find the project '"+name+"'");
}
} catch (SQLException ex) {
throw new WebServiceFailure(ex.getMessage());
}
}
Many thanks
Upvotes: 0
Views: 13251
Reputation: 21
Finally, I found exactly what I was searching for :
In the section "Passing Information from the Handler Level to the Application"
Upvotes: 2