Pulsar集群和PulsarManager添加JWT认证

本文将介绍Pulsar集群添加JWT认证及Pulsar的管理界面pulsar-manager的相关配置,加密方式采用的非对称加密,Pulsar的版本为v2.9.1,pulsar-manger的版本为v0.2.0。

一、Pulsar添加JWT认证

Pulsar官网对这块的描述很详细,可以进行参考。

1. 生成秘钥对

bin/pulsar tokens create-key-pair --output-private-key jwt-private.key --output-public-key jwt-public.key

2. 生成用于超级管理员的 token

bin/pulsar tokens create --private-key jwt-private.key --subject admin

以下为管理员的示例token

eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.xxxxxxxxxxxxxA

3. 生成给测试用户的 token

可通过--expiry-time设置token的有效期,1y有效期为1年。

bin/pulsar tokens create --private-key jwt-private.key --subject test-user --expiry-time 1y

以下为测试用户的示例token

eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.xxxxxxxxxxxxxB

4.配置集群broker的broker.conf

broker的配置和官网描述的有所不同,注意不要配置错误,否则无法使用。
集群中的每个节点的broker的配置都要修改,并且将public.key文件分发到各个节点。
brokerClientAuthenticationParameters配置的为超级管理员的token

# 开启认证
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":"eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.xxxxxxxxxxxxxA"}
# 使用 tokenPublicKey 的公钥文件位置
tokenPublicKey=/usr/local/apache-pulsar-2.9.1/jwt-public.key

5. 重启 broker

bin/pulsar-daemon stop broker
bin/pulsar-daemon start broker

二、分配权限并验证

1.给普通用户赋生产和消费权限

--auth-params为超级管理员的token

bin/pulsar-admin \
--admin-url "http://127.0.0.1:8080/" \
--auth-params {"token":"eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.xxxxxxxxxxxxxA"} \
--auth-plugin "org.apache.pulsar.client.impl.auth.AuthenticationToken" \
namespaces grant-permission public/default --role test-user --actions produce,consume

2.验证token

验证超级管理员的token

bin/pulsar tokens validate -pk  /usr/local/apache-pulsar-2.9.1/jwt-public.key -i "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.xxxxxxxxxxxxxA"

验证普通用户的token

bin/pulsar tokens validate -pk  /usr/local/apache-pulsar-2.9.1/jwt-public.key -i "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.xxxxxxxxxxxxxB"

3.验证生产数据

验证超级管理员生产数据

bin/pulsar-client \
--url "pulsar://127.0.0.1:6650" \
--auth-plugin "org.apache.pulsar.client.impl.auth.AuthenticationToken" \
--auth-params {"token":"eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.xxxxxxxxxxxxxA"} \
produce public/default/test-token -m "hello pulsar" -n 10

验证普通用户生产数据

bin/pulsar-client \
--url "pulsar://127.0.0.1:6650" \
--auth-plugin "org.apache.pulsar.client.impl.auth.AuthenticationToken" \
--auth-params {"token":"eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.xxxxxxxxxxxxxB"} \
produce public/default/test-token -m "hello pulsar" -n 10

4.普通用户回收权限

--auth-params为超级管理员的token

bin/pulsar-admin \
--admin-url "http://127.0.0.1:8080/" \
--auth-params {"token":"eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.xxxxxxxxxxxxxA"} \
--auth-plugin "org.apache.pulsar.client.impl.auth.AuthenticationToken" \
namespaces revoke-permission public/default --role test-user 

5.修改client配置

修改集群中所有节点的client.conf,这样在pulsar集群中执行pulsar-client命令时,就不用带许多参数了。修改完成之后需要重启broker
authParams为超级管理员的token

webServiceUrl=http://localhost:8080/
brokerServiceUrl=pulsar://localhost:6650/
authPlugin=org.apache.pulsar.client.impl.auth.AuthenticationToken
authParams=token:eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.xxxxxxxxxxxxxA

6.查看命名空间权限配置

bin/pulsar-admin namespaces permissions public/default

三、修改pulsar-manger的配置

在给Pulsar添加了认证相关的配置后,发现pulsar-manger的管理页面是无法使用的,需要我们也进行token相关的配置。
修改pulsar-manager目录下的application.properties文件中jwt相关的配置。
backend.jwt.token为超级管理员的token

