Monday, November 3, 2014

Set Authentication WebService and Set Authentication Cookies in SoapUi

Removed
After development step, we need to test the Web Services. Using SoapUi, we can give the generated WSDL address and get all the exposed WebMethods.

I developed an application where authentication is needed. So the called method is authenticate which will generate a JSessionId (Cookie) :


@Resource 
private WebServiceContext wsContext;

 
@WebMethod
public User authenticate(@WebParam(name = "userName") String userName, @WebParam(name = "password") String password){
   
 User userToconnect = null;

 HttpServletRequest req = (HttpServletRequest) wsContext.getMessageContext().get(MessageContext.SERVLET_REQUEST);

 userToconnect = webService.authenticate(userName, password);
  
 req.getSession().setAttribute("User", userToconnect);
    
 return userToconnect;  
}

In next method, we will need to get the user. This one will no more be sent by the authenticated user, but it will be retrieved directly from the WebServiceContext.


public User getUser(WebServiceContext wsContext) throws Exception{

 User user = getUserFromSession(wsContext);
 if(user== null){
  throw new  Exception("User not Authenticated ", UamErrorCode.INVALID_PARAMETER);
 }
 return user.getEnterpriseName();
}


Calling the authenticate method using SoapUi, we will get a Set-Cookie in the header.



Then, in next call, user need to specify the given JsessionId. So the Authentication Cookies should be specified in SoapUi for next requests. This can be done using headers. Clic on add, Copy the JSeesionId generated by the autehnticate (copy all the row, paste, then remove the Set-Cookie). Then create a Header called Cookie, and Set the JSESSIONID.



Thant's all, now your method will work.

Don't forget to share with friends ...

No comments :

Post a Comment

Articles les plus consultés