java - 使用 UME API 登录 UME

标签 java ejb-3.0 netweaver

我正在尝试连接到 SAP AS JAVA 系统并操作 UME。 我在 Java 系统中有一个 EJB WebService (HelloWorldEJB),我正在尝试从外部应用程序 (AppService) Restful 服务访问 HelloWorldEJB。

所以流程是这样的: UI5应用程序(AppWeb) --> AppService --> HelloWorldEJB

我能够访问 HelloWorldEJB,但是当我检查登录用户时,显示为“ guest 用户”。这是因为HelloWorldEJB中没有完成用户授权。

如何使用 UME API 以用户身份登录 HelloWorldEJB?

调用HelloWorldEJB的AppService代码:

@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代码:

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

我知道我们应该使用

ILogonAuthentication logonAuthentication = UMFactory.getLogonAuthenticator();

并传递 HttpServletRequest 和 HttpServletResponse

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

但我无法将 HttpServletRequest 和 HttpServletResponse 传递给登录。 我没有使用 Servlet 客户端来访问 EJB。

最佳答案

解决方案由作者透露并在另一个站点上给出。我只是将其提供给进一步的研究人员。

首先我们需要为WebService类设置以下注释:

@AuthenticationDT(authenticationLevel = AuthenticationEnumsAuthenticationLevel.BASIC)

为此,您需要以下导入

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

然后需要通过以下步骤设置 Web Service 的 Web 安全性:

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

然后可以通过用户/密码从任何 REST 服务调用 Web 服务,如下所示:

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

关于java - 使用 UME API 登录 UME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54212053/

相关文章:

abap - 导出和导入 ABAP 类

asp.net - SAP NetWeaver 7 试用版和 SAP .NET 连接器 3.0 - 可用功能?

java - 覆盖 X-Powered-By HTTP header

java - 我可以使用 JSSC 与 USB 设备通信吗

java - 在 Java 中使用比较器对列表进行排序

java - 在单个 Hibernate 事务中保存多个新实体

java - 必须或不能扩展 javax.ejb.EJBLocalObject 接口(interface)?

java - 表示层中的实体类?

java - 无法编辑项目中的特定 webdynpro java View

java - 如何从一个EJB调用另一个EJB?