cancel
Showing results for 
Search instead for 
Did you mean: 

How can we set username and password in spring boot neo4j programmatically

kapilarora5988
Node Link

How can we set username and password in spring boot neo4j programmatically?

1 ACCEPTED SOLUTION

gerrit_meier
Neo4j
Neo4j

Assuming you are using Spring Data Neo4j 5 from the other topics, you can define your own SessionFactory or just the Configuration bean. This is usually done by the Spring Boot starter / auto configuration for you, but will not produce it if you are providing it manually.
So either:

@Configuration
public class Neo4jConfig {

	@Bean
	public SessionFactory sessionFactory() {
		org.neo4j.ogm.config.Configuration config =
		new org.neo4j.ogm.config.Configuration.Builder().credentials("neo4j", "secret").uri("neo4j://localhost:7687").build();
		return new SessionFactory(config, "com.myappliction");
	}
}

or

@Configuration
public class Neo4jConfig {

	@Bean
	public org.neo4j.ogm.config.Configuration configuration(){
		return new org.neo4j.ogm.config.Configuration.Builder().credentials("neo4j", "secret").uri("neo4j://localhost:7687").build();
	}
}

would work for you.

View solution in original post

5 REPLIES 5

gerrit_meier
Neo4j
Neo4j

Assuming you are using Spring Data Neo4j 5 from the other topics, you can define your own SessionFactory or just the Configuration bean. This is usually done by the Spring Boot starter / auto configuration for you, but will not produce it if you are providing it manually.
So either:

@Configuration
public class Neo4jConfig {

	@Bean
	public SessionFactory sessionFactory() {
		org.neo4j.ogm.config.Configuration config =
		new org.neo4j.ogm.config.Configuration.Builder().credentials("neo4j", "secret").uri("neo4j://localhost:7687").build();
		return new SessionFactory(config, "com.myappliction");
	}
}

or

@Configuration
public class Neo4jConfig {

	@Bean
	public org.neo4j.ogm.config.Configuration configuration(){
		return new org.neo4j.ogm.config.Configuration.Builder().credentials("neo4j", "secret").uri("neo4j://localhost:7687").build();
	}
}

would work for you.

kapilarora5988
Node Link

Can we use sdn 6 with spring boot 2.3.5?

No, this is unfortunately not possible.
Spring Data Neo4j 6 is based on the newest Spring Data commons that are based on the new version of the Spring framework itself. So the only way to not run into dependency versions issues is to upgrade to Spring Boot 2.4.

jeneydani
Node

Hi Gerrit! I am moving from spring boot 2.3 to 2.4.(neo4j 6)
In 2.3 i had the database authentication set by the org.neo4j.ogm.config.Configuration as you described above(but pulled from vault programatically). And then that was given into the SessionFactory, which was given into the Neo4jTransactionManager.

I am not really in the clear yet, about what all that does, i am just tasked to upgrade spring boot.

I guess the main question for me here is: how can i set the credentials as before - as the Configuration bean is not avaiable anymore?
I see that it should be provided in the application.yaml, but if i am pulling the password from vault from a VaultService bean, then i cant write it in there.

The other question would be, SessionFactory/Neo4jTransactionManager are now autoconfigured and i dont have to set those right?

( cool videos:D)
Thanks,
Daniel

Basically yes: The Neo4jTransactionManager will get auto-created for you. There is no need for a (Neo4j-OGM) SessionFactory anymore.

You should create a Driver bean instead.

@Bean
public Driver driver(/* Here you could access the Vault properties */) {
	AuthToken authToken = AuthTokens.basic(/* Vault user */, /* Vault password */);
	String boltUri = "bolt..." /* or also coming from Vault */;
	/* Optional and only if needed for further driver configuration */
	Config config = Config.builder().build();
	return GraphDatabase.driver(boltUri, authToken, config);
}

Disclaimer: There might be a more convenient way to use the Vault in combination with Spring Boot to set the properties like spring.neo4j.uri, ...username, ...password directly, but I am not very experienced with it.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.