How to configure database connection runtime in Spring Boot? - spring

I made a new Spring Boot project using the Spring Initializr. I'm building an On-premise backend so what I'm trying to achieve is that when the user opens the jar executable and the server starts, he should be able to configure the database connection by going to localhost:8080/ in his web browser. Basically the index.html will have a form with 4 fields for IP Address, Database Name, UserName and Password. When the form is submitted spring will try to connect to the database with the provided information.
I have all my entities, repositories and controllers but currently the only way i can connect to a database is with the application.properties file, but since the user wont have access to the source, there should be a way for him to configure his database.
Thanks for your time!

I would suggest to use the Spring cloud Config server to store database related properties which is capable of picking up configuration at run time. Although it is typically configured with a Git repository, you can store them locally as pointed out in this thread.

Related

Update keycloak from config file

I am using keycloak embedded into spring application explained in this tutorial https://www.baeldung.com/keycloak-embedded-in-spring-boot-app
There is a configuration file for keycloak that is contained within that application, but the configuration is imported into keycloak and into database only the first time that application is started.
Is there a way to make updates on that config file reflect on the state of the keycloak?
Developers should be able to change configurations and push changes to version control. When somebody pulls the new configuration it should reflect on his local instance of keycloak.

How to integrate Keycloak user data into app db

I have a spring boot application and a user column in my app db. The problem is now that my user informations are divided up at the spring boot db and the Keycloak db.
I found this older posts where there synced the data manually (redundant on both dbs), isn't there a best practice way, isn't this a standard usecase, I am wondering why there is no information about this?
How to integrate or make use of KeyCloak user database in my application?
Keycloak provider and user storage
I would say that the standard use-case is you connect
Keycloak to your application's database, which contains users' information (keycloak is just a server to authenticate your users).
To be able to connect from Keycloak to your application's database, you need to implement new Service provider interface (User Storage SPI) as mentioned in the documentation.

How to store OAuth2 session into database and share it between Spring Boot servers

I want to create a solution based on that tutorial: https://www.baeldung.com/rest-api-spring-oauth2-angular
But it's not clear for me how several Spring Boot servers behind load balanced can share the same token session.
With session cookie the session data can be stored in MySQL and of server goes offline the session will be resumed by another Spring Boot server.
Can we have with OAuth2 the same setup with shared database so that the session data can be shared and switching of severs can be completely transparent for the end users?
Can we have with OAuth2 the same setup with shared database so that
the session data can be shared and switching of severs can be
completely transparent for the end users?
Yes, You just have to define a JdbcTokenStore and use it instead of the default InMemoryStore to store your OAuth tokens. The JdbcTokenStore must use a DataSource which is pointing to a MySQL Database.
Finally if you configure your Spring Boot apps to connect to the same MySQL Database to store Tokens by defining a DataSource you can get it working.
This post could be a good starting point.

Spring Boot: When to call remote service?

I have serveral microservices communicating with each other.
For general configuration I use Spring Cloud Config which works well.
Some of the services need to access database resources of a legacy system. So they need to know where the database (databases in a multi-tenant environment) is located and which credentials to use.
Using Spring Cloud Config I see two possibilities:
application.properties: This would expose the db settings to all services. That´s no option.
my-crazy-service.properties: This would work fine but I would have to configure any service which needs db access. Doesn´t scale well.
So my idea is to implement another microservice which is responsible for any connection infomation. This service exposes a rest endpoint using spring-data-rest.
In case Service A wants to use the legacy db it can call the new service and ask for the required data.
Now I wonder when the best time is to request the connection info from the remote service.
On startup of each microservice? Where should such startup code be located?
In general where should initialization stuff be done?

Modify ldap provider url without restarting the spring security application

we are providing facility to customer to configure ldap server runtime. But when i modify provider server url used in constructor of context source, the application crashes. Is there any way to change ldap server url at runtime? for LdapAuthenticationProvider.
If this is a case where you are changing the provider because one may be down for some reason, you should set up multiple authentication providers (security:authentication-provider) in your spring-security config file. Spring-security will start at the top of the list & keep trying until it finds one that works. That way you can leave this setup & not have a need to redeploy your code.

Resources