问题:
当你UnicastRemoteObject.exportObject(instance)
. 该实例现在是否对所有客户公开可用。即使找到它的端口需要一点技巧。
这是这种情况:
我有一个 java RMI 客户端/服务器设置,我想添加一些身份验证。允许客户端在任何其他 RPC 调用工作之前使用用户/密码组合。
我在网上找到了一个简单的建议,起初看起来是个好主意。
interface LoginService implements Remote {
public MainService login(String username, char[] password) throws RemoteException;
}
interface MainService implements Remote {
/* all my real rpc calls go here */
}
这个想法是,创建一个远程对象来体现对 RPC 的身份验证后访问。并通过进行身份验证的第一层访问它。
LoginServiceImpl.login()
必须看起来像那样。
public MainService login(String username, char[] password) throws RemoteException {
/* verify username and password */
MainService service = new MainServiceImpl();
MainService stub = UnicastRemoteObject.exportObject(service, 0);
return stub;
}
因此,每个调用的客户端login()
都有自己的专用远程实例MainService
。自然,我会将整个内容包装在 ssl 中以保护纯文本密码。
这就是问题:
似乎在我导出了我的新MainServiceImpl
实例之后,它现在可以公开使用了。任何其他知道要查找什么的客户端都可以连接到它并在该MainServiceImpl
实例上进行调用。
我必须在创建 MainService 后导出它,否则 RMI 不会将存根发送到客户端。相反,它将尝试序列化 MainService 实例。
我可以将用户名粘贴在 中MainService
,但这实际上无济于事。