summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--microservices/02-api-gateway/linux/deb/package/etc/ceit-fs-api-gateway/application.yaml2
-rw-r--r--microservices/02-api-gateway/src/main/resources/application.yaml2
-rw-r--r--microservices/03-resource-server/src/main/java/com/example/resourceserver/config/SecurityConfig.java2
-rw-r--r--microservices/03-resource-server/src/main/java/com/example/resourceserver/security/AuthRestController.java43
-rw-r--r--microservices/03-resource-server/src/main/java/com/example/resourceserver/security/UserRestController.java12
5 files changed, 46 insertions, 15 deletions
diff --git a/microservices/02-api-gateway/linux/deb/package/etc/ceit-fs-api-gateway/application.yaml b/microservices/02-api-gateway/linux/deb/package/etc/ceit-fs-api-gateway/application.yaml
index 2747d3b..d037d5b 100644
--- a/microservices/02-api-gateway/linux/deb/package/etc/ceit-fs-api-gateway/application.yaml
+++ b/microservices/02-api-gateway/linux/deb/package/etc/ceit-fs-api-gateway/application.yaml
@@ -3,7 +3,7 @@ server:
address: 127.0.0.1
app:
- userRolesUrl: "http://localhost:8052/myservices/api/security/users/{username}/roles"
+ userRolesUrl: "http://localhost:8052/myservices/security/open/users/{username}/roles"
# logging:
# level:
diff --git a/microservices/02-api-gateway/src/main/resources/application.yaml b/microservices/02-api-gateway/src/main/resources/application.yaml
index b444233..f51dbfe 100644
--- a/microservices/02-api-gateway/src/main/resources/application.yaml
+++ b/microservices/02-api-gateway/src/main/resources/application.yaml
@@ -3,7 +3,7 @@ server:
address: 127.0.0.1
app:
- userRolesUrl: "http://localhost:8052/myservices/api/security/users/{username}/roles"
+ userRolesUrl: "http://localhost:8052/myservices/security/open/users/{username}/roles"
# logging:
# level:
diff --git a/microservices/03-resource-server/src/main/java/com/example/resourceserver/config/SecurityConfig.java b/microservices/03-resource-server/src/main/java/com/example/resourceserver/config/SecurityConfig.java
index 7102e69..bf9dbe5 100644
--- a/microservices/03-resource-server/src/main/java/com/example/resourceserver/config/SecurityConfig.java
+++ b/microservices/03-resource-server/src/main/java/com/example/resourceserver/config/SecurityConfig.java
@@ -18,7 +18,7 @@ public class SecurityConfig {
http
.authorizeHttpRequests(authorize -> authorize
// 1. Specify the URL to allow without restrictions
- .requestMatchers("/api/security/users/*/roles", "/public/**").permitAll()
+ .requestMatchers("/security/open/users/*/roles", "/public/**").permitAll()
// 2. Require authentication for all other requests
.anyRequest().authenticated()
)
diff --git a/microservices/03-resource-server/src/main/java/com/example/resourceserver/security/AuthRestController.java b/microservices/03-resource-server/src/main/java/com/example/resourceserver/security/AuthRestController.java
new file mode 100644
index 0000000..2c9b48a
--- /dev/null
+++ b/microservices/03-resource-server/src/main/java/com/example/resourceserver/security/AuthRestController.java
@@ -0,0 +1,43 @@
+package com.example.resourceserver.security;
+
+import java.util.Optional;
+import java.util.Set;
+
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * This controller is a special one. The getRoles() method is accessed by calls from API gateway
+ * when someone logs in to the API gateway.
+ *
+ * The path is publicly accessible. To prevent access via API gateway by API gateway clients, a different
+ * path is used (not start with /api).
+ *
+ * SecurityConfig.java has been configured to allow access without authentication.
+ *
+ * AuthRestController
+ */
+@RestController
+@RequestMapping("/security/open/users")
+public class AuthRestController {
+ private UserService userService;
+
+ public AuthRestController(UserService userService) {
+ this.userService = userService;
+ }
+
+ @GetMapping("/{username}/roles")
+ public ResponseEntity<RolesResponse> getRoles(@PathVariable String username) {
+ Optional<User> user = userService.findByUsername(username);
+
+ if (user.isEmpty()) {
+ return ResponseEntity.ok(new RolesResponse(Set.of()));
+ }
+
+ return ResponseEntity.ok(new RolesResponse(user.get().getRoles()));
+ }
+
+}
diff --git a/microservices/03-resource-server/src/main/java/com/example/resourceserver/security/UserRestController.java b/microservices/03-resource-server/src/main/java/com/example/resourceserver/security/UserRestController.java
index 0eb0d51..1b0598d 100644
--- a/microservices/03-resource-server/src/main/java/com/example/resourceserver/security/UserRestController.java
+++ b/microservices/03-resource-server/src/main/java/com/example/resourceserver/security/UserRestController.java
@@ -13,7 +13,6 @@ import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -27,17 +26,6 @@ public class UserRestController {
this.userService = userService;
}
- @GetMapping("/{username}/roles")
- public ResponseEntity<RolesResponse> getRoles(@PathVariable String username) {
- Optional<User> user = userService.findByUsername(username);
-
- if (user.isEmpty()) {
- return ResponseEntity.ok(new RolesResponse(Set.of()));
- }
-
- return ResponseEntity.ok(new RolesResponse(user.get().getRoles()));
- }
-
@GetMapping("/test")
public String test(Principal principal) {
return principal.toString();