Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Login in UME using UME API

0 Kudos

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

1 ACCEPTED SOLUTION

0 Kudos

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();
}
2 REPLIES 2

0 Kudos

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();
}

AntalP
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Amir,

You may check the SPML web service, it is intended to maintain users remotely.

Best regards,

Antal