使用身份验证从JAVA连接到RServe

13
  • I am running RServe from Server machine using cmd

    Rserve.exe --RS-conf Rserv.conf --RS-port 12306
    
  • Rserv.conf file has following content:

    pwdfile RserveAuth.txt
    auth required
    remote enable
    plaintext disable

  • RserveAuth.txt has following contents:

    Admin 123456

  • I am connecting to R Server from JAVA

    import org.rosuda.REngine.REXPMismatchException;
    import org.rosuda.REngine.REngineException;
    import org.rosuda.REngine.Rserve.RConnection;
    import org.rosuda.REngine.Rserve.RserveException;
    import org.rosuda.REngine.REXP;
    import org.rosuda.REngine.*;
    public class ConnecttoR
    {
      ...
      ...
     public void connectR()
     {
         try 
        { 
            RConnection connection = new RConnection("172.16.33.242",12306); // Works if authentication is not required in Rserv.conf 
        }
        catch (RserveException e) {
        e.printStackTrace();
        } 
        catch(REXPMismatchException e){
        e.printStackTrace();
        }
        catch(REngineException e){
        e.printStackTrace();            
    } 
     }
    }
    
  • Connection to Rserve is open to all without username & Password. How shall I add security and allow connection only with valid credentials to access Rserve


看起来你的 RServer 是在 Windows 上运行的 Rserve.exe,客户端用户是在运行什么操作系统 [Windows、MacOSX、Linux]?你的生产环境的 RServer 是在 Windows 上吗?只是检查一下需求。 - Technophobe01
@Technophobe01,RServer在Windows机器上运行,我的客户端可以是任何连接到它的Windows/Linux/MacOSX。连接请求是从JAVA类发出的,然后我使用连接对象在Rserve上运行脚本,并将脚本的输出传递给JSP页面。 - Akki
请注意,Windows下的RServe版本存在限制。最重要的限制是“不支持并行连接,后续连接共享相同的命名空间”/“不支持会话 - 这是因为不支持并行连接的结果”。请参阅https://www.rforge.net/Rserve/rserve-win.html中的“下载/使用Rserve Windows版本之前请阅读!” - Andrey Belykh
@AndreyBelykh 很好的观点 - 你比我先说了。我的感觉是,一种选择是通过Windows Ubuntu子系统运行Rserve,然后通过ssh限制访问。 - Technophobe01
1个回答

4

在创建连接后启用身份验证后,您需要首先执行login命令。Java库为此提供了特殊的包装器。

以下是示例用法的代码:

RConnection connection = new RConnection("127.0.0.1",12306);
connection.login("Admin", "123456");
REXP x = connection.eval("R.version.string");
System.out.println(x.asString());

此外,我建议将完整路径作为pwdfile的值。

它在内部使用SSH进行连接吗?这是一个适用于Windows和Linux系统的解决方案吗? - Madhavi Jouhari
是的,它适用于任何系统(我已经在MacOS X上测试过),而且它使用本地套接字连接。 - Babl
因为我按照您的解决方案操作,但出现了org.rosuda.REngine.Rserve.RserveException: login failed, request status: authorization failed at org.rosuda.REngine.Rserve.RConnection.login(RConnection.java:399)错误。 - Madhavi Jouhari
你确定凭据是正确的吗?就像您所看到的系统已经连接,只有用户名和密码不匹配。 - Babl
凭据是正确的,也与rserv.conf文件中的pwdfile相同。虽然感谢您的验证 :) - Madhavi Jouhari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接