From d797917c645cd08d6e0228c71c90c13ae03629d3 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 09:52:52 +0200 Subject: [PATCH 01/11] build: Update dependencies in build.gradle.kts --- build.gradle.kts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 985ba1c..5e31d8a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -62,16 +62,18 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-validation") - compileOnly("org.projectlombok:lombok") - annotationProcessor("org.projectlombok:lombok") - testImplementation("org.springframework.boot:spring-boot-starter-test") - testRuntimeOnly("org.junit.platform:junit-platform-launcher") + implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0") implementation("org.springframework.boot:spring-boot-starter-security") implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server:3.3.4") implementation("org.springframework.boot:spring-boot-starter-oauth2-client:3.3.4") - runtimeOnly("org.postgresql:postgresql") - implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0") + testImplementation("com.h2database:h2") + testImplementation("org.springframework.boot:spring-boot-starter-test") + + compileOnly("org.projectlombok:lombok") + annotationProcessor("org.projectlombok:lombok") + testRuntimeOnly("org.junit.platform:junit-platform-launcher") + runtimeOnly("org.postgresql:postgresql") } tasks.withType { -- 2.45.2 From bd30f6ba7483add0204c28d82955b9a7c9f1c20e Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 09:53:28 +0200 Subject: [PATCH 02/11] build: Update dependencies in build.gradle.kts --- build.gradle.kts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 5e31d8a..75e4316 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -62,10 +62,10 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-validation") - implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0") + implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui") implementation("org.springframework.boot:spring-boot-starter-security") - implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server:3.3.4") - implementation("org.springframework.boot:spring-boot-starter-oauth2-client:3.3.4") + implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server") + implementation("org.springframework.boot:spring-boot-starter-oauth2-client") testImplementation("com.h2database:h2") testImplementation("org.springframework.boot:spring-boot-starter-test") -- 2.45.2 From e237e5741e37cefaf1a459d3fb2142f2def5a691 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 09:58:20 +0200 Subject: [PATCH 03/11] docs(OpenAPIConfiguration): update authentication description format --- .../config/OpenAPIConfiguration.java | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/szut/lf8_starter/config/OpenAPIConfiguration.java b/src/main/java/de/szut/lf8_starter/config/OpenAPIConfiguration.java index 1b2282b..3c782d3 100644 --- a/src/main/java/de/szut/lf8_starter/config/OpenAPIConfiguration.java +++ b/src/main/java/de/szut/lf8_starter/config/OpenAPIConfiguration.java @@ -1,7 +1,6 @@ package de.szut.lf8_starter.config; - import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; @@ -33,15 +32,33 @@ public class OpenAPIConfiguration { .addServersItem(new Server().url(this.context.getContextPath())) .info(new Info() .title("LF8 project starter") - .description("\n## Auth\n" + - "\n## Authentication\n" + "\nThis Hello service uses JWTs to authenticate requests. You will receive a bearer token by making a POST-Request in IntelliJ on:\n\n" + - "\n" + - "```\nPOST http://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token\nContent-Type: application/x-www-form-urlencoded\ngrant_type=password&client_id=employee-management-service&username=user&password=test\n```\n" + - "\n" + - "\nor by CURL\n" + - "```\ncurl -X POST 'http://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token'\n--header 'Content-Type: application/x-www-form-urlencoded'\n--data-urlencode 'grant_type=password'\n--data-urlencode 'client_id=employee-management-service'\n--data-urlencode 'username=user'\n--data-urlencode 'password=test'\n```\n" + - "\nTo get a bearer-token in Postman, you have to follow the instructions in \n [Postman-Documentation](https://documenter.getpostman.com/view/7294517/SzmfZHnd).") - + .description(""" + ## Auth + + ## Authentication + + This Hello service uses JWTs to authenticate requests. You will receive a bearer token by making a POST-Request in IntelliJ on: + + ``` + POST http://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token + Content-Type: application/x-www-form-urlencoded + grant_type=password&client_id=employee-management-service&username=user&password=test + ``` + + or by CURL: + + ``` + curl -X POST 'http://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token' + --header 'Content-Type: application/x-www-form-urlencoded' + --data-urlencode 'grant_type=password' + --data-urlencode 'client_id=employee-management-service' + --data-urlencode 'username=user' + --data-urlencode 'password=test' + ``` + + To get a bearer-token in Postman, you have to follow the instructions in + [Postman-Documentation](https://documenter.getpostman.com/view/7294517/SzmfZHnd). + """) .version("0.1")) .addSecurityItem(new SecurityRequirement().addList(securitySchemeName)) .components( -- 2.45.2 From 80438dc6c10521b4b097876ba9e0ee50f9c19162 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 10:01:42 +0200 Subject: [PATCH 04/11] build: update dependencies versions in build.gradle.kts --- build.gradle.kts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 75e4316..5e31d8a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -62,10 +62,10 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-validation") - implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui") + implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0") implementation("org.springframework.boot:spring-boot-starter-security") - implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server") - implementation("org.springframework.boot:spring-boot-starter-oauth2-client") + implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server:3.3.4") + implementation("org.springframework.boot:spring-boot-starter-oauth2-client:3.3.4") testImplementation("com.h2database:h2") testImplementation("org.springframework.boot:spring-boot-starter-test") -- 2.45.2 From db5c629080cc390d169a0742b497643a2d9c4900 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 10:07:09 +0200 Subject: [PATCH 05/11] build: Update springDocVersion variable in build.gradle.kts --- build.gradle.kts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 5e31d8a..39d46e0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -58,11 +58,13 @@ repositories { mavenCentral() } +val springDocVersion = "2.6.0" + dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-validation") - implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0") + implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:$springDocVersion") implementation("org.springframework.boot:spring-boot-starter-security") implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server:3.3.4") implementation("org.springframework.boot:spring-boot-starter-oauth2-client:3.3.4") -- 2.45.2 From 2268c377617d1faa98e0df2b6b5a4c3623374eb6 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 10:11:24 +0200 Subject: [PATCH 06/11] build: Update OAuth2 version in build.gradle.kts --- build.gradle.kts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 39d46e0..d15cf48 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -59,6 +59,7 @@ repositories { } val springDocVersion = "2.6.0" +val oauth2Version = "3.3.4" dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") @@ -66,8 +67,8 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-validation") implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:$springDocVersion") implementation("org.springframework.boot:spring-boot-starter-security") - implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server:3.3.4") - implementation("org.springframework.boot:spring-boot-starter-oauth2-client:3.3.4") + implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server:$oauth2Version") + implementation("org.springframework.boot:spring-boot-starter-oauth2-client:$oauth2Version") testImplementation("com.h2database:h2") testImplementation("org.springframework.boot:spring-boot-starter-test") -- 2.45.2 From b586d4e3129c67afe365c637aa1ba1024480b66a Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 10:14:43 +0200 Subject: [PATCH 07/11] refactor(HelloController): simplify collection conversion to list --- src/main/java/de/szut/lf8_starter/hello/HelloController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/szut/lf8_starter/hello/HelloController.java b/src/main/java/de/szut/lf8_starter/hello/HelloController.java index 0f665f2..c846477 100644 --- a/src/main/java/de/szut/lf8_starter/hello/HelloController.java +++ b/src/main/java/de/szut/lf8_starter/hello/HelloController.java @@ -58,7 +58,7 @@ public class HelloController { .readAll() .stream() .map(e -> this.helloMapper.mapToGetDto(e)) - .collect(Collectors.toList()); + .toList(); } @Operation(summary = "deletes a Hello by id") -- 2.45.2 From 5e7e8cd3542baa29c5efcd71339b1fe0b18def0e Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 10:19:56 +0200 Subject: [PATCH 08/11] refactor(HelloController): simplify collection conversion --- src/main/java/de/szut/lf8_starter/hello/HelloController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/szut/lf8_starter/hello/HelloController.java b/src/main/java/de/szut/lf8_starter/hello/HelloController.java index c846477..5755a9e 100644 --- a/src/main/java/de/szut/lf8_starter/hello/HelloController.java +++ b/src/main/java/de/szut/lf8_starter/hello/HelloController.java @@ -94,6 +94,6 @@ public class HelloController { .findByMessage(message) .stream() .map(e -> this.helloMapper.mapToGetDto(e)) - .collect(Collectors.toList()); + .toList(); } } -- 2.45.2 From 37ab14d33e4bd53b5fdccc40d57d261d4c2147eb Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 10:23:43 +0200 Subject: [PATCH 09/11] refactor(security): simplify logout method parameters --- .../de/szut/lf8_starter/security/KeycloakLogoutHandler.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/szut/lf8_starter/security/KeycloakLogoutHandler.java b/src/main/java/de/szut/lf8_starter/security/KeycloakLogoutHandler.java index 8555ef9..3051a80 100644 --- a/src/main/java/de/szut/lf8_starter/security/KeycloakLogoutHandler.java +++ b/src/main/java/de/szut/lf8_starter/security/KeycloakLogoutHandler.java @@ -25,10 +25,10 @@ public class KeycloakLogoutHandler implements LogoutHandler { @Override public void logout(HttpServletRequest request, HttpServletResponse response, Authentication auth) { - logout(request, auth); + logout(auth); } - public void logout(HttpServletRequest request, Authentication auth) { + public void logout(Authentication auth) { logoutFromKeycloak((OidcUser) auth.getPrincipal()); } -- 2.45.2 From 21dd134dd6783285ead2dc16c692a5b48f71fc45 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 10:29:16 +0200 Subject: [PATCH 10/11] refactor(security): remove unused KeycloakLogoutHandler dependency --- .../szut/lf8_starter/security/KeycloakSecurityConfig.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java b/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java index 7ff32cc..f64bdd6 100644 --- a/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java +++ b/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java @@ -29,14 +29,10 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher; @EnableWebSecurity class KeycloakSecurityConfig { - private static final String GROUPS = "groups"; private static final String REALM_ACCESS_CLAIM = "realm_access"; private static final String ROLES_CLAIM = "roles"; - private final KeycloakLogoutHandler keycloakLogoutHandler; - - KeycloakSecurityConfig(KeycloakLogoutHandler keycloakLogoutHandler) { - this.keycloakLogoutHandler = keycloakLogoutHandler; + KeycloakSecurityConfig() { } @Bean -- 2.45.2 From 981f63b2474e9983b687b6604851322842b20f07 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 10:33:26 +0200 Subject: [PATCH 11/11] refactor(HelloController): remove unused methods and comments --- .../lf8_starter/hello/HelloController.java | 34 ------------------- 1 file changed, 34 deletions(-) diff --git a/src/main/java/de/szut/lf8_starter/hello/HelloController.java b/src/main/java/de/szut/lf8_starter/hello/HelloController.java index 5755a9e..2ca47da 100644 --- a/src/main/java/de/szut/lf8_starter/hello/HelloController.java +++ b/src/main/java/de/szut/lf8_starter/hello/HelloController.java @@ -45,22 +45,6 @@ public class HelloController { return this.helloMapper.mapToGetDto(helloEntity); } - @Operation(summary = "delivers a list of hellos") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "list of hellos", - content = {@Content(mediaType = "application/json", - schema = @Schema(implementation = HelloGetDto.class))}), - @ApiResponse(responseCode = "401", description = "not authorized", - content = @Content)}) - @GetMapping - public List findAll() { - return this.service - .readAll() - .stream() - .map(e -> this.helloMapper.mapToGetDto(e)) - .toList(); - } - @Operation(summary = "deletes a Hello by id") @ApiResponses(value = { @ApiResponse(responseCode = "204", description = "delete successful"), @@ -78,22 +62,4 @@ public class HelloController { this.service.delete(entity); } } - - @Operation(summary = "find hellos by message") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "List of hellos who have the given message", - content = {@Content(mediaType = "application/json", - schema = @Schema(implementation = HelloGetDto.class))}), - @ApiResponse(responseCode = "404", description = "qualification description does not exist", - content = @Content), - @ApiResponse(responseCode = "401", description = "not authorized", - content = @Content)}) - @GetMapping("/findByMessage") - public List findAllEmployeesByQualification(@RequestParam String message) { - return this.service - .findByMessage(message) - .stream() - .map(e -> this.helloMapper.mapToGetDto(e)) - .toList(); - } } -- 2.45.2