如何禁用 postgrest 查询的匿名执行

问题描述 投票:0回答:1

Postgrest 是 postgreeSQL 数据库的 REST API。 我正在使用 postgrest v9.0.0 具有以下配置:

db-uri = "postgres://remote_worker:1HyiYai@localhost:5432/myDb"
db-schema = "public"
db-anon-role = "remote_worker"
jwt-secret = "1HyiYaiMTAAJ1pluZnBtAMnTUH19E3gg"
db-pool = 10
db-pool-timeout = 10
server-host = "!4"
server-port = 3000

我假设如果我在配置中输入jwt-secret参数,它会自动导致只有jwt授权才有效。

但是,我可以未经授权提出请求 即使只是在浏览器中输入 -> http://localhost:3000/myTable ?Id=eq.2。 或者在命令行中->curl http://localhost:3000/Kits

同时当我向 授权参数例如curl http://localhost:3000/Kits -H“授权:Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoicmVtb3RlX3dvcmtlciJ9.wAzG0zeHPYBflP4PhipUh0W8pvPLCbOQ2M4NFNTOSgc” 那么仅当令牌正确时请求才会通过。

如何禁用请求的匿名执行?

postgresql rest postgrest
1个回答
4
投票

当您不使用 jwt 令牌时,使用的角色始终是 db-anon-role。 因此,根据您的数据库设置,此角色可以具有对架构的读取访问权限并显示查询的响应。

如果您只想拥有授权用户,则需要在数据库上创建一个新用户,删除 db-anon 用户模式上的权限,并只允许新用户使用该模式。

以下 postgres 设置应该有效:

create role web_anon nologin;
create role authenticator;
grant web_anon to authenticator;

create role rest_api nologin;
grant rest_api to authenticator;

create database "mydb" with owner rest_api

postgrest 会议:

db-uri = "postgres://authenticator:omni123@localhost:5432/mydb"
db-schema = "public"
db-anon-role = "web_anon"

默认用户 web_anon 无权访问 mydb 如果您按照 postgrest 文档中所述在 jwt 令牌中添加“角色”rest_api,则所有带有令牌的请求都应该有效 https://postgrest.org/en/stable /tutorials/tut1.html#tut1

© www.soinside.com 2019 - 2024. All rights reserved.