open62541 使用账号密码认证示例

2024-08-28 08:20

本文主要是介绍open62541 使用账号密码认证示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、官方源码示例

源码参考
在这里插入图片描述

服务端官方示例:

/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */#include <open62541/plugin/accesscontrol_default.h>
#include <open62541/plugin/log_stdout.h>
#include <open62541/server.h>
#include <open62541/server_config_default.h>#include <signal.h>
#include <stdlib.h>static UA_Boolean
allowAddNode(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_AddNodesItem *item) {printf("Called allowAddNode\n");return UA_TRUE;
}static UA_Boolean
allowAddReference(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_AddReferencesItem *item) {printf("Called allowAddReference\n");return UA_TRUE;
}static UA_Boolean
allowDeleteNode(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_DeleteNodesItem *item) {printf("Called allowDeleteNode\n");return UA_FALSE; // Do not allow deletion from client
}static UA_Boolean
allowDeleteReference(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_DeleteReferencesItem *item) {printf("Called allowDeleteReference\n");return UA_TRUE;
}UA_Boolean running = true;
static void stopHandler(int sign) {UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");running = false;
}static UA_UsernamePasswordLogin logins[2] = {{UA_STRING_STATIC("peter"), UA_STRING_STATIC("peter123")},{UA_STRING_STATIC("paula"), UA_STRING_STATIC("paula123")}
};int main(void) {signal(SIGINT, stopHandler);signal(SIGTERM, stopHandler);UA_Server *server = UA_Server_new();UA_ServerConfig *config = UA_Server_getConfig(server);UA_ServerConfig_setDefault(config);/* Disable anonymous logins, enable two user/password logins */config->accessControl.clear(&config->accessControl);UA_StatusCode retval = UA_AccessControl_default(config, false, NULL,&config->securityPolicies[config->securityPoliciesSize-1].policyUri, 2, logins);if(retval != UA_STATUSCODE_GOOD)goto cleanup;/* Set accessControl functions for nodeManagement */config->accessControl.allowAddNode = allowAddNode;config->accessControl.allowAddReference = allowAddReference;config->accessControl.allowDeleteNode = allowDeleteNode;config->accessControl.allowDeleteReference = allowDeleteReference;retval = UA_Server_run(server, &running);cleanup:UA_Server_delete(server);return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}

二、程序测试

将官方的示例集成到工程种,使用源码的方式开发,需要修改头文件,修改后如下:

/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */#include "open62541.h"#include <signal.h>
#include <stdlib.h>//arm-linux-gcc open62541.c server3.c -o server3static UA_Boolean
allowAddNode(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_AddNodesItem *item) {printf("Called allowAddNode\n");return UA_TRUE;
}static UA_Boolean
allowAddReference(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_AddReferencesItem *item) {printf("Called allowAddReference\n");return UA_TRUE;
}static UA_Boolean
allowDeleteNode(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_DeleteNodesItem *item) {printf("Called allowDeleteNode\n");return UA_FALSE; // Do not allow deletion from client
}static UA_Boolean
allowDeleteReference(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_DeleteReferencesItem *item) {printf("Called allowDeleteReference\n");return UA_TRUE;
}UA_Boolean running = true;
static void stopHandler(int sign) {UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");running = false;
}static UA_UsernamePasswordLogin logins[2] = {{UA_STRING_STATIC("peter"), UA_STRING_STATIC("peter123")},{UA_STRING_STATIC("paula"), UA_STRING_STATIC("paula123")}
};int main(void) {signal(SIGINT, stopHandler);signal(SIGTERM, stopHandler);UA_Server *server = UA_Server_new();UA_ServerConfig *config = UA_Server_getConfig(server);UA_ServerConfig_setDefault(config);/* Disable anonymous logins, enable two user/password logins */config->accessControl.clear(&config->accessControl);UA_StatusCode retval = UA_AccessControl_default(config, false, NULL,&config->securityPolicies[config->securityPoliciesSize-1].policyUri, 2, logins);if(retval != UA_STATUSCODE_GOOD)goto cleanup;/* Set accessControl functions for nodeManagement */config->accessControl.allowAddNode = allowAddNode;config->accessControl.allowAddReference = allowAddReference;config->accessControl.allowDeleteNode = allowDeleteNode;config->accessControl.allowDeleteReference = allowDeleteReference;retval = UA_Server_run(server, &running);cleanup:UA_Server_delete(server);return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}

文件结构如下:
在这里插入图片描述

交叉编译指令:

arm-linux-gcc open62541.c server3.c -o server3

将编译后的可执行程序下载到 Arm Linux 开发板,打开客户端进行测试。

无账号密码直接连接,提示失败,符合预期
在这里插入图片描述
在这里插入图片描述
改为账号密码登录,正常连接,获取数据,符合预期,如下:
在这里插入图片描述
在这里插入图片描述

这篇关于open62541 使用账号密码认证示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1114231

相关文章

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

最新Spring Security的基于内存用户认证方式

《最新SpringSecurity的基于内存用户认证方式》本文讲解SpringSecurity内存认证配置,适用于开发、测试等场景,通过代码创建用户及权限管理,支持密码加密,虽简单但不持久化,生产环... 目录1. 前言2. 因何选择内存认证?3. 基础配置实战❶ 创建Spring Security配置文件