示例:配置 REST API
此示例演示如何在 Junos OS 设备上配置 REST API。
要求
需要运行 Junos OS 14.2 或更高版本的路由、交换或安全设备。
概述
此示例在瞻博网络 M10i 多业务边缘路由器上配置 REST API。该示例配置 HTTP 和 HTTPS 访问,同时使用 lighttpd 和 juise 跟踪。
配置
CLI 快速配置
要快速配置此示例,请复制以下命令,将其粘贴到文本文件中,删除所有换行符,更改详细信息,以便与网络配置匹配,将命令复制并粘贴到层 [edit] 级的 CLI 中,然后从配置模式进入 commit 。
set system services rest control allowed-sources [192.0.2.0 198.51.100.0] set system services rest control connection-limit 100 set system services rest http port 3000 set system services rest http addresses [203.0.113.0 203.0.113.1] set system services rest https port 3443 set system services rest https addresses [203.0.113.2 203.0.113.3] set system services rest https server-certificate testcert set system services rest https cipher-list rsa-with-3des-ede-cbc-sha set system services rest https mutual-authentication certificate-authority testca set system services rest traceoptions flag all set system services rest enable-explorer
配置 REST API
分步过程
要配置 REST API,请执行以下作:
指定传入 HTTP 和 HTTPS 连接允许的 IP 地址。
[edit] user@R1# set system services rest control allowed-sources [192.0.2.0 198.51.100.0]
指定允许通过 HTTP 和 HTTPS 进行的最大连接数。
[edit] user@R1# set system services rest control connection-limit 100
为传入的 HTTP 连接设置 TCP 端口。
[edit] user@R1# set system services rest http port 3000
设置服务器侦听传入 HTTP 连接的地址。
[edit] user@R1# set system services rest http addresses [203.0.113.0 203.0.113.1]
设置传入 HTTPS 连接的 TCP 端口。
[edit] user@R1# set system services rest https port 3443
设置服务器侦听传入 HTTPS 连接的地址。
[edit] user@R1# set system services rest https addresses [203.0.113.2 203.0.113.3]
设置服务器证书。
[edit] user@R1# set system services rest https server-certificate testcert
配置服务器可用于执行加密和解密功能的密码集。
[edit] user@R1# set system services rest https cipher-list rsa-with-3des-ede-cbc-sha
(选答)使用证书在服务器和客户端上设置 TLS 相互身份验证。
[edit] user@R1# set system services rest https mutual-authentication certificate-authority testca
(选答)为 lighttpd 和/或 juise 配置跟踪选项。
[edit] user@R1# set system services rest traceoptions flag all
(选答)启用 REST API 资源管理器。
[edit] user@R1# set system services rest enable-explorer
提交配置。
[edit] user@R1# commit and-quit
结果
system {
services {
rest {
control {
allowed-sources [ 192.0.2.0 198.51.100.0 ];
connection-limit 100;
}
enable-explorer;
http {
addresses [ 203.0.113.0 203.0.113.1 ];
port 3000;
}
https {
port 3443;
addresses [ 203.0.113.2 203.0.113.3 ];
server-certificate testcert;
cipher-list rsa-with-3des-ede-cbc-sha;
mutual-authentication {
certificate-authority testca;
}
}
traceoptions {
flag all;
}
}
}
}
当为 REST API 启用 traceoptions 时,日志存储在以下位置:/var/chroot/rest-api/var/log/lighttpd。
此位置适用于运行Junos OS ACX7100等平台。跟踪日志可以帮助排查与 REST API 相关的问题。
验证
验证 REST API 配置
目的
确认 REST API 配置在设备上工作正常。
行动
发出 show configuration system services rest 作模式命令,显示 REST API 配置。
user@R1> show configuration system services rest
http {
port 3000;
addresses [ 203.0.113.0 203.0.113.1 ];
}
https {
port 3443;
addresses [ 203.0.113.2 203.0.113.3 ];
server-certificate testcert;
cipher-list rsa-with-3des-ede-cbc-sha;
mutual-authentication {
certificate-authority testca;
}
}
control {
allowed-sources [ 192.0.2.0 198.51.100.0 ];
connection-limit 100;
}
traceoptions {
flag all;
}
enable-explorer;
意义
此示例在瞻博网络 M10i 多业务边缘路由器上配置了 HTTP 和 HTTPS 访问。对于 HTTP 访问,设备侦听端口 3000,并允许来自 IP 地址 192.0.2.0、198.51.100.0、203.0.113.0 和 203.0.113.1 的流量。为了实现更安全的连接,HTTPS 访问使用端口 3443 配置了相互身份验证,并允许的 IP 地址为 192.0.2.0、198.51.100.0、203.0.113.2 和 203.0.113.3。已为 HTTP 和 HTTPS 配置连接限制为 100,并启用了 juise 和 lighttpd 跟踪。默认情况下,REST API 资源管理器处于禁用状态(请参阅 示例:使用 REST API 资源管理器)。