|
@@ -7,10 +7,13 @@ import com.example.opc_common.enums.ResultEnum;
|
|
|
import com.example.opc_common.exception.CustomException;
|
|
|
import com.example.opc_common.util.Blank;
|
|
|
import com.example.opc_common.util.Result;
|
|
|
+import com.example.opc_da.config.OpcuaClientConfig;
|
|
|
+import com.example.opc_da.config.SpringContextUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
|
|
|
import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig;
|
|
|
import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfigBuilder;
|
|
|
+import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider;
|
|
|
import org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider;
|
|
|
import org.eclipse.milo.opcua.sdk.client.api.identity.X509IdentityProvider;
|
|
|
import org.eclipse.milo.opcua.sdk.client.nodes.UaNode;
|
|
@@ -31,9 +34,6 @@ import org.eclipse.milo.opcua.stack.core.types.structured.*;
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
import java.security.KeyPair;
|
|
|
-import java.security.KeyStoreException;
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
-import java.security.UnrecoverableKeyException;
|
|
|
import java.security.cert.X509Certificate;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
@@ -45,6 +45,9 @@ import java.util.function.Predicate;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
public class OpcUaValidate implements DataSourceValidate {
|
|
|
+
|
|
|
+ private static OpcuaClientConfig opcuaClientConfig = SpringContextUtils.getBean(OpcuaClientConfig.class);
|
|
|
+
|
|
|
@Override
|
|
|
public void Validate(DataSource dataSource) {
|
|
|
if (Blank.isEmpty(dataSource.getIpAddress(), dataSource.getIpPort(), dataSource.getIdentityProvider())) {
|
|
@@ -192,7 +195,7 @@ public class OpcUaValidate implements DataSourceValidate {
|
|
|
map.put("itemReadName", str + "." + name);
|
|
|
// map.put("nodeId", nd.getNodeId());
|
|
|
map.put("nodeIndex", nd.getNodeId().getNamespaceIndex().intValue());
|
|
|
- map.put("identifier",nd.getNodeId().getIdentifier());
|
|
|
+ map.put("identifier", nd.getNodeId().getIdentifier());
|
|
|
map.put("dataType", nd.getNodeId().getType());
|
|
|
map.put("description", nd.getDescription());
|
|
|
mapLeaveList.add(map);
|
|
@@ -225,7 +228,7 @@ public class OpcUaValidate implements DataSourceValidate {
|
|
|
map.put("nodeId", nd.getNodeId());
|
|
|
map.put("nodeIndex", nd.getNodeId().getNamespaceIndex().intValue());
|
|
|
map.put("dataType", nd.getNodeId().getType());
|
|
|
- map.put("identifier",nd.getNodeId().getIdentifier());
|
|
|
+ map.put("identifier", nd.getNodeId().getIdentifier());
|
|
|
map.put("description", nd.getDescription());
|
|
|
mapList.add(map);
|
|
|
}
|
|
@@ -252,7 +255,7 @@ public class OpcUaValidate implements DataSourceValidate {
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
- private static OpcUaClientConfig getOpcUaClientConfig(DataSource dataSource) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException {
|
|
|
+ private static OpcUaClientConfig getOpcUaClientConfig(DataSource dataSource) throws Exception {
|
|
|
List<EndpointDescription> endpoints = null;
|
|
|
EndpointDescription endpoint = null;
|
|
|
String endpointUrl = "opc.tcp://" + dataSource.getIpAddress() + ":" + dataSource.getIpPort();
|
|
@@ -274,36 +277,32 @@ public class OpcUaValidate implements DataSourceValidate {
|
|
|
e.printStackTrace();
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
+ OpcUaClientConfigBuilder config = OpcUaClientConfig.builder()
|
|
|
+ .setApplicationName(LocalizedText.english(opcuaClientConfig.getAppName()))
|
|
|
+ .setApplicationUri(opcuaClientConfig.getAppUri())
|
|
|
+ .setEndpoint(endpoint);
|
|
|
+
|
|
|
+ KeyStoreLoader keyStoreLoader = new KeyStoreLoader();
|
|
|
+ keyStoreLoader.getKeyStoreLoader(opcuaClientConfig);
|
|
|
+ X509Certificate clientCertificate = keyStoreLoader.getClientCertificate();
|
|
|
+ KeyPair clientKeyPair = keyStoreLoader.getClientKeyPair();
|
|
|
+
|
|
|
+ if (dataSource.getSecurityMode() != 1) {
|
|
|
+ // 非None 需要证书和密钥
|
|
|
+ config.setCertificate(clientCertificate).setKeyPair(clientKeyPair);
|
|
|
+ }
|
|
|
//使用账号密码登录
|
|
|
if (dataSource.getIdentityProvider() == UserTokenType.UserName.getValue()) {
|
|
|
- return OpcUaClientConfig.builder()
|
|
|
- .setApplicationName(LocalizedText.english("OpcUaClientWithCertificateExample"))
|
|
|
- .setApplicationUri("urn:example:OpcUaClientWithCertificateExample")
|
|
|
- .setEndpoint(endpoint)
|
|
|
- .setIdentityProvider(new UsernameProvider(dataSource.getIpUserName(), dataSource.getIpPassword()))
|
|
|
+ return config.setIdentityProvider(new UsernameProvider(dataSource.getIpUserName(), dataSource.getIpPassword()))
|
|
|
.build();
|
|
|
//使用证书登录
|
|
|
} else if (dataSource.getIdentityProvider() == UserTokenType.Certificate.getValue()) {
|
|
|
- KeyStoreLoader keyStoreLoader = new KeyStoreLoader();
|
|
|
- keyStoreLoader.getKeyStoreLoader("jlclient-ai", dataSource.getCertificateDir(), dataSource.getCertificateKey().toCharArray());
|
|
|
- X509Certificate clientCertificate = keyStoreLoader.getClientCertificate();
|
|
|
- KeyPair clientKeyPair = keyStoreLoader.getClientKeyPair();
|
|
|
// 配置客户端SSLContext,使用证书
|
|
|
- return OpcUaClientConfig.builder()
|
|
|
- .setApplicationName(LocalizedText.english("OpcUaClientWithCertificateExample"))
|
|
|
- .setApplicationUri("urn:example:OpcUaClientWithCertificateExample")
|
|
|
- .setEndpoint(endpoint)
|
|
|
- .setCertificate(clientCertificate)
|
|
|
- .setKeyPair(clientKeyPair)
|
|
|
- .setIdentityProvider(new X509IdentityProvider(clientCertificate, clientKeyPair.getPrivate()))
|
|
|
+ return config.setIdentityProvider(new X509IdentityProvider(clientCertificate, clientKeyPair.getPrivate()))
|
|
|
.build();
|
|
|
//否则为匿名登录
|
|
|
} else {
|
|
|
- return OpcUaClientConfig.builder()
|
|
|
- .setApplicationName(LocalizedText.english("OpcUaClientWithCertificateExample"))
|
|
|
- .setApplicationUri("urn:example:OpcUaClientWithCertificateExample")
|
|
|
- .setEndpoint(endpoint)
|
|
|
- .build();
|
|
|
+ return config.setIdentityProvider(new AnonymousProvider()).build();
|
|
|
}
|
|
|
}
|
|
|
|