Spring boot CRUD JDBC API unable to connect to Oracle Database - spring

I am working on a CRUD JDBC API that will be able to being able to GET, POST, UPDATE, and DELETE data in my Table located SQL Developer. The application is running fine in spring boot but it does not show any connectivity with my data in SQL developer. I have double checked my applications.properties file but no luck.
Application.properties
spring.datasource.url = jdbc:oracle:thin:#localhost:1521:orcl
spring.datasource.username = DIP
spring.datasource.password = DIP
spring.datasource.driver-class-name = oracle.jdbc.driver.OracleDriver
server.port = 8080
SQL DEVELOPER Connection Name
DIP#//localhost:1521/orcl
JDBC SYSTEM PATH IN POM.XML
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.4.0.0.1</version>
<scope>system</scope>
<systemPath>C:/ojdbc8.jar</systemPath>
</dependency>

Related

How do I connect to Azure SQL DB with AD password using JDBC configuration?

I am trying to connect from my Spring Boot (2.0.1) Web Application to an Azure SQL DB using Azure AD (with the Application as the DB user) but cannot seem to connect the dots.
I followed the steps for Connecting using Azure AD Auth. However, I get a failed login.
I'm assuming that's because without setting the 'authentication' property in the JDBC url, it's just authenticating against the database directly (user created using CREATE USER <> FROM EXTERNAL PROVIDER from instructions)?
Is there a way to configure the spring datasource/jdbc libraries to use the proper Azure AD authentication (adal4j-1.6.3) while connecting? The example does this in code, but I'm having trouble finding the proper configurations.
I tried using 'authentication=ActiveDirectoryPassword', but kept getting a 'AADSTS50034: The user account does not exist in the directory' error.
spring.datasource.url="jdbc:sqlserver://myServer.database.windows.net:1433;database=myDB;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;applicationName=myApp;"
spring.datasource.username: <user>
spring.datasource.accessToken: <key>
The bigger goal is to secure my database access to specific apps. I had added my app as a Reader on my server and ideally would authenticate as that application to track usage/analytics/etc from that app.
I apologize if this is way off, I'm new to Azure and Oauth. Thanks.
For me the below properties and dependencies worked:
Instead of using adal4j I have used msal4j(recommended by Microsoft)
spring:
datasource:
url: jdbc:sqlserver://<Azure SQL Server Name>.database.windows.net:1433;databaseName=<DB Name>;encrypt=true;loginTimeout=30
username: <UserName>
password: <Password>
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
hikari:
data-source-properties:
authentication: ActiveDirectoryPassword
pom.xml
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>9.2.1.jre8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/msal4j -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.9.1</version>
</dependency>
There is no way to configure Spring to use an application Id + key from Azure AD in place of username/password when connecting to Azure SQL DB. The only way I found was to modify code and create a #Bean method that returned an SQLServerDataSource where I manually retrieve a Client Credential access token and pass that into the data source.
Connecting using an access token - shows how to retrieve token and set on data source
Creating a custom Data Source - shows how to use Spring to create and use custom data source
Also be aware of token management (see my other question).
Including dependency adal4j with below configuration would help
spring.datasource.username=\<your AD username\>
spring.datasource.password=\<your AD password\>
spring.datasource.hikari.data-source-properties.authentication=ActiveDirectoryPassword
Please confirm that you are using a User that exists in the database and is a user in the same tenant where your database exists. If you are using a version of the Microsoft JDBC Driver that is earlier than the 4.0 version, you are also required to append the server name to the UserId to the connection string.
https://learn.microsoft.com/en-us/sql/connect/jdbc/connecting-to-an-azure-sql-database?view=sql-server-2017

How should I configure application.properties to connect Azure SQL database in spring boot?

I created sample spring boot application with mysql db.now I want to connect Azure SQL database instead of MySQL.how to write application.properties?
Here are the Application.properties:
spring.datasource.url=<JDBC Connection String>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
Replace <JDBC Connect String>, <username>, <password> with your SQL database JDBC connection String, login name and password.
You can copy your Azure database connection String from Azure portal:

spring-boot: start application without database if password fails

I am using the application in two instances that have different database passwords. My code only needs to access and query one instance, and doesn't need to access database on another instance. If I try to deploy the app with wrong password, it gives me "password authentication" error and doesn't start the application.
I want the app to ignore the database connection if password authentication is failed.
Here is my application.properties file.
spring.security.enabled=false
management.security.enabled=false
security.basic.enabled=false
## Postgresql for storing backup details
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url= jdbc:postgresql://localhost:5432/dbName
spring.datasource.username=username
spring.datasource.password=password
#spring.datasource.continue-on-error=true
#spring.datasource.initialize=false
spring.jpa.database-platform = org.hibernate.dialect.PostgreSQL94Dialect
spring.datasource.primary.continueOnError=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Spring boot version is 1.5.9
You can run spring boot application without datasource. For that you must disable the auto configuration of datasource and probably the JPA auto configuration as well. Place the below line in your #SpringBootApplication
#EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})

Spring Cloud Bus - Refresh a specific cluster of client

I have a spring cloud config server configured over spring cloud bus over kafka.
I am using Edgware.RELEASE with boot 1.5.9.
When I send a POST request to the endpoint /bus/refresh with destination=clientId:dev:** in the body via POSTMAN to config server, all the clients get their beans refreshed even though their clientId doesn't match the value in the destination field.
Here are additional configuration details:
spring cloud config server
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
application.properties
server.port=8888
management.security.enabled=false
spring.cloud.bus.enabled=true
spring.kafka.bootstrap-servers=localhost:9092
I have two config clients with id - config-client1 and config-client2
After changing an application property in the repository for config-client2, I submit a POST request to /bus/refresh endpoint on the config server, with destination=config-client2:dev:** in the body of the request. I was hoping this would refresh/re-initialize the beans only in config-client2 application. But I noticed beans in config-client application are also refreshed/re-initialized. I also noticed config-client application receiving refresh event along with config-client2.
I was hoping only config-client2 application receives the event and its beans are the only ones that are refreshed as a result of it.
Wondering if I am missing any configuration setting to enable granular refresh at specific client level.
I did go through the document posted at - http://cloud.spring.io/spring-cloud-static/Edgware.RELEASE/single/spring-cloud.html, but the results are not as explained.
Any help with a working example is appreciated.
You can use:
curl -X POST http://localhost:8001/actuator/bus-refresh/config-client2
You also need this in your application.properties or .yml.
spring.cloud.config.uri=http://localhost:8001
spring.cloud.config.uri should point to your Spring Config Server

Custom service connector for DB2 service in pivotal cloud foundry

I have a spring boot app which i am trying to connect to DB2 service.But i am getting No service connection creator exception as pivotal cloud foundry has support for Mongo db and few other db's.Can anyone tell how to write custom service connector in spring boot app for DB2 service ?
Create the DB2 service as a user-provided service using the command line:
cf cups db2-service -p "uri,user,password"
You will be prompted for the parameters. Enter user, password, and URI:
jdbc:db2://<host>[:<port>]/<database_name>
Make sure you have Cloud Connectors declared in your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cloud-connectors</artifactId>
</dependency>
Bind the service to your app. Now you can access the database through declarations like:
#Autowired
JdbcTemplate _jdbcTemplate;

Resources