@Component public class ClientGen {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载加密类型</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> { Security.addProvider(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BouncyCastleProvider()); } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Logger log =<span style="color: rgba(0, 0, 0, 1)"> LoggerFactory.getLogger(getClass()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> milo用来连接opc ua的client</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> OpcUaClient opcUaClient; @Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> OpcUaConfig opcUaConfig; </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 创建opc ua客户端(在项目启动的时候执行该方法) * </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @PostConstruct </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> createClient_() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> UaException { String endPoint </span>=<span style="color: rgba(0, 0, 0, 1)"> opcUaConfig.getEndpointUrl(); opcUaClient </span>=<span style="color: rgba(0, 0, 0, 1)"> OpcUaClient.create( endPoint, endpointDescriptions </span>-><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">final</span> List<EndpointDescription> collect =<span style="color: rgba(0, 0, 0, 1)"> endpointDescriptions.stream().collect(Collectors.toList()); </span><span style="color: rgba(0, 0, 255, 1)">final</span> Optional<EndpointDescription> first =<span style="color: rgba(0, 0, 0, 1)"> endpointDescriptions .stream() .findFirst(); System.out.println(first.get().getSecurityPolicyUri()); </span><span style="color: rgba(0, 0, 255, 1)">return</span> Optional.of(collect.get(0<span style="color: rgba(0, 0, 0, 1)">)); }, configBuilder </span>-><span style="color: rgba(0, 0, 0, 1)"> configBuilder .setApplicationName(LocalizedText.english(</span>"opc-ua client"<span style="color: rgba(0, 0, 0, 1)">)) .setApplicationUri(endPoint)
// .setIdentityProvider(new UsernameProvider(username, password))
.setIdentityProvider(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> AnonymousProvider()) .setRequestTimeout(uint(</span>5000<span style="color: rgba(0, 0, 0, 1)">)) .build() ); log.info(</span>"创建客户端:{} 成功!"<span style="color: rgba(0, 0, 0, 1)">, opcUaClient); } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 创建opc ua客户端(在项目启动的时候执行该方法) * </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> createClient(){ </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { Path securityTempDir </span>= Paths.get(System.getProperty("java.io.tmpdir"), "security"<span style="color: rgba(0, 0, 0, 1)">); Files.createDirectories(securityTempDir); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Files.exists(securityTempDir)) { </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Exception("没有创建安全目录: " +<span style="color: rgba(0, 0, 0, 1)"> securityTempDir); } log.info(</span>"安全目录: {}"<span style="color: rgba(0, 0, 0, 1)">, securityTempDir.toAbsolutePath()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载秘钥</span> KeyStoreLoader loader = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> KeyStoreLoader().load(securityTempDir); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">安全策略 None、Basic256、Basic128Rsa15、Basic256Sha256</span> SecurityPolicy securityPolicy =<span style="color: rgba(0, 0, 0, 1)"> SecurityPolicy.None; List</span><EndpointDescription><span style="color: rgba(0, 0, 0, 1)"> endpoints; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { endpoints </span>=<span style="color: rgba(0, 0, 0, 1)"> DiscoveryClient.getEndpoints(opcUaConfig.getEndpointUrl()).get(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Throwable ex) { String discoveryUrl </span>=<span style="color: rgba(0, 0, 0, 1)"> opcUaConfig.getEndpointUrl(); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!discoveryUrl.endsWith("/"<span style="color: rgba(0, 0, 0, 1)">)) { discoveryUrl </span>+= "/"<span style="color: rgba(0, 0, 0, 1)">; } discoveryUrl </span>+= "discovery"<span style="color: rgba(0, 0, 0, 1)">; log.info(</span>"开始连接 URL: {}"<span style="color: rgba(0, 0, 0, 1)">, discoveryUrl); endpoints </span>=<span style="color: rgba(0, 0, 0, 1)"> DiscoveryClient.getEndpoints(discoveryUrl).get(); } EndpointDescription endpoint </span>=<span style="color: rgba(0, 0, 0, 1)"> endpoints.stream() .filter(e </span>-><span style="color: rgba(0, 0, 0, 1)"> e.getEndpointUrl().equals(opcUaConfig.getEndpointUrl())) .findFirst().orElseThrow(() </span>-> <span style="color: rgba(0, 0, 255, 1)">new</span> Exception("没有节点返回"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">.filter(e -> e.getSecurityPolicyUri().equals(securityPolicy.getUri())) </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">.filter(opcUaConfig.endpointFilter()) </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">.findFirst() </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">.orElseThrow(() -> new Exception("没有连接上端点"));</span> log.info("使用端点: {} [{}/{}]"<span style="color: rgba(0, 0, 0, 1)">, endpoint.getEndpointUrl(), securityPolicy, endpoint.getSecurityMode()); OpcUaClientConfig config </span>=<span style="color: rgba(0, 0, 0, 1)"> OpcUaClientConfig.builder() .setApplicationName(LocalizedText.english(</span>"eclipse milo opc-ua client"<span style="color: rgba(0, 0, 0, 1)">)) .setApplicationUri(</span>"urn:eclipse:milo:examples:client"<span style="color: rgba(0, 0, 0, 1)">) .setCertificate(loader.getClientCertificate()) </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 证书</span>
.setKeyPair(loader.getClientKeyPair())
.setEndpoint(endpoint) </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据匿名验证和第三个用户名验证方式设置传入对象 AnonymousProvider(匿名方式)UsernameProvider(账户密码)</span> .setIdentityProvider(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> AnonymousProvider()) .setRequestTimeout(uint(</span><span style="color: rgba(0, 0, 0, 1)">)) .build(); opcUaClient </span>=<span style="color: rgba(0, 0, 0, 1)"> OpcUaClient.create(config); log.info(</span>"创建客户端:{} 成功!"<span style="color: rgba(0, 0, 0, 1)">, opcUaClient); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { log.error(</span>"创建客户端失败" +<span style="color: rgba(0, 0, 0, 1)"> e.getMessage()); } }
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/17927.html