简体   繁体   English

Java OPC-UA 客户端 Eclipse Milo 端点 URL 更改为 localhost

[英]Java OPC-UA Client Eclipse Milo endpoint URL changes to localhost

I am using Java OPC-UA client Eclipse Milo .我正在使用 Java OPC-UA 客户端Eclipse Milo Whenever I create a session using endpoint URL of server, method UaTcpStackClient.getEndpoints() changes URL to localhost .每当我使用服务器的端点 URL 创建会话时,方法UaTcpStackClient.getEndpoints()将 URL 更改为localhost

String endpointUrl = "opc.tcp://10.8.0.104:48809";

EndpointDescription[] endpoints = UaTcpStackClient.getEndpoints(endpointUrl).get();

EndpointDescription endpoint = Arrays.stream(endpoints)
            .filter(e -> e.getSecurityPolicyUri().equals(securityPolicy.getSecurityPolicyUri()))
            .findFirst().orElseThrow(() -> new Exception("no desired endpoints returned"));

However value of endpoint.getEndpointUrl() returns opc.tcp://127.0.0.1:4880/ which results failure in connection.然而endpoint.getEndpointUrl()值返回opc.tcp://127.0.0.1:4880/导致连接失败。

I have no idea why my OPC URL gets changed?我不知道为什么我的 OPC URL 会改变?

This is a pretty common problem when implementing a UA client.在实现 UA 客户端时,这是一个非常常见的问题。

The server is ultimately responsible for the contents of the endpoints you get back, and the one you're connecting to is (mis)configured to return 127.0.0.1 in the endpoint URLs, apparently.服务器最终负责您返回的端点的内容,而您要连接的端点显然被(错误地)配置为在端点 URL 中返回 127.0.0.1。

You need to check the endpoints you get from the server and then depending on the nature of your application either just replace them right away with new copied EndpointDescription s containing URLs that you've modified or let the user know and ask them for permission first.您需要检查从服务器获取的端点,然后根据应用程序的性质,立即将它们替换为包含您已修改的 URL 的新复制EndpointDescription ,或者让用户知道并首先请求他们的许可。

Either way, you need to create a new set of EndpointDescription s in which you've corrected the URL before you go on to create the OpcUaClient .无论哪种方式,您都需要创建一组新的EndpointDescription ,在继续创建OpcUaClient之前更正了其中的 URL。

Alternatively... you could figure out how to get your server configured properly so it returns endpoints containing a publicly reachable hostname or IP address.或者……您可以弄清楚如何正确配置您的服务器,以便它返回包含可公开访问的主机名或 IP 地址的端点。

Update 2:更新 2:

Since v0.2.2 there has been EndpointUtil.updateUrl that can be used to modify EndpointDescription s.从 v0.2.2 开始,有EndpointUtil.updateUrl可以用来修改EndpointDescription s。

Update:更新:

The code to replace the endpoint URL could be some variation of this:替换端点 URL 的代码可能是以下代码的一些变体:

private static EndpointDescription updateEndpointUrl(
    EndpointDescription original, String hostname) throws URISyntaxException {

    URI uri = new URI(original.getEndpointUrl()).parseServerAuthority();

    String endpointUrl = String.format(
        "%s://%s:%s%s",
        uri.getScheme(),
        hostname,
        uri.getPort(),
        uri.getPath()
    );

    return new EndpointDescription(
        endpointUrl,
        original.getServer(),
        original.getServerCertificate(),
        original.getSecurityMode(),
        original.getSecurityPolicyUri(),
        original.getUserIdentityTokens(),
        original.getTransportProfileUri(),
        original.getSecurityLevel()
    );
}

Caveat: this works in most cases, but one notable case it does not work is when the remote endpoint URL contains characters that aren't allowed in a URL hostname (according to the RFC), such as underscore (a '_'), which unfortunately IS allowed in eg the hostname of a Windows machine.警告:这在大多数情况下都有效,但一个值得注意的情况是当远程端点 URL 包含 URL 主机名中不允许的字符时(根据 RFC),例如下划线('_'),不幸的是,这在例如 Windows 机器的主机名中是允许的。 So you may need to use some other method of parsing the endpoint URL rather than relying on the URI class to do it.因此,您可能需要使用其他一些方法来解析端点 URL,而不是依赖 URI 类来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM