Java OPC-UA客户端Eclipse Milo端点URL更改为localhost [英] Java OPC-UA Client Eclipse Milo endpoint URL changes to localhost

查看:920
本文介绍了Java OPC-UA客户端Eclipse Milo端点URL更改为localhost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java OPC-UA客户端 Eclipse Milo .每当我使用服务器的端点URL创建会话时,方法UaTcpStackClient.getEndpoints()会将URL更改为localhost.

I am using Java OPC-UA client Eclipse Milo. Whenever I create a session using endpoint URL of server, method UaTcpStackClient.getEndpoints() changes URL to 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"));

但是endpoint.getEndpointUrl()的值将返回opc.tcp://127.0.0.1:4880/,这将导致连接失败.

However value of endpoint.getEndpointUrl() returns opc.tcp://127.0.0.1:4880/ which results failure in connection.

我不知道为什么我的OPC URL被更改了?

I have no idea why my OPC URL gets changed?

推荐答案

在实现UA客户端时,这是一个非常普遍的问题.

This is a pretty common problem when implementing a UA client.

服务器最终负责您返回的端点的内容,并且您连接到的端点的配置(错误)配置为显然在端点URL中返回127.0.0.1.

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的新复制的EndpointDescription,要么让用户知道并询问他们首先获得许可.

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 EndpointDescriptions containing URLs that you've modified or let the user know and ask them for permission first.

无论哪种方式,您都需要创建一组新的EndpointDescription,其中的URL已经更正,然后再继续创建OpcUaClient.

Either way, you need to create a new set of EndpointDescriptions in which you've corrected the URL before you go on to create the OpcUaClient.

或者...您可以弄清楚如何正确配置服务器,以便它返回包含可公开访问的主机名或IP地址的端点.

Alternatively... you could figure out how to get your server configured properly so it returns endpoints containing a publicly reachable hostname or IP address.

更新:

替换端点URL的代码可能是以下形式的变体:

The code to replace the endpoint URL could be some variation of this:

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

注意:在大多数情况下都可以使用该提示,但值得注意的一种情况是,当远程端点URL包含URL主机名(根据RFC)中不允许使用的字符时,例如下划线('_ '),不幸的是,例如Windows计算机的主机名.因此,您可能需要使用其他方法来解析终结点URL,而不是依赖URI类来完成它.

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 e.g. the hostname of a Windows machine. So you may need to use some other method of parsing the endpoint URL rather than relying on the URI class to do it.

这篇关于Java OPC-UA客户端Eclipse Milo端点URL更改为localhost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