01-16-2019 11:06 AM
I'm trying to connect to a SAP AS JAVA System and Manipulate the UME. I have a EJB WebService (HelloWorldEJB) in the Java System, I'm trying to hit HelloWorldEJB from an External application's (AppService) Restful service.
So the flow is like: UI5 Application (AppWeb) --> AppService --> HelloWorldEJB
I'm able to hit the HelloWorldEJB, but when I check the logged in user it's show as "J2EE_GUEST". This is because user authorization is not done in HelloWorldEJB. How I can login with a user in HelloWorldEJB using UME API?
AppService code to call the HelloWorldEJB:
@Path("/services")
public class RestService {
@GET
@Path("/hello")
public String sayHello() {
String result = "";
try {
java.net.URL url = new java.net.URL("http://host.com/HelloBeanService/HelloBean?wsdl");
javax.xml.namespace.QName qName = new javax.xml.namespace.QName("http://sap.com/tutorial/helloworld/", "HelloBeanService");
HelloBeanService client = new HelloBeanService(url, qName);
HelloBean helloService = client.getHelloBeanPort();
result = helloService.sayHello();
} catch (Exception e) {
result = e.toString();
}
return result;
}
}
HelloWorldEJB code to check logged in user:
@WebService(endpointInterface = "com.sap.tutorial.helloworld.HelloBeanRemote", portName = "HelloBeanPort", serviceName = "HelloBeanService", targetNamespace = "http://sap.com/tutorial/helloworld/")
@Stateless(name="HelloBean")
public class HelloBean implements HelloBeanRemote, HelloBeanLocal {
public String sayHello() {
IUser user = UMFactory.getAuthenticator().getLoggedInUser();
return "Hello" + user.getDisplayName(); //retuned "J2EE_GUEST"
//need to login user here.
}
}
I got to know I should use
ILogonAuthentication logonAuthentication =UMFactory.getLogonAuthenticator();
logonAuthentication.logon(request, response,"default")
But I'm not using Servlet Client to access the service, so how I can set the value for request and response.
Thanks,
Amir Suhail
01-18-2019 12:26 PM
I found the solution.
First we need to set following @annotation for the WebService class
@AuthenticationDT(authenticationLevel = AuthenticationEnumsAuthenticationLevel.BASIC)
This can be imported from:
import com.sap.engine.services.webservices.espbase.configuration.ann.dt.AuthenticationDT;
import com.sap.engine.services.webservices.espbase.configuration.ann.dt.AuthenticationEnumsAuthenticationLevel;
Then we need to set Web security for the Web Service. You can find the steps to do that here: https://help.sap.com/doc/saphelp_nw73ehp1/7.31.19/en-US/4b/5c8953d4be4cb9e10000000a42189b/frameset.h...
Then you can call the web service from any rest service by setting username and password like below:
try{
java.net.URL url =new java.net.URL("http://host.com/HelloBeanService/HelloBean?wsdl");
javax.xml.namespace.QName qName =new javax.xml.namespace.QName("http://sap.com/tutorial/helloworld/", "HelloBeanService");
HelloBeanService client=new HelloBeanService(url, qName);
HelloBean helloService =client.getHelloBeanPort();
// Add username and password for Basic Authentication
Map<String, Object> reqContext = ((BindingProvider) helloService).getRequestContext();
reqContext.put(BindingProvider.USERNAME_PROPERTY, "YOUR_USERNAME");
reqContext.put(BindingProvider.PASSWORD_PROPERTY, "YOUR_PASSWORD");
result= helloService.sayHello();
}
catch(Exceptione){
result=e.toString();
}
01-18-2019 12:26 PM
I found the solution.
First we need to set following @annotation for the WebService class
@AuthenticationDT(authenticationLevel = AuthenticationEnumsAuthenticationLevel.BASIC)
This can be imported from:
import com.sap.engine.services.webservices.espbase.configuration.ann.dt.AuthenticationDT;
import com.sap.engine.services.webservices.espbase.configuration.ann.dt.AuthenticationEnumsAuthenticationLevel;
Then we need to set Web security for the Web Service. You can find the steps to do that here: https://help.sap.com/doc/saphelp_nw73ehp1/7.31.19/en-US/4b/5c8953d4be4cb9e10000000a42189b/frameset.h...
Then you can call the web service from any rest service by setting username and password like below:
try{
java.net.URL url =new java.net.URL("http://host.com/HelloBeanService/HelloBean?wsdl");
javax.xml.namespace.QName qName =new javax.xml.namespace.QName("http://sap.com/tutorial/helloworld/", "HelloBeanService");
HelloBeanService client=new HelloBeanService(url, qName);
HelloBean helloService =client.getHelloBeanPort();
// Add username and password for Basic Authentication
Map<String, Object> reqContext = ((BindingProvider) helloService).getRequestContext();
reqContext.put(BindingProvider.USERNAME_PROPERTY, "YOUR_USERNAME");
reqContext.put(BindingProvider.PASSWORD_PROPERTY, "YOUR_PASSWORD");
result= helloService.sayHello();
}
catch(Exceptione){
result=e.toString();
}
01-28-2019 9:10 PM