尽管出现在受信任的包列表中,但该类不在受信任的包中 [英] The class is not in the trusted packages although appears in the list of trusted packages

查看:26
本文介绍了尽管出现在受信任的包列表中,但该类不在受信任的包中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在没有任何特殊设置的情况下在 2 个不同的 Spring Boot 应用程序之间实现简单的 Kafka 通信,该应用程序只有一个 kafkalistener.我给消费者的 yml 如下:

I am trying to implement a simple Kafka communication between 2 different Spring Boot applications with out any special settings, this application has only one kafkalistener. My yml for the consumer is the following:

spring:
  kafka:
    bootstrap-servers: ip_here
    topic:
      json: topic_here
    consumer:
      group-id: group_id
      auto-offset-reset: earliest
      value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
      properties:
        spring:
          json:
            trusted:
              packages: 'com.example.kw.dtos.Classdata'

我收到的错误如下:

Caused by: java.lang.IllegalArgumentException: 类'com.example.kw.dtos.Classdata' 不在受信任的包中:[java.util、java.lang、com.example.kw.dtos.Classdata].如果你相信这个类可以安全地反序列化,请提供它的名称.如果序列化仅由受信任的来源完成,您也可以启用相信所有人 (*).

Caused by: java.lang.IllegalArgumentException: The class 'com.example.kw.dtos.Classdata' is not in the trusted packages: [java.util, java.lang, com.example.kw.dtos.Classdata]. If you believe this class is safe to deserialize, please provide its name. If the serialization is only done by a trusted source, you can also enable trust all (*).

该软件包在受信任的软件包中,但出了点问题.

The package is in the trusted packages but something is wrong.

我的工厂类:

@Configuration
@EnableKafka
public class MsgListener {

    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;

    @Bean
    public Map<String, Object> consumerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "json");
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        props.put(JsonDeserializer.TRUSTED_PACKAGES, "com.example.kw.dtos.Classdata");
        return props;
    }

    @Bean
    public ConsumerFactory<String, Classdata> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(
                consumerConfigs(),
                new StringDeserializer(),
                new JsonDeserializer<>(Classdata.class));
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, Classdata> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, Classdata> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }
}

推荐答案

应该只是包 com.example.kw.dtos

String packageName = ClassUtils.getPackageName(requestedType).replaceFirst("\\[L", "");
for (String trustedPackage : this.trustedPackages) {
    if (packageName.equals(trustedPackage)) {
        return true;
    }
}

这篇关于尽管出现在受信任的包列表中,但该类不在受信任的包中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