backend.jwt.token=eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.xxxxxxxxxxxxxA
jwt.broker.token.mode=PRIVATE
jwt.broker.public.key=/usr/local/apache-pulsar-2.9.1/jwt-public.key
jwt.broker.private.key=/usr/local/apache-pulsar-2.9.1/jwt-private.key

修改完配置后,重启pulsar-manger即可。

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 环境准备 - Pulsar集群 - Java 8 - Spring Boot 2.x - Pulsar Java Client 2. 添加依赖 在`pom.xml`中添加以下依赖: ```xml <dependency> <groupId>org.apache.pulsar</groupId> <artifactId>pulsar-client</artifactId> <version>${pulsar.version}</version> </dependency> <dependency> <groupId>org.apache.pulsar</groupId> <artifactId>pulsar-spring-boot-starter</artifactId> <version>${pulsar.version}</version> </dependency> ``` 其中`${pulsar.version}`为Pulsar的版本号。 3. 配置Pulsar连接信息 在`application.yml`中添加以下配置: ```yaml pulsar: serviceUrl: pulsar://localhost:6650 authPluginClassName: org.apache.pulsar.client.impl.auth.AuthenticationToken authParams: token:eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c ``` 其中`serviceUrl`为Pulsar集群连接地址,`authPluginClassName`和`authParams`为认证信息,可以根据实际情况进行修改。 4. 发送消息 在Spring Boot中可以使用`PulsarTemplate`来发送消息,示例代码如下: ```java import org.apache.pulsar.client.api.MessageId; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.concurrent.ListenableFutureCallback; import org.springframework.util.concurrent.ListenableFutureTask; import org.springframework.util.concurrent.ListenableFutureTaskAdapter; @Service public class PulsarProducerService { @Autowired private PulsarTemplate<String> pulsarTemplate; public void sendMessage(String topic, String message) { ListenableFutureTask<MessageId> future = new ListenableFutureTaskAdapter<>(() -> pulsarTemplate.send(topic, message)); future.addCallback(new ListenableFutureCallback<MessageId>() { @Override public void onFailure(Throwable ex) { System.out.println("Send message failed: " + ex.getMessage()); } @Override public void onSuccess(MessageId result) { System.out.println("Send message success: " + result); } }); future.run(); } } ``` 在上述代码中,`PulsarTemplate`使用泛型`<String>`,表示发送的消息为字符串类型。`sendMessage`方法接收两个参数,分别为消息的主题和内容。发送消息的过程中,使用`ListenableFuture`来处理异步回调。在回调函数中,可以根据发送结果进行相应的处理。 5. 接收消息 在Spring Boot中可以使用`PulsarConsumerFactory`来创建消费者,示例代码如下: ```java import org.apache.pulsar.client.api.Consumer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PulsarConsumerService { @Autowired private PulsarConsumerFactory pulsarConsumerFactory; public void receiveMessage(String topic, ConsumerMessageHandler<String> handler) throws Exception { Consumer<String> consumer = pulsarConsumerFactory.createConsumer(topic, "my-subscription", String.class); consumer.subscribe(); while (true) { String message = consumer.receive().getValue(); handler.handle(message); consumer.acknowledgeAsync(consumer.getLastMessageId()); } } } ``` 在上述代码中,`PulsarConsumerFactory`使用泛型`<String>`,表示接收消息的类型为字符串。`receiveMessage`方法接收两个参数,分别为消息的主题和消息处理器。在接收消息的过程中,使用`while`循环不断接收消息,并交给消息处理器进行处理。处理完成后,使用`acknowledgeAsync`方法对消息进行确认。 6. 运行测试 在完成上述步骤后,可以编写测试代码来测试消息的发送和接收。示例代码如下: ```java import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class PulsarApplicationTests { @Autowired private PulsarProducerService pulsarProducerService; @Autowired private PulsarConsumerService pulsarConsumerService; @Test void sendMessage() { pulsarProducerService.sendMessage("my-topic", "Hello, Pulsar!"); } @Test void receiveMessage() throws Exception { pulsarConsumerService.receiveMessage("my-topic", message -> { System.out.println("Received message: " + message); }); } } ``` 在运行测试代码后,可以在控制台中看到消息发送和接收的相关信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值