【问题标题】:Connect to RServe from JAVA using authentication使用身份验证从 JAVA 连接到 RServe
【发布时间】:2018-04-28 11:58:20
【问题描述】:
  • 我正在使用 cmd 从服务器机器运行 RServe

    Rserve.exe --RS-conf Rserv.conf --RS-port 12306
    
  • Rserv.conf 文件有以下内容:

    pwdfile RserveAuth.txt
    需要身份验证
    远程启用
    明文禁用

  • RserveAuth.txt 有以下内容:

    管理员 123456

  • 我正在从 JAVA 连接到 R Server

    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();            
    } 
     }
    }
    
  • 到 Rserve 的连接对所有人开放,无需用户名和密码。如何添加安全性并仅允许使用有效凭据进行连接以访问 Rserve

【问题讨论】:

  • 您的 RServer 好像在 Windows Rserve.exe 上,运行 [Windows、MacOSX、Linux] 的客户端用户是什么?您的生产 RServer 在 Windows 上吗?只是检查要求。
  • @Technophobe01 RServer 在 Windows 机器上,我的客户端可以是任何连接到它的 Windows/Linux/MacOSX。对 Rserver 的连接请求是从 JAVA 类发出的,后来我使用连接对象在 Rserve 上运行脚本并将脚本的输出传递给 JSP 页面。
  • 仅供参考。 Windows 下的 RServe 版本是有限的。最重要的限制是“不支持并行连接,后续连接共享相同的命名空间”/“不支持会话 - 这是不支持并行连接的结果”。请参阅“下载/使用 Windows 版本的 Rserve 之前请阅读!”下的 rforge.net/Rserve/rserve-win.html
  • @AndreyBelykh 好点——你打败了我。我的感觉是,一种选择是通过 Windows Ubuntu 子系统运行 Rserve,然后通过 ssh 限制访问。

标签: java r windows tcp-ip rserve


【解决方案1】:

由于您在创建连接后作为第一个命令启用了身份验证,因此您需要执行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 机器的解决方案吗?
  • 是的,它适用于任何系统(我已经用 MacOS X 测试过),不,它使用本机套接字连接。
  • bcz 我按照你的解决方案得到了一个 org.rosuda.REngine.Rserve.RserveException: login failed, request status: authentication failed at org.rosuda.REngine.Rserve.RConnection.login(RConnection.java :399) 错误
  • 您确定凭据正确吗?如您所见,系统已连接,只有用户名和密码不匹配。
  • 凭据是正确的,并且与 rserv.conf 文件中的 pwdfile 中的凭据相同。感谢您的验证:)
猜你喜欢
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 2016-03-26
相关资源
最近更新 更多