From aa122113ade36f02dc8fdbebdf1232b5c4b8742c Mon Sep 17 00:00:00 2001 From: Kamal Wickramanayake Date: Fri, 3 Jul 2026 19:02:36 +0530 Subject: Microservice sample projects --- .../oauth2server/Oauth2ServerApplication.java | 13 ++++++++ .../com/example/oauth2server/config/AppConfig.java | 16 +++++++++ .../oauth2server/config/SecurityConfig.java | 27 +++++++++++++++ .../oauth2server/config/UserProperties.java | 19 +++++++++++ .../src/main/resources/application.yaml | 39 ++++++++++++++++++++++ .../src/main/resources/templates/error.html | 26 +++++++++++++++ .../oauth2server/Oauth2ServerApplicationTests.java | 13 ++++++++ 7 files changed, 153 insertions(+) create mode 100644 microservices/01-oauth2-server/src/main/java/com/example/oauth2server/Oauth2ServerApplication.java create mode 100644 microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/AppConfig.java create mode 100644 microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/SecurityConfig.java create mode 100644 microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/UserProperties.java create mode 100644 microservices/01-oauth2-server/src/main/resources/application.yaml create mode 100644 microservices/01-oauth2-server/src/main/resources/templates/error.html create mode 100644 microservices/01-oauth2-server/src/test/java/com/example/oauth2server/Oauth2ServerApplicationTests.java (limited to 'microservices/01-oauth2-server/src') diff --git a/microservices/01-oauth2-server/src/main/java/com/example/oauth2server/Oauth2ServerApplication.java b/microservices/01-oauth2-server/src/main/java/com/example/oauth2server/Oauth2ServerApplication.java new file mode 100644 index 0000000..68ee050 --- /dev/null +++ b/microservices/01-oauth2-server/src/main/java/com/example/oauth2server/Oauth2ServerApplication.java @@ -0,0 +1,13 @@ +package com.example.oauth2server; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Oauth2ServerApplication { + + public static void main(String[] args) { + SpringApplication.run(Oauth2ServerApplication.class, args); + } + +} diff --git a/microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/AppConfig.java b/microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/AppConfig.java new file mode 100644 index 0000000..d841104 --- /dev/null +++ b/microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/AppConfig.java @@ -0,0 +1,16 @@ +package com.example.oauth2server.config; + +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix = "app") +public class AppConfig { + private List users; + + // Getters and Setters are required + public List getUsers() { return users; } + public void setUsers(List users) { this.users = users; } +} diff --git a/microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/SecurityConfig.java b/microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/SecurityConfig.java new file mode 100644 index 0000000..205ad71 --- /dev/null +++ b/microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/SecurityConfig.java @@ -0,0 +1,27 @@ +package com.example.oauth2server.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; + +@Configuration +public class SecurityConfig { + private AppConfig appConfig; + + public SecurityConfig(AppConfig appConfig) { + this.appConfig = appConfig; + } + + @Bean + public UserDetailsService userDetailsService() { + InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); + + appConfig.getUsers().forEach(user -> manager.createUser(User.withUsername(user.getUsername()) + .password(user.getPassword()) + .build())); + + return manager; + } +} diff --git a/microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/UserProperties.java b/microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/UserProperties.java new file mode 100644 index 0000000..c81a52a --- /dev/null +++ b/microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/UserProperties.java @@ -0,0 +1,19 @@ +package com.example.oauth2server.config; + +public class UserProperties { + private String username; + private String password; + + public String getUsername() { + return username; + } + public void setUsername(String username) { + this.username = username; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } +} diff --git a/microservices/01-oauth2-server/src/main/resources/application.yaml b/microservices/01-oauth2-server/src/main/resources/application.yaml new file mode 100644 index 0000000..b263a5e --- /dev/null +++ b/microservices/01-oauth2-server/src/main/resources/application.yaml @@ -0,0 +1,39 @@ +server: + port: 8051 + address: 127.0.0.1 + +#logging: +# level: +# org.springframework.security: trace + +spring: + application: + name: oauth2-server + security: + oauth2: + authorizationserver: + client: + oidc-client: + registration: + client-id: "api-gateway" + client-secret: "{noop}apiGatewayPassword1234" + client-authentication-methods: + - "client_secret_basic" + authorization-grant-types: + - "authorization_code" + - "refresh_token" + redirect-uris: + - "http://localhost:5173/bff/login/oauth2/code/api-gateway" + post-logout-redirect-uris: + - "http://localhost:5173/" + scopes: + - "openid" + - "profile" + require-authorization-consent: false + +app: + users: + - username: admin + password: "{noop}abc123" + - username: user1 + password: "{noop}abc123" diff --git a/microservices/01-oauth2-server/src/main/resources/templates/error.html b/microservices/01-oauth2-server/src/main/resources/templates/error.html new file mode 100644 index 0000000..f97dc77 --- /dev/null +++ b/microservices/01-oauth2-server/src/main/resources/templates/error.html @@ -0,0 +1,26 @@ + + + + + An Error Occurred + + + +

Something went wrong!

+

Status: 500

+

Error: Internal Server Error

+

Message: Unexpected error

+

Path: /

+ + Back to Home + +
+
+ +
+
+ + + \ No newline at end of file diff --git a/microservices/01-oauth2-server/src/test/java/com/example/oauth2server/Oauth2ServerApplicationTests.java b/microservices/01-oauth2-server/src/test/java/com/example/oauth2server/Oauth2ServerApplicationTests.java new file mode 100644 index 0000000..507215a --- /dev/null +++ b/microservices/01-oauth2-server/src/test/java/com/example/oauth2server/Oauth2ServerApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.oauth2server; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Oauth2ServerApplicationTests { + + @Test + void contextLoads() { + } + +} -- cgit v1.2.3