pulsar的客户端权限控制功能(一)

4 篇文章 0 订阅

1. 概述

最近在研究pulsar的权限验证部分,权限验证包含两部分:

  • 客户端连接
  • 客户端的访问控制

pulsar提供了authentication和authorization两种方式实现上述两个功能,每一种方式提供了接口。

2. 版本

pulsar 2.8.0

3. 客户端连接验证

默认情况下,pulsar是不会开启连接验证的,即客户端到broker之间、broker到broker之间的访问都没有任何限制。但是在线上环境中,对于权限的控制往往是很重要的。

3.1 在broker.conf文件中开启客户端连接认证
# Enable authentication
authenticationEnabled=true

# Authentication provider name list, which is comma separated list of class names
# 可以提供N个处理验证的处理类,然后broker接收到客户端连接后就会调用此类的方法进行处理
authenticationProviders=auth.server.VVAuthenticationProvider

# Interval of time for checking for expired authentication credentials
authenticationRefreshCheckSeconds=60

# Authentication settings of the broker itself. Used when the broker connects to other brokers,
# either in same or other clusters
brokerClientTlsEnabled=false
# 这里是broker之间连接时,broker客户端用到的处理类。通常可以和客户端的认证处理类一样。
# 正式项目中,需要在broker端判断是哪一种连接,分别做好权限认证。
brokerClientAuthenticationPlugin=auth.client.VVAuthentication
brokerClientAuthenticationParameters=
brokerClientTrustCertsFilePath=
3.2 实现连接验证处理类

权限验证分为客户端和服务端,服务端即broker,客户端即我们自己编写的producer或者consumer。服务端需要实现org.apache.pulsar.broker.authentication.AuthenticationProvider接口,客户端需要实现org.apache.pulsar.client.api.Authentication
下面分别给出服务端和客户端的代码。

服务端认证代码
package auth.server;

import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.authentication.AuthenticationDataSource;
import org.apache.pulsar.broker.authentication.AuthenticationProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.naming.AuthenticationException;
import java.io.IOException;
import java.util.Set;

/**
 * @author cc
 * @function
 * @date 2021/7/27 14:19
 */
public class VVAuthenticationProvider implements AuthenticationProvider {
    private static final Logger log = LoggerFactory.getLogger(VVAuthenticationProvider.class);
    private static final String methodName = "vv_auth_v2";

    private String header = "vv_auth";

    @Override
    public void initialize(ServiceConfiguration config) throws IOException {
        log.info(methodName + " initialize");
        if (config == null) {
            return;
        }

        Set<String> superRoles = config.getSuperUserRoles();
        if (superRoles == null) {
            return;
        }
        for (String role : superRoles) {
            log.info(methodName + " initialize " + role);
        }
    }

    @Override
    public String getAuthMethodName() {
        log.info(methodName + " getAuthMethodName");
        return methodName;
    }

    @Override
    public String authenticate(AuthenticationDataSource authData) throws AuthenticationException {
        log.info(methodName + " authenticate");

        String roleToken = "unknown";
        if (authData.hasDataFromCommand()) {
            roleToken = authData.getCommandData();
        } else if (authData.hasDataFromHttp()) {
            roleToken = authData.getHttpHeader(header);
        } else {
            throw new AuthenticationException("Authentication data source does not have a role token");
        }

        log.info(methodName + " authenticate " + roleToken);
        return roleToken;
    }

    @Override
    public void close() throws IOException {
        log.info(methodName + " close");
    }
}


客户端连接认证代码
package auth.client;

import org.apache.pulsar.client.api.Authentication;
import org.apache.pulsar.client.api.AuthenticationDataProvider;
import org.apache.pulsar.client.api.PulsarClientException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

/**
 * @author cc
 * @function
 * @date 2021/7/27 14:00
 */
public class VVAuthentication implements Authentication {
    private static final Logger log = LoggerFactory.getLogger(VVAuthentication.class);
    private static final String methodName = "vv_auth_v2";

    @Override
    public String getAuthMethodName() {
        log.info(methodName + " getAuthMethodName");
        return methodName;
    }

    @Override
    public AuthenticationDataProvider getAuthData() throws PulsarClientException {
        log.info(methodName + " getAuthData");
        return new VVAuthenticationDataProvider();
    }

    @Override
    public void configure(Map<String, String> authParams) {
        log.info(methodName + " configure");
        if (authParams == null) {
            return;
        }

        authParams.forEach((key, value) -> {
            log.info(methodName + " configure " + key + "=" + value);
        });
    }

