1

I am working through a text book example program pertaining to Spring Microservices. The application.properties file is straightforward.

# Gives us access to the H2 database web console
spring.h2.console.enabled=true
# Generates the database *only* if it's not there yet
spring.jpa.hibernate.ddl-auto=update
# Creates the database in a file
spring.datasource.url=jdbc:h2:file:~/social-multiplication;DB_CLOSE_ON_EXIT=FALSE;
# For educational purposes we will show the SQL in console
spring.jpa.properties.hibernate.show_sql=true

Based on this, I'm assuming the default user "sa" and blank password should be sufficient to investigate the contents of the database.

When restarting the project, the console gives the message:

H2 console available at '/h2-console'. Database available at 'jdbc:h2:file:~/social-multiplication'

I am able to get the console form at localhost:8080/hb-console. I placed the URL "jdbc:h2:file:~/social-multiplication" for the location and left the username and password, and tried to enter the database, but at this point am getting "wrong user name or password" error message.

I have attempted to follow advice from other posts, such as this one: how to reslove wrong username and password error in h2 database and springboot?. The two database files at "~/social-multiplication" were deleted, and the program rebooted. This did not help.

I tried making an explicit username and password in the application.properties file, but with this, the program would not even load. It errors out on the very first line when it came time to build the database. Was using the following two line in application.properties:

spring.datasource.username=admin
spring.datasource.password=password

The line that errors out is the single line in the main().

public static void main(String[] args) {
    SpringApplication.run(SocialMultiplicationApplication.class, args);
}

Error:

2022-03-15 23:37:19.424  INFO 6260 ---   main] com.zaxxer.hikari.HikariDataSource    :HikariPool-1 - Starting...
2022-03-15 23:37:21.024 ERROR 6260 ---   main] com.zaxxer.hikari.pool.HikariPool     :HikariPool-1 - Exception during pool initialization.

org.h2.jdbc.JdbcSQLInvalidAuthorizationSpecException: Wrong user name or password [28000-200]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:461) ~[h2-1.4.200.jar:1.4.200]
        ...
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.4.jar:2.6.4]
        at microservices.book.multiplication.SocialMultiplicationApplication.main(SocialMultiplicationApplication.java:10) ~[classes/:na]

I find this very difficult to interpret. I thought one could set these properties. But something in the code is requiring or using a different password or username? There is no authentication or security explicitly implemented at this point in the tutorial (https://github.com/microservices-practical/microservices-v4)

Another thing I tried was to move the h2 database location to within the project folder, which also failed to work.

Given the number of improvements in security in many programs since 2017, I'm wondering if there are some new defaults or rules that were not dealt with in the tutorial that need to be handled.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
  • I had the same error when starting my Spring Boot app, but it just happens that I was not running it with the right working directory, and thus it was accessing a bad file. After changing the working dir it was ok. Seems to be a very different issue from yours though. – Didier L May 25 '22 at 14:43
  • Please refer to my answer here https://stackoverflow.com/a/74924258/398348 – likejudo Dec 26 '22 at 22:02

1 Answers1

0

Witch version of spring boot you use ?

Pre-conditions: delete h2 database or change the path file of database to be sure.

Note: if you create the h2 database with this sa user you can not access to database with another user.

  • Spring boot version <2.4.x : no change.

the default value of spring.datasource.username is sa and the default value of spring.datasource.password is " " (empty).

  • Spring boot version >=2.4.x :

You shoud to define the property : spring.datasource.username=sa in application.properties.

skillup
  • 190
  • 6