summaryrefslogtreecommitdiff
path: root/microservices/01-oauth2-server/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'microservices/01-oauth2-server/src/main')
-rw-r--r--microservices/01-oauth2-server/src/main/java/com/example/oauth2server/Oauth2ServerApplication.java13
-rw-r--r--microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/AppConfig.java16
-rw-r--r--microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/SecurityConfig.java27
-rw-r--r--microservices/01-oauth2-server/src/main/java/com/example/oauth2server/config/UserProperties.java19
-rw-r--r--microservices/01-oauth2-server/src/main/resources/application.yaml39
-rw-r--r--microservices/01-oauth2-server/src/main/resources/templates/error.html26
6 files changed, 140 insertions, 0 deletions
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<UserProperties> users;
+
+ // Getters and Setters are required
+ public List<UserProperties> getUsers() { return users; }
+ public void setUsers(List<UserProperties> 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 @@
+<!DOCTYPE html>
+<html xmlns:th="http://thymeleaf.org" xmlns:sec="http://thymeleaf.org/extras/spring-security">
+
+<head>
+ <title>An Error Occurred</title>
+</head>
+
+<body>
+ <h1>Something went wrong!</h1>
+ <p>Status: <span th:text="${status}">500</span></p>
+ <p>Error: <span th:text="${error}">Internal Server Error</span></p>
+ <p>Message: <span th:text="${message}">Unexpected error</span></p>
+ <p>Path: <span th:text="${path}">/</span></p>
+
+ <a th:href="@{/}">Back to Home</a>
+
+ <div sec:authorize="isAuthenticated()" style="padding-top: 10px;">
+ <form th:action="@{/logout}" method="post">
+ <button type="submit">
+ Logout
+ </button>
+ </form>
+ </div>
+</body>
+
+</html> \ No newline at end of file