Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Latest commit

 

History

History

blueprint-springmvc-thymeleaf-javaconfig-security-database

DigitalCollections: Blueprints 2: Spring MVC + Thymeleaf + JavaConfig + Security +JPA/Hibernate

This blueprint webapp is demonstrating the integration of fundamental technologies for a Spring Java webapp.

Technologies used:

  • Overall: Java, Spring, Spring Security
  • Frontend: Spring MVC, Thymeleaf
  • Business: Java
  • Backend: JPA/Hibernate, Flyway

Features:

  • Automatic admin user wizard.

  • Multilingual GUI.

  • Login/logout.

  • User management (CRUD)

  • Session logging incl. AOP-logging

  • Layer modularization (Frontend, Business, Backend; each API and IMPL)

  • Maven Site

Installation

  1. Install PostgreSql:

    on Ubuntu:

$ apt-cache search postgresql
...
postgresql - object-relational SQL database (supported version)
postgresql-9.4 - object-relational SQL database, version 9.4 server
...
$ sudo apt-get install postgresql
  1. Create a database on your PostgreSql instance:
$ sudo su - postgres
($ dropdb 'blue_db')
$ psql -c "CREATE USER blue PASSWORD 'somepassword';"
CREATE ROLE
$ createdb blue_db -O blue
Check:

List databases:
$ psql -l
                       List of databases
Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
          |          |          |             |             | postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
          |          |          |             |             | postgres=CTc/postgres
blue_db   | blu      | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(4 rows)
List tables of database blue_db:
$ psql -d blue_db
psql (9.4.1)
Type "help" for help.
blue_db=# \d
No relations found.
blue_db=# \q
  1. Put your database properties into configuration file(s):
$ cd <source directory>
$ vi dc-blueprints-crud-hibernate-backend-impl-jpa/src/main/resources/de/digitalcollections/blueprints/crud/config/SpringConfigBackend-<profile>.properties

database.name=blue_db
database.hostname=localhost
database.password=somepassword
database.port=5432
database.username=blue

Build

$ cd <source directory>
$ mvn clean install

Usage

Run (in development)

$ cd dc-blueprints-crud-hibernate-frontend-impl-webapp
$ mvn jetty:run

Run (in production)

  • Deploy WAR to Tomcat
  • Start with java environment variable "spring.profiles.active" set to "PROD" (-Dspring.profiles.active:PROD)

View

Browser: http://localhost:9898

The webapp connects to the database and if no admin user exists, the admin user creation assistant is launched. Create an admin user.

Development Quickstart using Docker Compose

Installation

Install Docker according to the official Docker documentation. Install Docker Compose according to the official documentation.

Debian 9

$ su -
# apt-get install apt-transport-https dirmngr
# echo 'deb https://apt.dockerproject.org/repo debian-stretch main' >> /etc/apt/sources.list
# apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys F76221572C52609D
Executing: /tmp/apt-key-gpghome.wDKSqs4VYM/gpg.1.sh --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys F76221572C52609D
gpg: key F76221572C52609D: public key "Docker Release Tool (releasedocker) <[email protected]>" imported
gpg: Total number processed: 1
gpg:               imported: 1
# apt-get update
# apt-get install docker-engine
# apt-get install docker-compose

Configuration

All Linux

Add your user to docker group to run docker without sudo:

$ sudo groupadd docker
$ sudo gpasswd -a yourusername docker
$ sudo service docker restart

Usage

To get the application quickly up running, you can start all backend services using Docker Compose:

$ docker-compose build
$ docker-compose up -d

Then PostgreSql is running in a container and everything is ready for running a local instance of the application (see below).

To stop the container run

$ docker-compose stop

To delete the container and all data:

$ docker-compose down

Migrations

Flyway 4.2.0 to 5.2.4

pom.xml:

<dependency>
  <groupId>org.flywaydb</groupId>
  <artifactId>flyway-core</artifactId>
  <version>5.2.4</version>
</dependency>

SpringConfigBackendDatabase.java:

old:

@Bean(initMethod = "migrate")
public Flyway flyway() {
  Flyway flyway = new Flyway();
  flyway.setDataSource(pooledDataSource()); // could be another datasource with different user/pwd...
  flyway.setLocations("classpath:/de/digitalcollections/blueprints/crud/backend/impl/database/migration");
  flyway.setBaselineOnMigrate(true);
  return flyway;
}

new:

@Bean(initMethod = "migrate")
public Flyway flyway() {
  Flyway flyway = Flyway.configure().dataSource(pooledDataSource()).locations("classpath:/de/digitalcollections/blueprints/crud/backend/impl/database/migration").baselineOnMigrate(true).load();
  return flyway;
}

Manually rename table "schema_version" to "flyway_schema_history":

alter table schema_version rename to flyway_schema_history;