引用的路径:
static {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
System.setProperty("java.security.auth.login.config",
loader.getResource("").getPath() + File.separator + "com/shenyue/collectionpro/kafka_client_jaas.conf");
}
文件内容:
KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="*****"
password="****";
};
2021-08-13 09:03:00.786 INFO 4576 --- [ scheduling-1] o.a.k.clients.producer.KafkaProducer : [Producer clientId=producer-4] Closing the Kafka producer with timeoutMillis = 0 ms.
2021-08-13 09:03:00.786 ERROR 4576 --- [ scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task
org.apache.kafka.common.KafkaException: Failed to construct kafka producer
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:434) ~[kafka-clients-2.5.1.jar!/:na]
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:287) ~[kafka-clients-2.5.1.jar!/:na]
at org.springframework.kafka.core.DefaultKafkaProducerFactory.createRawProducer(DefaultKafkaProducerFactory.java:668) ~[spring-kafka-2.5.7.RELEASE.jar!/:2.5.7.RELEASE]
at org.springframework.kafka.core.DefaultKafkaProducerFactory.createKafkaProducer(DefaultKafkaProducerFactory.java:542) ~[spring-kafka-2.5.7.RELEASE.jar!/:2.5.7.RELEASE]
at org.springframework.kafka.core.DefaultKafkaProducerFactory.doCreateProducer(DefaultKafkaProducerFactory.java:519) ~[spring-kafka-2.5.7.RELEASE.jar!/:2.5.7.RELEASE]
at org.springframework.kafka.core.DefaultKafkaProducerFactory.createProducer(DefaultKafkaProducerFactory.java:480) ~[spring-kafka-2.5.7.RELEASE.jar!/:2.5.7.RELEASE]
at org.springframework.kafka.core.DefaultKafkaProducerFactory.createProducer(DefaultKafkaProducerFactory.java:474) ~[spring-kafka-2.5.7.RELEASE.jar!/:2.5.7.RELEASE]
at org.springframework.kafka.core.KafkaTemplate.getTheProducer(KafkaTemplate.java:665) ~[spring-kafka-2.5.7.RELEASE.jar!/:2.5.7.RELEASE]
at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:551) ~[spring-kafka-2.5.7.RELEASE.jar!/:2.5.7.RELEASE]
at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:363) ~[spring-kafka-2.5.7.RELEASE.jar!/:2.5.7.RELEASE]
at com.shenyue.collectionpro.mdb.Business.AccessServerBusiness.business(AccessServerBusiness.java:56) ~[classes!/:2.4.0]
at com.shenyue.collectionpro.mdb.convert.DBConvert.convertType(DBConvert.java:158) ~[classes!/:2.4.0]
at com.shenyue.collectionpro.mdb.service.impl.TestServicelmpl.testMedthod(TestServicelmpl.java:22) ~[classes!/:2.4.0]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_191]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.2.10.RELEASE.jar!/:5.2.10.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.10.RELEASE.jar!/:5.2.10.RELEASE]
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) [spring-context-5.2.10.RELEASE.jar!/:5.2.10.RELEASE]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [na:1.8.0_191]
at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_191]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source) [na:1.8.0_191]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) [na:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_191]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_191]
Caused by: java.lang.SecurityException: java.io.IOException: 配置错误: 没有此文件或目录
at sun.security.provider.ConfigFile$Spi.<init>(Unknown Source) ~[na:1.8.0_191]
at sun.security.provider.ConfigFile.<init>(Unknown Source) ~[na:1.8.0_191]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_191]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_191]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_191]
at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.8.0_191]
at java.lang.Class.newInstance(Unknown Source) ~[na:1.8.0_191]
at javax.security.auth.login.Configuration$2.run(Unknown Source) ~[na:1.8.0_191]
at javax.security.auth.login.Configuration$2.run(Unknown Source) ~[na:1.8.0_191]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_191]
at javax.security.auth.login.Configuration.getConfiguration(Unknown Source) ~[na:1.8.0_191]
at org.apache.kafka.common.security.JaasContext.defaultContext(JaasContext.java:114) ~[kafka-clients-2.5.1.jar!/:na]
at org.apache.kafka.common.security.JaasContext.load(JaasContext.java:98) ~[kafka-clients-2.5.1.jar!/:na]
at org.apache.kafka.common.security.JaasContext.loadClientContext(JaasContext.java:84) ~[kafka-clients-2.5.1.jar!/:na]
at org.apache.kafka.common.network.ChannelBuilders.create(ChannelBuilders.java:134) ~[kafka-clients-2.5.1.jar!/:na]
at org.apache.kafka.common.network.ChannelBuilders.clientChannelBuilder(ChannelBuilders.java:73) ~[kafka-clients-2.5.1.jar!/:na]
at org.apache.kafka.clients.ClientUtils.createChannelBuilder(ClientUtils.java:105) ~[kafka-clients-2.5.1.jar!/:na]
at org.apache.kafka.clients.producer.KafkaProducer.newSender(KafkaProducer.java:442) ~[kafka-clients-2.5.1.jar!/:na]
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:423) ~[kafka-clients-2.5.1.jar!/:na]
... 26 common frames omitted
Caused by: java.io.IOException: 配置错误: 没有此文件或目录
at sun.security.provider.ConfigFile$Spi.init(Unknown Source) ~[na:1.8.0_191]
at sun.security.provider.ConfigFile$Spi.init(Unknown Source) ~[na:1.8.0_191]
... 45 common frames omitted
ok
怎么解决的呢?
把权限文件放外面把路径写死就行了
你需要把kafka_client_jaas.conf 放在你启动项目的Resource目录下,通过classpath路径直接获取。
原因是你打成jar包之后,启动项目会把所有的jar和class文件加载了启动项目,你使用getResources他不会去找你所对应的jar包里面的class文件获取配kafka_client_jaas.conf ,而是直接通过项目直接获取kafka_client_jaas.conf,则此此路径不对则找不到相对应的文件。
是放在Resource下的
kafka_client_jaas.conf
是在启动类的Resource
中吗?是的
你如果是使用FileInputStream去读取文件,它是绝对路径获取文件,
使用Jar的时候启动运行,系统不是认这个路径的。
你可以读取文件的时候使用
他是直接去classes目录中获取资源读取的。
他不是读的文件而是读的配置
先前发布贴出来的代码是如何获取配置文件的路径。
能否将读取
kafka_client_jaas.conf
配置文件的代码贴出来看下吗?我把它写死了文件放出来了
KafkaClient { org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="Zjshenyue2021"; };
你的答案