您当前的位置:首页 > 学海无涯 > 信息化管理网站首页信息化管理
项目接口API规范
发布时间:2021-01-19作者:♂逸風★淩軒
前后端跨域问题,从我工作以来就一直是很多公司无可避免的问题,因为项目组分割,各自独立创建,一个接口一个域名的比比皆是,然后访问就跨域问题层出不穷,甚至于不安全的直接在Nginx或者业务端代码设定允许跨域为*......
基于安全和项目的规范化管理,所以我在我所在公司都实施以下规则以实现避免跨域和对接口访问限定
一、项目层级目录规则:
1、 项目接口解释:
(1)BaseUrl:最低分为两类,用户侧和管理侧:用户侧域名用于限定公共开放的域名访问地址,而管理侧用于限定公司组织内部成员后台管理使用的访问域名地址,在基于多项目管理非一致授权的的情况下,会增加其他侧的域名
(2) 类别:/api为后端REST接口
/web为各自前端Front页面,在确定/根页面无冲突规划的情况下,可以使用根页面,非强制必须二级web
/rpc为后端其他特定类的接口
/socket为后端socket类接口
(3) 接口:对应项目的分类接口网关
(4)版本:接口的版本号
2、Nginx层配置管理:
(1) /api /rpc 作为公共服务vhost,定义pro.public.conf
pro.public.conf
location /api/id4 {
proxy_pass http://x.x.x.3:6501;
}
location /api/terminalr/TerminalHub {
proxy_pass http://x.x.x.4:7211;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_read_timeout 120s;
}(2)项目侧引用
站点配置
server{
listen 443 ssl;
server_name front.example.com;
ssl_certificate /web/certs/front.example.com.pem;
ssl_certificate_key /web/certs/front.example.com.key;
location / {
proxy_pass http://x.x.x.1:80;
}
location /web {
proxy_pass http://x.x.x.11:80;
}
include pro.public.conf;
}
server{
listen 443 ssl;
server_name admin.example.com;
ssl_certificate /web/certs/admin.example.com.pem;
ssl_certificate_key /web/certs/admin.example.com.key;
location / {
proxy_pass http://x.x.x.2:80;
}
location /web {
proxy_pass http://x.x.x.22:80;
}
include pro.public.conf;
}3、安全限定管理
(1) 对接口引入来源访问白名单机制greyip.conf:
greyip.conf
#服务器自身 allow 172.16.0.0/16; allow 10.10.0.0/16; #公司 allow 116.25.239.0/254; #拒绝所有 deny all;
然后对应接口二级目录引入例如:
location /api/terminalr {
include greyip.conf;
proxy_pass http://x.x.x.5:7211;
}(2)对接口引入限定域访问机制whitedomain.conf:
whitedomain.conf
if ( $host != 'admin.zhigujinyun.com' ){
return 403;
}location /api/terminalr {
include whitedomain.conf;
proxy_pass http://x.x.x.5:7211;
}
(3)代码侧引入(慎用)
"AllowedHosts": "admin.example.com"关键字词:跨域,前后端,nginx

