Spring Boot配置认证后无法连接Neo4J数据库问题求助
针对你遇到的TransactionSystemException: Could not open a new Neo4j session: Unsupported authentication token, scheme 'none' is only allowed when auth is disabled报错,以下是几个排查和解决方向:
检查配置项前缀是否匹配依赖版本
不同版本的Spring Data Neo4J配置前缀可能存在差异,若使用较新版本的依赖,尝试将配置项替换为:spring.neo4j.uri=bolt://localhost:7687 spring.neo4j.authentication.username=neo4j spring.neo4j.authentication.password=myPassw@rd同时确认pom.xml或build.gradle中Spring Data Neo4J的版本与Neo4J服务器版本兼容,版本不匹配可能导致配置读取失效。
验证配置文件是否被正确加载
确保application.properties位于src/main/resources目录下,若使用多环境配置,确认当前激活的环境配置包含正确的认证信息。
可以添加一段代码验证配置读取情况:import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class ConfigChecker implements CommandLineRunner { @Value("${spring.data.neo4j.username:未读取到}") private String neo4jUsername; @Value("${spring.data.neo4j.password:未读取到}") private String neo4jPassword; @Override public void run(String... args) throws Exception { System.out.println("读取到的Neo4J用户名:" + neo4jUsername); System.out.println("读取到的Neo4J密码:" + neo4jPassword); } }启动应用后查看控制台输出,确认配置是否被正确读取。
检查自定义Neo4J配置是否覆盖了默认配置
若代码中自定义了Neo4jSessionFactory或DriverBean,需确保创建Driver时传入了正确的认证信息:import org.neo4j.driver.AuthTokens; import org.neo4j.driver.Driver; import org.neo4j.driver.GraphDatabase; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Neo4jConfig { @Value("${spring.data.neo4j.uri}") private String uri; @Value("${spring.data.neo4j.username}") private String username; @Value("${spring.data.neo4j.password}") private String password; @Bean public Driver neo4jDriver() { return GraphDatabase.driver(uri, AuthTokens.basic(username, password)); } }避免手动创建Driver时使用无认证的方式,导致忽略配置文件中的账号密码。
清理项目缓存并重新构建
执行mvn clean install(Maven项目)或gradle clean build(Gradle项目)清理缓存,重新构建项目,避免旧的编译文件或配置缓存导致问题。同时重启IDE确保配置修改被识别。
内容的提问来源于stack exchange,提问作者ambuli

