一、前言
opc ua(open platform communications unified architecture)是针对工业自动化领域的跨平台通信协议标准。它在 opc 经典版本的基础上进行优化,可以在不同操作系统、设备和编程语言之间进行安全且可靠的数据交换。对于很多工业控制、设备监控以及物联网相关项目,opc ua 是常用的数据通信方式。
在 java 中,我们常用的 opc ua 客户端开发库包括:
- eclipse milo
- prosys opc ua sdk for java
- 其他商业或开源的 java sdk
本篇将使用 eclipse milo 作为示例库,演示如何在 java 中使用匿名、用户名密码以及证书加密三种方式连接到 opc ua 服务器。若需要使用其他 sdk,原理大同小异,api 的调用方式会有所不同。
二、准备工作
jdk
建议至少使用 jdk 8 或更高版本。
maven 或 gradle
便于引入 eclipse milo 等依赖。如果使用 maven,请在 pom.xml
中添加以下依赖:
<dependency> <groupid>org.eclipse.milo</groupid> <artifactid>sdk-client</artifactid> <version>0.6.15</version> <!-- 版本号可根据需要更新 --> </dependency> <dependency> <groupid>org.eclipse.milo</groupid> <artifactid>server-examples</artifactid> <version>0.6.15</version> </dependency>
如果使用 gradle,则在 build.gradle
中添加:
implementation 'org.eclipse.milo:sdk-client:0.6.15'
opc ua 服务器
本地或远程的 opc ua 服务器环境,用于测试连接。可以在虚拟机或本地主机上安装开源的 opc ua 服务器,也可以使用商业软件自带的模拟服务器。
证书文件(仅在证书加密方式时需要)
若您在服务器上开启了证书加密,需要准备好客户端证书(public key)和客户端私钥(private key),也可能需要服务器的信任证书。eclipse milo 提供了简单的证书管理机制,或者您也可以使用标准 java keystore 的方式来存储并读取证书和私钥。
三、匿名方式连接
3.1 匿名方式简介
匿名连接是最简单的方式,不需要用户名、密码或任何证书。只要服务器允许匿名访问,就可以通过匿名方式连接。适合在测试环境或对安全要求不高的场景下使用。
3.2 示例代码
以下演示最基本的匿名连接流程,包括:
- 创建 opc ua client 配置
- 初始化并连接到服务器
- 读取或写入数据(仅作示例)
请确保替换示例中的 endpointurl
与 nodeid
等信息为你自己的实际配置。
import org.eclipse.milo.opcua.sdk.client.opcuaclient; import org.eclipse.milo.opcua.stack.core.security.securitypolicy; import org.eclipse.milo.opcua.sdk.client.api.config.opcuaclientconfigbuilder; import org.eclipse.milo.opcua.stack.core.types.structured.endpointdescription; import org.eclipse.milo.opcua.stack.core.types.structured.usertokenpolicy; import org.eclipse.milo.opcua.stack.core.types.enumerated.usertokentype; import org.eclipse.milo.opcua.sdk.client.api.identity.anonymousprovider; import java.util.list; import java.util.concurrent.completablefuture; public class opcuaanonymousexample { public static void main(string[] args) { try { // opc ua 服务器地址,例如 "opc.tcp://localhost:49320" string url= "opc.tcp://127.0.0.1:49320"; // 创建 client opcuaclient client = opcuaclient.create(url, endpoints -> endpoints.stream() .filter(e -> e.getsecuritypolicyuri().equals(securitypolicy.none.geturi())) .findfirst(), configbuilder -> configbuilder //访问方式 .setidentityprovider(new anonymousprovider()) .setrequesttimeout(uinteger.valueof(5000)) .build()); } // 连接到服务器 completablefuture<opcuaclient> future = client.connect(); future.get(); // 等待连接完成 system.out.println("匿名连接成功!"); // 在此处可以进行读写操作,例如读取节点的值 // client.readvalue(0, timestampstoreturn.both, new readvalueid(nodeid, ...)); // ... // 最后断开连接 client.disconnect().get(); system.out.println("客户端断开连接。"); } catch (exception e) { e.printstacktrace(); } } // 简单选择一个安全策略为 none 的端点(匿名方式一般使用安全策略none,具体看服务器配置) private static endpointdescription choosesecureendpoint(list<endpointdescription> endpoints) { endpointdescription result = null; for (endpointdescription e : endpoints) { if (e.getsecuritypolicyuri().equals(securitypolicy.none.geturi())) { result = e; break; } } return result; } }
在上述示例中,最关键的步骤是将身份认证方式设为 new anonymousprovider()
并选择一个 securitypolicy 为 none
的 endpoint。这样即可使用匿名方式成功连接。
四、用户名密码方式连接
4.1 用户名密码方式简介
在实际生产环境中,常常需要使用账号密码进行身份验证,以限制访问权限、保护关键信息。与匿名方式相比,多了用户名密码的配置,但整体流程类似。
4.2 示例代码
import org.eclipse.milo.opcua.sdk.client.opcuaclient; import org.eclipse.milo.opcua.stack.core.security.securitypolicy; import org.eclipse.milo.opcua.sdk.client.api.config.opcuaclientconfigbuilder; import org.eclipse.milo.opcua.stack.core.types.structured.endpointdescription; import org.eclipse.milo.opcua.stack.core.types.structured.usertokenpolicy; import org.eclipse.milo.opcua.stack.core.types.enumerated.usertokentype; import org.eclipse.milo.opcua.sdk.client.api.identity.usernameprovider; import java.util.list; import java.util.concurrent.completablefuture; public class opcuausernamepasswordexample { public static void main(string[] args) { try { string endpointurl = "opc.tcp://127.0.0.1:4840"; list<endpointdescription> endpoints = opcuaclient .getendpoints(endpointurl).get(); endpointdescription endpoint = chooseusernameendpoint(endpoints); opcuaclientconfigbuilder configbuilder = new opcuaclientconfigbuilder(); configbuilder.setendpoint(endpoint); // 假设用户名为 "user", 密码为 "password" configbuilder.setidentityprovider(new usernameprovider("user", "password")); opcuaclient client = opcuaclient.create(configbuilder.build()); completablefuture<opcuaclient> future = client.connect(); future.get(); system.out.println("用户名密码方式连接成功!"); // 进行后续读写操作 // ... client.disconnect().get(); system.out.println("客户端断开连接。"); } catch (exception e) { e.printstacktrace(); } } private static endpointdescription chooseusernameendpoint(list<endpointdescription> endpoints) { // 通常 opc ua 服务器也支持 securitypolicy.none + username 方式 // 也可能是 basic128rsa15, basic256, etc. 具体看服务端配置 for (endpointdescription e : endpoints) { if (e.getsecuritypolicyuri().equals(securitypolicy.none.geturi())) { // 确保端点支持 username 类型的认证 for (usertokenpolicy tokenpolicy : e.getuseridentitytokens()) { if (tokenpolicy.gettokentype() == usertokentype.username) { return e; } } } } return null; } }
要点说明:
- 将 identityprovider 切换为 new usernameprovider("username", "password")。
- 根据服务端提供的用户名、密码进行配置。
- 需要注意端点是否支持 username 类型认证。如果端点仅支持 anonymous 或 certificate,则无法使用用户名密码方式。
五、证书加密方式连接
5.1 证书加密方式简介
在实际工业环境中,安全性要求更高时通常会启用证书加密(基于 public key infrastructure)。
- 每个客户端都会持有一份证书(公钥)和对应的私钥,服务器端也有自己的证书。
- 当客户端与服务器通信时,会先验证双方的证书签名并进行加密传输,从而保证安全性与完整性。
在这种方式下,服务端可能要求:
- 客户端必须提供已经被服务器信任(或在服务器端手动信任)的证书。
- 采用特定的安全策略(例如
basic256sha256
)并通过相应端点连接。
5.2 证书和私钥获取
- 可以通过第三方工具(例如 openssl、keytool 或 eclipse milo 提供的证书工具脚本)生成自签名证书。
- 生成后的证书和私钥,可以存储在 java keystore 中,或者存储为
.der
、.pem
等格式并让应用程序读取。
下方示例假设已经拥有 clientcert.der
(客户端公钥)和 clientkey.der
(客户端私钥),并且服务器端配置了对应的信任或信任链。
opc ua访问证书类
import org.eclipse.milo.opcua.sdk.server.util.hostnameutil; import org.eclipse.milo.opcua.stack.core.util.selfsignedcertificatebuilder; import org.eclipse.milo.opcua.stack.core.util.selfsignedcertificategenerator; import org.slf4j.logger; import org.slf4j.loggerfactory; import java.io.inputstream; import java.io.outputstream; import java.nio.file.files; import java.nio.file.path; import java.security.*; import java.security.cert.x509certificate; import java.util.regex.pattern; class keystoreloader { private final logger logger = loggerfactory.getlogger(getclass()); private static final pattern ip_addr_pattern = pattern.compile( "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"); // 证书别名 private static final string client_alias = "client-ai"; // 获取私钥的密码 private static final char[] password = "password".tochararray(); // 证书对象 private x509certificate clientcertificate; // 密钥对对象 private keypair clientkeypair; keystoreloader load(path basedir) throws exception { // 创建一个使用`pkcs12`加密标准的keystore。keystore在后面将作为读取和生成证书的对象。 keystore keystore = keystore.getinstance("pkcs12"); // pkcs12的加密标准的文件后缀是.pfx,其中包含了公钥和私钥。 // 而其他如.der等的格式只包含公钥,私钥在另外的文件中。 path serverkeystore = basedir.resolve("example-client.pfx"); logger.info("loading keystore at {}", serverkeystore); // 如果文件不存在则创建.pfx证书文件。 if (!files.exists(serverkeystore)) { keystore.load(null, password); // 用2048位的ras算法。`selfsignedcertificategenerator`为milo库的对象。 keypair keypair = selfsignedcertificategenerator.generatersakeypair(2048); // `selfsignedcertificatebuilder`也是milo库的对象,用来生成证书。 // 中间所设置的证书属性可以自行修改。 selfsignedcertificatebuilder builder = new selfsignedcertificatebuilder(keypair) .setcommonname("eclipse milo example client") .setorganization("digitalpetri") .setorganizationalunit("dev") .setlocalityname("folsom") .setstatename("ca") .setcountrycode("us") .setapplicationuri("urn:eclipse:milo:examples:client") .adddnsname("localhost") .addipaddress("127.0.0.1"); // get as many hostnames and ip addresses as we can listed in the certificate. for (string hostname : hostnameutil.gethostnames("0.0.0.0")) { if (ip_addr_pattern.matcher(hostname).matches()) { builder.addipaddress(hostname); } else { builder.adddnsname(hostname); } } // 创建证书 x509certificate certificate = builder.build(); // 设置对应私钥的别名,密码,证书链 keystore.setkeyentry(client_alias, keypair.getprivate(), password, new x509certificate[]{certificate}); try (outputstream out = files.newoutputstream(serverkeystore)) { // 保存证书到输出流 keystore.store(out, password); } } else { try (inputstream in = files.newinputstream(serverkeystore)) { // 如果文件存在则读取 keystore.load(in, password); } } // 用密码获取对应别名的私钥。 key serverprivatekey = keystore.getkey(client_alias, password); if (serverprivatekey instanceof privatekey) { // 获取对应别名的证书对象。 clientcertificate = (x509certificate) keystore.getcertificate(client_alias); // 获取公钥 publickey serverpublickey = clientcertificate.getpublickey(); // 创建keypair对象。 clientkeypair = new keypair(serverpublickey, (privatekey) serverprivatekey); } return this; } // 返回证书 x509certificate getclientcertificate() { return clientcertificate; } // 返回密钥对 keypair getclientkeypair() { return clientkeypair; } }
5.3 示例代码
public static opcuaclient initclient(string url,securitypolicy securitypolicy) { try { if (securitypolicy.equals(securitypolicy.none)){ return opcuaclient.create(url, endpoints -> endpoints.stream() .filter(e -> e.getsecuritypolicyuri().equals(securitypolicy.geturi())) .findfirst(), configbuilder -> configbuilder //访问方式 .setidentityprovider(new anonymousprovider()) .setrequesttimeout(uinteger.valueof(5000)) .build()); } path securitytempdir = paths.get(system.getproperty("java.io.tmpdir"), "security"); files.createdirectories(securitytempdir); if (!files.exists(securitytempdir)) { throw new exception("unable to create security dir: " + securitytempdir); } keystoreloader loader = new keystoreloader().load(securitytempdir); file pkidir = securitytempdir.resolve("pki").tofile(); defaulttrustlistmanager trustlistmanager = new defaulttrustlistmanager(pkidir); defaultclientcertificatevalidator certificatevalidator = new defaultclientcertificatevalidator(trustlistmanager); string hostname = inetaddress.getlocalhost().gethostname(); return opcuaclient.create(url, endpoints -> endpoints.stream() .map(endpoint -> { // 构建一个新的 endpointdescription(可选修改某些字段) return new endpointdescription( url, endpoint.getserver(), endpoint.getservercertificate(), endpoint.getsecuritymode(), // 或者强制改为某种模式 endpoint.getsecuritypolicyuri(), endpoint.getuseridentitytokens(), endpoint.gettransportprofileuri(), endpoint.getsecuritylevel() ); }) .filter(e -> e.getsecuritypolicyuri().equals(securitypolicy.geturi())) .findfirst(), configbuilder -> configbuilder //访问方式 .setapplicationname(localizedtext.english("datacollector-driver")) .setapplicationuri(string.format("urn:%s:opcua-client", hostname)) // 必须与证书中的uri一致 .setkeypair(loader.getclientkeypair()) .setcertificate(loader.getclientcertificate()) .setcertificatechain(loader.getclientcertificatechain()) .setcertificatevalidator(certificatevalidator) .setidentityprovider(new usernameprovider("admin", "123456")) .setrequesttimeout(uinteger.valueof(5000)) .build()); } catch (exception e) { throw new runtimeexception(e); } }
证书路径
我们需要把服务器证书放在pki\trusted\certs
目录下:
要点说明:
- 选择合适的安全策略(如
basic256sha256
)。 - 使用客户端证书和私钥(可以自签名,也可以通过权威 ca 签发)。
- 服务端需信任此客户端证书(在服务器配置中添加到信任列表)。
- 配置
certificatemanager
、certificatevalidator
以及x509identityprovider
。
六、常见问题与注意事项
端点选择
- 不同 opc ua 服务器可能同时暴露多个端点,包含不同的安全模式(security mode)和安全策略(security policy)。
- 在匿名或用户名密码方式时,如果选择了需要证书的端点,就会出现认证失败或连接被拒的情况。
- 在证书加密方式时,如果选择了安全策略为 none 的端点,则证书不会被使用,同样也会连接异常或者导致安全策略不匹配。
服务器信任客户端证书
- 大多数 opc ua 服务器在默认情况下不信任任何客户端的证书,需要在服务端管理界面或配置文件中手动将客户端证书加入白名单。
- 记得查看服务器日志,若提示「untrusted certificate」,就需要在服务器端操作信任列表。
安全策略与性能
- 加密等级越高(如 basic256sha256),对 cpu 资源消耗越大,通信速度会相对降低,但数据安全性更强。
- 在测试环境或低安全需求的场景下可以先使用 securitypolicy.none ;正式项目上线时再切换到更高的安全策略。
兼容性
- 不同版本的 opc ua sdk、服务器或 java 版本之间可能存在兼容性问题;如果连接失败,可以尝试升级或降低 milo 版本、换用不同的 jdk 版本等。
- opc ua 服务器上若启用特定的加密算法(例如 aes-256),客户端也需要对应的加密套件。
断线重连
- 工业现场环境中网络抖动常见,客户端需要实现断线重连或重试机制,以确保数据采集的连续性与稳定性。
到此这篇关于java连接opcua的文章就介绍到这了,更多相关java连接opcua内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论