From 41b068b6d48f9d82babd1cfce3520ad0b28be861 Mon Sep 17 00:00:00 2001 From: Kamal Wickramanayake Date: Sat, 20 Jun 2026 18:25:56 +0530 Subject: Added Spring Boot role based security sample application --- .../src/main/resources/application.properties | 1 + .../resources/config/application-dev.properties | 33 ++++++++ .../src/main/resources/data.sql | 5 ++ .../src/main/resources/static/css/main.css | 92 ++++++++++++++++++++++ .../src/main/resources/templates/contact/form.html | 30 +++++++ .../main/resources/templates/contact/result.html | 14 ++++ .../src/main/resources/templates/index.html | 18 +++++ .../src/main/resources/templates/layout/main.html | 36 +++++++++ 8 files changed, 229 insertions(+) create mode 100644 spring-boot/10-role-based-security/src/main/resources/application.properties create mode 100644 spring-boot/10-role-based-security/src/main/resources/config/application-dev.properties create mode 100644 spring-boot/10-role-based-security/src/main/resources/data.sql create mode 100644 spring-boot/10-role-based-security/src/main/resources/static/css/main.css create mode 100644 spring-boot/10-role-based-security/src/main/resources/templates/contact/form.html create mode 100644 spring-boot/10-role-based-security/src/main/resources/templates/contact/result.html create mode 100644 spring-boot/10-role-based-security/src/main/resources/templates/index.html create mode 100644 spring-boot/10-role-based-security/src/main/resources/templates/layout/main.html (limited to 'spring-boot/10-role-based-security/src/main/resources') diff --git a/spring-boot/10-role-based-security/src/main/resources/application.properties b/spring-boot/10-role-based-security/src/main/resources/application.properties new file mode 100644 index 0000000..8a8c4c2 --- /dev/null +++ b/spring-boot/10-role-based-security/src/main/resources/application.properties @@ -0,0 +1 @@ +# Look at resources/config/application-dev.properties for settings for dev profile. \ No newline at end of file diff --git a/spring-boot/10-role-based-security/src/main/resources/config/application-dev.properties b/spring-boot/10-role-based-security/src/main/resources/config/application-dev.properties new file mode 100644 index 0000000..50973c7 --- /dev/null +++ b/spring-boot/10-role-based-security/src/main/resources/config/application-dev.properties @@ -0,0 +1,33 @@ +# This file contains settings used during development. +# If using Maven to run, try the following command: +# mvn spring-boot:run -Dspring-boot.run.profiles=dev + +# Web related settings + +# Disable caching for static resources +spring.web.resources.cache.period=0 +spring.thymeleaf.cache=false + +# Optional: Tell DevTools to watch changes but never restart the backend server for CSS +spring.devtools.restart.exclude=static/**,public/**,templates/** + + +# Database related settings + +spring.datasource.url=jdbc:postgresql://localhost:5432/springboot +spring.datasource.username=springboot +spring.datasource.password=abc123 + +# Create/update/drop database tables automatically? +# In production, setting value to none is better. +# Possible values: none, validate, update, create, create-drop +spring.jpa.hibernate.ddl-auto=create-drop + +# Print sql that gets executed. Good for developers. Set to false in production. +spring.jpa.show-sql=true + +# Always execute initialization scripts (resources/schema.sql, resources/data.sql) on startup +spring.sql.init.mode=always + +# Data source initialization scripts (schema.sql and data.sql) are executed after Hibernate/JPA auto-creates database tables. +spring.jpa.defer-datasource-initialization=true diff --git a/spring-boot/10-role-based-security/src/main/resources/data.sql b/spring-boot/10-role-based-security/src/main/resources/data.sql new file mode 100644 index 0000000..5318008 --- /dev/null +++ b/spring-boot/10-role-based-security/src/main/resources/data.sql @@ -0,0 +1,5 @@ +INSERT INTO user_account (username, password, description) VALUES ('admin','$argon2id$v=19$m=16,t=2,p=1$YnJERWhhY0RxV1hGMFNWVg$OHRvhGZx1x9KqCoVZOfUD2TkJc+HQB5SamgYejw+K2Y', 'Admin user'); +INSERT INTO user_account (username, password, description) VALUES ('user1','$argon2id$v=19$m=16,t=2,p=1$YnJERWhhY0RxV1hGMFNWVg$OHRvhGZx1x9KqCoVZOfUD2TkJc+HQB5SamgYejw+K2Y', 'Normal user 1'); + +INSERT INTO user_roles (user_id, role_name) VALUES (1, 'ADMIN'); +INSERT INTO user_roles (user_id, role_name) VALUES (2, 'USER'); \ No newline at end of file diff --git a/spring-boot/10-role-based-security/src/main/resources/static/css/main.css b/spring-boot/10-role-based-security/src/main/resources/static/css/main.css new file mode 100644 index 0000000..edd53ea --- /dev/null +++ b/spring-boot/10-role-based-security/src/main/resources/static/css/main.css @@ -0,0 +1,92 @@ +body { + font-size: 1.125em; + font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; + line-height: 1.6em; + color: rgb(32, 34, 36);; + background-color: #FFF; + padding: 10px 5px; +} + +a { + color: #1475AC; + text-decoration: none; +} + +a:hover { + text-decoration: underline; +} + +ul li { + padding-bottom: 0.5em; +} + +header { + text-align: left; + font-size: 1.2em; +} + +header h1 { + margin-top: 0; + margin-bottom: 20px; +} + +header a { + color: #000000; + text-decoration: none; +} + +.main-nav { + margin-top: 0; + padding: 0; +} + +.main-nav ul { + margin: 0; + padding : 0; + list-style-type: none; + background-color: #F6F8F8; + border: 1px solid #eee; +} + +.main-nav ul li { + margin-left: 0; + display: inline; + padding: 0 8px; + border-left: 1px solid #F9F9FF; + border-right: 1px solid #F9F9FF; +} + +.main-nav ul li:hover { + background-color: white; + border-left: 1px solid #addfff; + border-right: 1px solid #addfff; +} + +.main-nav ul li a { + color: #555; + font-weight: bold; +} + +.main-nav ul li ai:hover { + color: #000; +} + +footer { + margin-top: 20px; + font-size: 85%; + border-top: 1px solid #CCC; +} + +.form-field-container { + margin-top: 8px; + margin-bottom: 8px; +} + +.form-field-container label { + display: inline-block; + width: 5em; +} + +.form-field-container .validation-error { + color: red; +} \ No newline at end of file diff --git a/spring-boot/10-role-based-security/src/main/resources/templates/contact/form.html b/spring-boot/10-role-based-security/src/main/resources/templates/contact/form.html new file mode 100644 index 0000000..7debe1f --- /dev/null +++ b/spring-boot/10-role-based-security/src/main/resources/templates/contact/form.html @@ -0,0 +1,30 @@ + + + + +
+
+
+ + +
+
+ +
+ + +
+
+ +
+ + +
+
+ + +
+
+ + + \ No newline at end of file diff --git a/spring-boot/10-role-based-security/src/main/resources/templates/contact/result.html b/spring-boot/10-role-based-security/src/main/resources/templates/contact/result.html new file mode 100644 index 0000000..d1bda9a --- /dev/null +++ b/spring-boot/10-role-based-security/src/main/resources/templates/contact/result.html @@ -0,0 +1,14 @@ + + + + +
+

We received your message.

+ +

+ + Try again +

+ + + \ No newline at end of file diff --git a/spring-boot/10-role-based-security/src/main/resources/templates/index.html b/spring-boot/10-role-based-security/src/main/resources/templates/index.html new file mode 100644 index 0000000..dce7a4e --- /dev/null +++ b/spring-boot/10-role-based-security/src/main/resources/templates/index.html @@ -0,0 +1,18 @@ + + + + +
+

Hello, Username Placeholder!

+ +

Hello, [[${#authentication.name}]]!

+ +

This is the home page content.

+ +

Use the navigation links to explore other pages.

+
+ + + \ No newline at end of file diff --git a/spring-boot/10-role-based-security/src/main/resources/templates/layout/main.html b/spring-boot/10-role-based-security/src/main/resources/templates/layout/main.html new file mode 100644 index 0000000..df96fc5 --- /dev/null +++ b/spring-boot/10-role-based-security/src/main/resources/templates/layout/main.html @@ -0,0 +1,36 @@ + + + + + Base title not shown + + + + + +
+

ABC App

+
+ + + +

Page content area title

+ + + + + + + \ No newline at end of file -- cgit v1.2.3