Search code examples
javaejb-3.0netweaver

Login in UME using UME API


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 its, show as "Guest User". 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:wsldUrl");
            javax.xml.namespace.QName qName =  new javax.xml.namespace.QName("http://sap.com/qName", "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 {
        
        private String message = "Hello, ";
        public String sayHello() {
            IUser user = UMFactory.getAuthenticator().getLoggedInUser();
            return message + user.getDisplayName();
        }
}

I got to know we should use

ILogonAuthentication logonAuthentication = UMFactory.getLogonAuthenticator();

and pass HttpServletRequest and HttpServletResponse

logonAuthentication.logon(request, response, "default");

But I'm not able to get HttpServletRequest and HttpServletResponse to pass to logon. I'm not using a Servlet client to access the EJB.


Solution

  • The solution is revealed by author and given on another site. I just kindly give it here for further researchers.

    First we need to set following annotation for the WebService class:

    @AuthenticationDT(authenticationLevel = AuthenticationEnumsAuthenticationLevel.BASIC)
    

    to do this you need following imports

    import com.sap.engine.services.webservices.espbase.configuration.ann.dt.AuthenticationDT; 
    import com.sap.engine.services.webservices.espbase.configuration.ann.dt.AuthenticationEnumsAuthenticationLevel;
    

    Then one need to set Web security for the Web Service by these steps:

    https://help.sap.com/doc/saphelp_nw73ehp1/7.31.19/en-US/4b/5c8953d4be4cb9e10000000a42189b/frameset.htm

    Then web-service can be called from any REST-service by user/password like that:

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