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 "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoicmVtb3RlX3dvcmtlciJ9.wAzG0zeHPYBflP4PhipUh0W8pvPLCbOQ2M4NFNTOSg" 然后请求只通过正确的令牌。

如何禁用匿名执行请求?

4

1 回答 1

1

当您不使用 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 无权访问 mybd,如果您在 jwt 令牌中添加“角色”rest_api,则使用令牌的所有请求都应该工作,如 postgrest https://postgrest.org/en/stable/tutorials上的文档中所述/tut1.html#tut1

于 2022-01-14T14:55:29.843 回答