You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过Java程序无用户名密码连接KepwareserverEx.V5?

Java连接Kepware Server EX V5:无认证方案与可用库

首先得明确核心前提:要实现无用户名密码连接,必须先在Kepware Server端开启匿名访问权限——这是必要条件,不然任何客户端都没法绕过认证。你可以在Kepware的OPC UA Server配置面板里找到「Security」相关选项,开启匿名登录允许。

接下来聊聊可用的Java库和具体实现:

一、推荐的Java OPC UA库

你提到的Digital Petri UA SDK其实是支持匿名认证的,另外更常用且文档友好的是Eclipse Milo(开源纯Java实现),这两个库都是基于OPC UA协议的,比依赖COM组件的OPC DA方案更稳定、跨平台性更好。

依赖引入(以Eclipse Milo为例)

如果用Maven,在pom.xml中添加以下依赖:

<dependency>
    <groupId>org.eclipse.milo</groupId>
    <artifactId>sdk-client</artifactId>
    <version>0.6.10</version>
</dependency>
<dependency>
    <groupId>org.eclipse.milo</groupId>
    <artifactId>stack-client</artifactId>
    <version>0.6.10</version>
</dependency>

二、无用户名密码的连接代码实现

下面是用Eclipse Milo实现匿名连接Kepware的示例代码,核心是配置AnonymousIdentityToken来跳过账号密码验证:

import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig;
import org.eclipse.milo.opcua.stack.client.UaTcpStackClient;
import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText;
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
import org.eclipse.milo.opcua.stack.core.types.structured.AnonymousIdentityToken;

import java.util.concurrent.CompletableFuture;

public class KepwareAnonymousClient {
    public static void main(String[] args) throws Exception {
        // Kepware OPC UA默认端点地址,端口一般为49320,可根据实际配置调整
        String endpointUrl = "opc.tcp://localhost:49320";

        // 获取服务器端点列表
        CompletableFuture<EndpointDescription[]> endpointsFuture = UaTcpStackClient.getEndpoints(endpointUrl);
        EndpointDescription[] endpoints = endpointsFuture.get();

        // 筛选支持匿名访问的端点(优先选无安全策略的端点)
        EndpointDescription targetEndpoint = null;
        for (EndpointDescription ed : endpoints) {
            if (SecurityPolicy.None.getUri().equals(ed.getSecurityPolicyUri())) {
                targetEndpoint = ed;
                break;
            }
        }

        if (targetEndpoint == null) {
            throw new Exception("未找到支持匿名访问的端点,请检查Kepware的匿名权限配置");
        }

        // 构建客户端配置,指定匿名身份令牌
        OpcUaClientConfig clientConfig = OpcUaClientConfig.builder()
                .setEndpoint(targetEndpoint)
                .setIdentityProvider(session -> CompletableFuture.completedFuture(new AnonymousIdentityToken()))
                .setApplicationName(LocalizedText.english("Kepware Anonymous Client"))
                .setApplicationUri("urn:kepware-anonymous-client")
                .build();

        // 创建并连接客户端
        try (OpcUaClient client = OpcUaClient.create(clientConfig)) {
            client.connect().get();
            System.out.println("成功连接到Kepware Server!");

            // 这里可以添加读写标签的逻辑,示例:读取指定节点的值
            // NodeId tagNodeId = new NodeId(2, "Channel1.Device1.Tag1");
            // DataValue tagValue = client.readValue(0.0, TimestampsToReturn.Both, tagNodeId).get();
            // System.out.println("标签值:" + tagValue.getValue().getValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

三、关于VB方案的补充

你之前用VB的Interop.OPCAutomation.dll是基于OPC DA协议的,Java也有OPC DA相关库(比如JEasyOPC),但这类库需要依赖Windows的COM组件,配置复杂且跨平台性差,更推荐上面的OPC UA方案——毕竟Kepware本身原生支持OPC UA,这是更现代、更稳定的工业通信协议。

注意事项

  1. 确认Kepware的OPC UA Server已启动,且在配置中开启了匿名访问权限;
  2. 检查防火墙是否放行Kepware的OPC UA端口(默认49320);
  3. 如果Kepware配置了加密安全策略,需确保对应端点允许匿名访问,代码中可调整端点筛选逻辑适配。

内容的提问来源于stack exchange,提问作者anusha

火山引擎 最新活动