Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 0fcfe13

Browse files
1. **Security Configuration**: The SecurityConfiguration class has been updated to include additional security settings:
* `.pathMatchers("/users/v3/**").permitAll()` and `.pathMatchers("/swagger-ui/**").permitAll()` allow access to certain routes without authentication. * `.anyExchange().authenticated()` ensures that all other requests are authenticated. 2. **OpenAPI Configuration**: A new `OpenAPIConfig` class has been added to define the OpenAPI configuration for the project: * The `customOpenAPI()` method returns a custom `OpenAPI` instance with additional metadata, such as the title, description, and version. * The `components` section defines the security scheme for the API, which includes a "bearer-key" scheme with HTTP bearer format and header location. 3. **Security Scheme**: A new `SecurityScheme` instance has been added to define the security scheme: * `.type(SecurityScheme.Type.HTTP)` specifies that the security scheme uses HTTP headers. * `.scheme("bearer")` specifies that the security scheme uses the Bearer token. * `.bearerFormat("JWT")` specifies that the token format is JSON Web Tokens (JWT). * `.in(SecurityScheme.In.HEADER)` specifies that the token should be sent in the `Authorization` header.
1 parent 9736221 commit 0fcfe13

9 files changed

Lines changed: 107 additions & 1 deletion

File tree

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@
8787
<artifactId>spring-security-test</artifactId>
8888
<scope>test</scope>
8989
</dependency>
90+
<dependency>
91+
<groupId>org.springdoc</groupId>
92+
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
93+
<version>2.8.5</version>
94+
</dependency>
9095
</dependencies>
9196
<dependencyManagement>
9297
<dependencies>

src/main/java/br/com/imaginer/resqueueuser/adapter/controller/AuthController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import br.com.imaginer.resqueueuser.adapter.gateway.keycloak.login.LoginService;
66
import br.com.imaginer.resqueueuser.adapter.gateway.keycloak.refresh.RefreshTokenRequest;
77
import br.com.imaginer.resqueueuser.adapter.gateway.keycloak.refresh.RefreshTokenService;
8+
import io.swagger.v3.oas.annotations.tags.Tag;
89
import org.springframework.http.ResponseEntity;
910
import org.springframework.web.bind.annotation.*;
1011
import reactor.core.publisher.Mono;
1112

