Pulsar 基于 JSON Web Tokens (RFC-7519) 提供了一个标准的 JWT 客户端,可以生成密钥、制作 token 和检测密钥有效性等功能。
bin/pulsar tokens create-secret-key --output my-secret.key
bin/pulsar tokens create-secret-key :生成一个对称密钥,保存到文件 my-secret.key 中。后续将使用这个密钥文件来创建 token。我们将超级管理员命名为 admin(对应 pulsar 认证概念里对 role)。这里不指定超时时间( --expiry-time),则默认将不过期。
bin/pulsar tokens create --secret-key my-secret.key --subject admin
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.Ra9pwWHTWjB67v5GkVuuDMqXWwfeTJuwflyvmhxYk_c
bin/pulsar tokens create --secret-key my-secret.key --subject test-user --expiry-time 7d
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIiLCJleHAiOjE2NTY3NzYwOTh9.awbp6DreQwUyV8UCkYyOGXCFbfo4ZoV-dofXYTnFXO8
# 开启认证
authenticationEnabled=true
# 认证提供者
authenticationProviders=org.apache.pulsar.broker.authentication.AuthenticationProviderToken
# 开启授权
authorizationEnabled=true
# 超级管理员
superUserRoles=admin
# broker Client 使用等认证插件
brokerClientAuthenticationPlugin=org.apache.pulsar.client.impl.auth.AuthenticationToken
# broker Client 通讯使用的 token(需要 admin role)
brokerClientAuthenticationParameters={"token":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.Ra9pwWHTWjB67v5GkVuuDMqXWwfeTJuwflyvmhxYk_c"}
# 使用 secretKey 的密钥文件位置(file://开头)
tokenSecretKey=file:///Users/futeng/workspace/github/futeng/pulsar-pseudo-cluster/pulsar-1/my-secret.key
bin/pulsar-daemon stop broker
bin/pulsar-daemon start broker
bin/pulsar tokens validate -sk /Users/futeng/workspace/github/futeng/pulsar-pseudo-cluster/pulsar-1/my-secret.key -i "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.Ra9pwWHTWjB67v5GkVuuDMqXWwfeTJuwflyvmhxYk_c"
# 打印:{sub=admin}
# produce as admin role
bin/pulsar-client \\\\
--url "pulsar://127.0.0.1:6650" \\\\
--auth-plugin "org.apache.pulsar.client.impl.auth.AuthenticationToken" \\\\
--auth-params {"token":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.Ra9pwWHTWjB67v5GkVuuDMqXWwfeTJuwflyvmhxYk_c"} \\\\
produce public/default/test -m "hello pulsar" -n 10