    @Override
    public void start() throws PulsarClientException {
        log.info(methodName + " start");
    }

    @Override
    public void close() throws IOException {
        log.info(methodName + " close");
    }
}
package auth.client;

import org.apache.pulsar.client.api.AuthenticationDataProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author cc
 * @function
 * @date 2021/7/27 14:08
 */
public class VVAuthenticationDataProvider implements AuthenticationDataProvider {
    private static final Logger log = LoggerFactory.getLogger(VVAuthenticationDataProvider.class);
    private static final String methodName = "vv_auth_v2";

    private String header = "vv_auth";
    private String token = "vv-role";

    @Override
    public boolean hasDataForHttp() {
        log.info(methodName + " hasDataForHttp");
        return true;
    }

    @Override
    public Set<Map.Entry<String, String>> getHttpHeaders() throws Exception {
        log.info(methodName + " getHttpHeaders");
        Map<String, String> headers = new HashMap<>();
        headers.put(header, token);
        return headers.entrySet();

    }

    @Override
    public boolean hasDataFromCommand() {
        log.info(methodName + " hasDataFromCommand");
        return true;
    }

    @Override
    public String getCommandData() {
        log.info(methodName + " getCommandData");
        return token;
    }
}

编写好代码后用maven打包,然后放到pulsar的lib下,重启broker组件即可。

4. 测试

package auth;

import auth.client.VVAuthentication;
import org.apache.pulsar.client.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author cc
 * @function
 * @date 2021/7/19 10:47
 */
public class AuthTest {
    private static final Logger log = LoggerFactory.getLogger(AuthTest.class);

    public static void main(String[] args) throws Exception {
        AuthTest main = new AuthTest();
        main.run();
    }

    String pulsarUrl = "pulsar://x.x.x.x:6650";
    String topic = "persistent://tenant_vv/ns1/auth_test";

    Authentication authentication = new VVAuthentication();

    void run() throws Exception {
        PulsarClient client = PulsarClient.builder()
                .authentication(authentication)
                .serviceUrl(pulsarUrl)
                .build();
        send(client);
        consume(client);

        System.out.println("connect successed ");

        client.close();
    }

    void consume(PulsarClient client) throws Exception {
        Consumer consumer = client.newConsumer()
                .topic(topic)
                .subscriptionName("consumer-test")
                .subscribe();

        while (true) {
            Message m = consumer.receive();
            if (m != null) {
                log.info("recv " + new String(m.getData()));
                consumer.acknowledge(m);
            } else {
                break;
            }
        }
    }

    void send(PulsarClient client) throws Exception {
        Producer p = client.newProducer()
                .topic(topic)
                .create();

        for (int i=0; i<10; i++) {
            p.newMessage().key("aaa").value(("hello " + i).getBytes()).send();
            log.info("send " + i);
            Thread.sleep(1000);
        }
        p.flush();
        p.close();
        System.out.println("send done");
    }

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
SpringBoot整合Pulsar是一种将Pulsar客户端与SpringBoot框架结合使用的方法。首先,需要在项目的依赖中引入Pulsar客户端的依赖项,如下所示: ```xml <dependency> <groupId>org.apache.pulsar</groupId> <artifactId>pulsar-client</artifactId> <version>2.9.1</version> </dependency> ``` 接下来,在项目的配置文件中添加Pulsar相关的配置,包括Pulsar的地址、主题和消费者组等信息。例如: ```properties # Pulsar地址 pulsar.url=pulsar://192.168.59.155:6650 # 主题 pulsar.topic=testTopic # 消费者组 pulsar.subscription=topicGroup ``` 然后,可以创建Pulsar的Client对象,用于与Pulsar进行交互。 关于Pulsar的部署命令,可以使用以下命令进行部署: ```shell docker run -it -p 6650:6650 -p 8080:8080 --mount source=pulsardata,target=/pulsar/data --mount source=pulsarconf,target=/pulsar/conf apache/pulsar:2.9.1 bin/pulsar standalone ``` 以上是SpringBoot整合Pulsar的基本步骤和命令。通过这种方式,可以在SpringBoot项目中使用Pulsar进行消息传递和处理。 #### 引用[.reference_title] - *1* *2* *3* [Spring Boot 整合分布式消息平台 Pulsar](https://blog.csdn.net/m0_73311735/article/details/131532850)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值