13+
@Tag(name = "Auth")
1214
@RestController
1315
@RequestMapping("/auth")
1416
public class AuthController {

src/main/java/br/com/imaginer/resqueueuser/adapter/controller/AuthTokenController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package br.com.imaginer.resqueueuser.adapter.controller;
22

33
import br.com.imaginer.resqueueuser.adapter.gateway.keycloak.security.AuthTokenService;
4+
import io.swagger.v3.oas.annotations.Operation;
5+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
6+
import io.swagger.v3.oas.annotations.tags.Tag;
7+
import lombok.extern.slf4j.Slf4j;
48
import org.springframework.web.bind.annotation.GetMapping;
59
import org.springframework.web.bind.annotation.RequestMapping;
610
import org.springframework.web.bind.annotation.RestController;
711
import reactor.core.publisher.Mono;
812

13+
@Slf4j
914
@RestController
1015
@RequestMapping("/auth")
1116
public class AuthTokenController {
@@ -16,9 +21,10 @@ public AuthTokenController(AuthTokenService authTokenService) {
1621
this.authTokenService = authTokenService;
1722
}
1823

24+
@Operation(hidden = true)
1925
@GetMapping("/token")
2026
public Mono<String> getAuthToken() {
2127
return authTokenService.getAccessToken()
22-
.doOnNext(token -> System.out.println("Token JWT: " + token));
28+
.doOnNext(token -> log.info("Token JWT: " + token));
2329
}
2430
}

src/main/java/br/com/imaginer/resqueueuser/adapter/controller/KeycloakCreateUserController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import br.com.imaginer.resqueueuser.adapter.gateway.keycloak.createuser.CreateUserRequestDTO;
44
import br.com.imaginer.resqueueuser.adapter.gateway.keycloak.createuser.CreateUserService;
5+
import io.swagger.v3.oas.annotations.Operation;
6+
import io.swagger.v3.oas.annotations.tags.Tag;
57
import org.springframework.http.HttpStatus;
68
import org.springframework.web.bind.annotation.*;
79
import reactor.core.publisher.Mono;
810

11+
@Tag(name = "Create User")
912
@RestController
1013
@RequestMapping("/users")
1114
public class KeycloakCreateUserController {
@@ -16,6 +19,7 @@ public KeycloakCreateUserController(CreateUserService createUserService) {
1619
this.createUserService = createUserService;
1720
}
1821

22+
@Operation(description = "Create new user.")
1923
@PostMapping("/create")
2024
@ResponseStatus(HttpStatus.CREATED)
2125
public Mono<Void> createUser(@RequestBody CreateUserRequestDTO request) {

src/main/java/br/com/imaginer/resqueueuser/adapter/controller/KeycloakGetUserController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import br.com.imaginer.resqueueuser.adapter.gateway.keycloak.getuser.GetUserResponse;
44
import br.com.imaginer.resqueueuser.adapter.gateway.keycloak.getuser.GetUserService;
5+
import io.swagger.v3.oas.annotations.Operation;
6+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
7+
import io.swagger.v3.oas.annotations.tags.Tag;
58
import org.springframework.http.ResponseEntity;
69
import org.springframework.web.bind.annotation.*;
710
import reactor.core.publisher.Mono;
811

12+
@Tag(name = "Keycloak User Details")
913
@RestController
1014
@RequestMapping("/users")
1115
public class KeycloakGetUserController {
@@ -16,6 +20,7 @@ public KeycloakGetUserController(GetUserService getUserService) {
1620
this.getUserService = getUserService;
1721
}
1822

23+
@Operation(description = "Get user details by email.", security = { @SecurityRequirement(name = "bearer-key") })
1924
@GetMapping
2025
public Mono<ResponseEntity<GetUserResponse>> getUser(@RequestParam String email) {
2126
return getUserService.getUser(email)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package br.com.imaginer.resqueueuser.infrastructure.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.cors.CorsConfiguration;
6+
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
7+
import org.springframework.web.cors.reactive.CorsWebFilter;
8+
9+
import java.util.List;
10+
11+
@Configuration
12+
public class CorsConfig {
13+
14+
@Bean
15+
public CorsWebFilter corsWebFilter() {
16+
CorsConfiguration corsConfig = new CorsConfiguration();
17+
corsConfig.setAllowedOrigins(List.of("http://localhost:8080", "https://gateway.imaginer.com.br"));
18+
corsConfig.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
19+
corsConfig.setAllowedHeaders(List.of("*"));
20+
corsConfig.setAllowCredentials(true);
21+
22+
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
23+
source.registerCorsConfiguration("/**", corsConfig);
24+
25+
return new CorsWebFilter(source);
26+
}
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package br.com.imaginer.resqueueuser.infrastructure.config;
2+
3+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
4+
5+
import io.swagger.v3.oas.models.Components;
6+
import io.swagger.v3.oas.models.OpenAPI;
7+
import io.swagger.v3.oas.models.info.Info;
8+
import io.swagger.v3.oas.models.security.SecurityScheme;
9+
import io.swagger.v3.oas.models.servers.Server;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
13+
import java.util.List;
14+
15+
@OpenAPIDefinition
16+
@Configuration
17+
public class OpenAPIConfig {
18+
19+
@Bean
20+
public OpenAPI customOpenAPI() {
21+
return new OpenAPI().info(new Info()
22+
.title("User API Service")
23+
.description("User API Service")
24+
.version("1.0.0"))
25+
.components(
26+
new Components()
27+
.addSecuritySchemes(
28+
"bearer-key",
29+
new SecurityScheme()
30+
.type(SecurityScheme.Type.HTTP)
31+
.scheme("bearer")
32+
.bearerFormat("JWT")
33+
.in(SecurityScheme.In.HEADER)
34+
.name("Authorization")
35+
))
36+
.servers(List.of(
37+
new Server().url("http://localhost:8080").description("Localhost (Desenvolvimento)"),
38+
new Server().url("https://gateway.imaginer.com.br").description("Servidor de Produção")
39+
));
40+
}
41+
}

src/main/java/br/com/imaginer/resqueueuser/infrastructure/config/security/SecurityConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
3333
.pathMatchers("/actuator/**").permitAll()
3434
.pathMatchers("/users/create").permitAll()
3535
.pathMatchers("/auth/login/**").permitAll()
36+
.pathMatchers("/users/v3/**").permitAll()
37+
.pathMatchers("/swagger-ui/**").permitAll()
38+
.pathMatchers("/webjars/**").permitAll()
3639
.anyExchange().authenticated()
3740

3841
).oauth2ResourceServer(oauth2 -> oauth2
@@ -46,4 +49,5 @@ public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
4649
public ReactiveJwtDecoder jwtDecoder() {
4750
return ReactiveJwtDecoders.fromIssuerLocation(jwtProperties.getIssuerUri());
4851
}
52+
4953
}

src/main/resources/application.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,15 @@ keycloak:
2222
base-url: ${AUTH_BASE_URL:http://localhost:9000}
2323
client-id: resqueue-client
2424
client-secret: ${AUTH_RESQUEUE_CLIENT_SECRET}
25+
26+
springdoc:
27+
api-docs:
28+
enabled: true
29+
path: /users/v3/api-docs
30+
swagger-ui:
31+
enabled: true
32+
path: /docs
33+
config-url: /users/v3/api-docs/swagger-config
34+
urls:
35+
- name: users-service
36+
url: /users/v3/api-docs

0 commit comments

Comments
 (0)