From ac0061b3a6089625249976cc8fd1a01c9ba776c8 Mon Sep 17 00:00:00 2001 From: Guilherme dos Santos Date: Sat, 18 Jan 2025 20:01:53 -0300 Subject: [PATCH] adicionado handler, swagger e dockerfile --- DockerFile | 9 ++++++ build.gradle | 4 ++- docker-compose.yml | 22 ++++++++++++++ .../business/converter/UsuarioConverter.java | 12 +++++--- .../controller/GlobalExceptionHandler.java | 29 +++++++++++++++++++ .../controller/UsuarioController.java | 5 ++++ .../exceptions/UnauthorizedException.java | 15 ++++++++++ .../security/SecurityConfig.java | 7 +++++ 8 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 DockerFile create mode 100644 docker-compose.yml create mode 100644 src/main/java/com/guilherme/agendador_tarefas/controller/GlobalExceptionHandler.java create mode 100644 src/main/java/com/guilherme/agendador_tarefas/infrastructure/exceptions/UnauthorizedException.java diff --git a/DockerFile b/DockerFile new file mode 100644 index 0000000..dbf648b --- /dev/null +++ b/DockerFile @@ -0,0 +1,9 @@ +FROM openjdk:17-jdk-alpine + +WORKDIR /app + +COPY build/libs/agendador-tarefas-0.0.1-SNAPSHOT.jar /app/agendador-tarefas.jar + +EXPOSE 8080 + +CMD ["java", "-jar", "/app/agendador-tarefas.jar"] \ No newline at end of file diff --git a/build.gradle b/build.gradle index 64735e4..1977365 100644 --- a/build.gradle +++ b/build.gradle @@ -28,7 +28,9 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'io.jsonwebtoken:jjwt-api:0.12.6' - runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.6' + implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.3' + + runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.6' runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.6' compileOnly 'org.projectlombok:lombok' runtimeOnly 'org.postgresql:postgresql' diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0e3f43b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,22 @@ +version: '3.8' + +services: + app: + build: . + ports: + - "8080:8080" + environment: + SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/db_usuario + SPRING_DATASOURCE_USERNAME: postgres + SPRING_DATASOURCE_PASSWORD: 082418 + depends_on: + - db + + db: + image: postgres:latest + environment: + POSTGRES_DB: db_usuario + POSTGRES_USER: postgres + POSTGRES_PASSWORD: 1234 + ports: + - "5432:5432" \ No newline at end of file diff --git a/src/main/java/com/guilherme/agendador_tarefas/business/converter/UsuarioConverter.java b/src/main/java/com/guilherme/agendador_tarefas/business/converter/UsuarioConverter.java index 56497e7..e87dd06 100644 --- a/src/main/java/com/guilherme/agendador_tarefas/business/converter/UsuarioConverter.java +++ b/src/main/java/com/guilherme/agendador_tarefas/business/converter/UsuarioConverter.java @@ -19,8 +19,10 @@ public Usuario paraUsuario(UsuarioDTO usuarioDTO) { .nome(usuarioDTO.getNome()) .email(usuarioDTO.getEmail()) .senha(usuarioDTO.getSenha()) - .enderecos(paraListaEndereco(usuarioDTO.getEnderecos())) - .telefones(paraListaTelefones(usuarioDTO.getTelefones())) + .enderecos(usuarioDTO.getEnderecos() != null ? + paraListaEndereco(usuarioDTO.getEnderecos()) : null) + .telefones(usuarioDTO.getTelefones() != null ? + paraListaTelefones(usuarioDTO.getTelefones()) : null) .build(); } @@ -61,8 +63,10 @@ public UsuarioDTO paraUsuarioDTO(Usuario usuarioDTO){ .nome(usuarioDTO.getNome()) .email(usuarioDTO.getEmail()) .senha(usuarioDTO.getSenha()) - .enderecos(paraListaEnderecoDTO(usuarioDTO.getEnderecos())) - .telefones(paraListaTelefonesDTO(usuarioDTO.getTelefones())) + .enderecos(usuarioDTO.getEnderecos() != null ? + paraListaEnderecoDTO(usuarioDTO.getEnderecos()) : null) + .telefones(usuarioDTO.getTelefones() != null ? + paraListaTelefonesDTO(usuarioDTO.getTelefones()) : null) .build(); } diff --git a/src/main/java/com/guilherme/agendador_tarefas/controller/GlobalExceptionHandler.java b/src/main/java/com/guilherme/agendador_tarefas/controller/GlobalExceptionHandler.java new file mode 100644 index 0000000..17ff97e --- /dev/null +++ b/src/main/java/com/guilherme/agendador_tarefas/controller/GlobalExceptionHandler.java @@ -0,0 +1,29 @@ +package com.guilherme.agendador_tarefas.controller; + +import com.guilherme.agendador_tarefas.infrastructure.exceptions.ConflictException; +import com.guilherme.agendador_tarefas.infrastructure.exceptions.ResourceNotFoundException; +import com.guilherme.agendador_tarefas.infrastructure.exceptions.UnauthorizedException; +import org.springframework.boot.context.config.ConfigDataException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(ResourceNotFoundException.class) + public ResponseEntity handleResourceNotFoundException(ResourceNotFoundException ex){ + return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(ConfigDataException.class) + public ResponseEntity handleConflictException(ConflictException ex){ + return new ResponseEntity<>(ex.getMessage(), HttpStatus.CONFLICT); + } + + @ExceptionHandler(UnauthorizedException.class) + public ResponseEntity handleUnauthorizedException(UnauthorizedException ex){ + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); + } +} diff --git a/src/main/java/com/guilherme/agendador_tarefas/controller/UsuarioController.java b/src/main/java/com/guilherme/agendador_tarefas/controller/UsuarioController.java index 73f5807..86d2b4b 100644 --- a/src/main/java/com/guilherme/agendador_tarefas/controller/UsuarioController.java +++ b/src/main/java/com/guilherme/agendador_tarefas/controller/UsuarioController.java @@ -5,6 +5,9 @@ import com.guilherme.agendador_tarefas.business.DTO.TelefoneDTO; import com.guilherme.agendador_tarefas.business.DTO.UsuarioDTO; import com.guilherme.agendador_tarefas.infrastructure.security.JwtUtil; +import com.guilherme.agendador_tarefas.infrastructure.security.SecurityConfig; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationManager; @@ -15,6 +18,8 @@ @RestController @RequestMapping("/usuario") @RequiredArgsConstructor +@Tag(name = "Tarefas", description = "Cadastro tarefas de usuários") +@SecurityRequirement(name = SecurityConfig.SECURITY_SCHEME) public class UsuarioController { private final UsuarioService usuarioService; diff --git a/src/main/java/com/guilherme/agendador_tarefas/infrastructure/exceptions/UnauthorizedException.java b/src/main/java/com/guilherme/agendador_tarefas/infrastructure/exceptions/UnauthorizedException.java new file mode 100644 index 0000000..380be26 --- /dev/null +++ b/src/main/java/com/guilherme/agendador_tarefas/infrastructure/exceptions/UnauthorizedException.java @@ -0,0 +1,15 @@ +package com.guilherme.agendador_tarefas.infrastructure.exceptions; + +import javax.naming.AuthenticationException; + +public class UnauthorizedException extends AuthenticationException{ + + public UnauthorizedException(String mensagem){ + super(mensagem); + } + + public UnauthorizedException(String mensagem, Throwable throwable){ + super(mensagem, throwable); + } + +} diff --git a/src/main/java/com/guilherme/agendador_tarefas/infrastructure/security/SecurityConfig.java b/src/main/java/com/guilherme/agendador_tarefas/infrastructure/security/SecurityConfig.java index 7e45b76..616d9eb 100644 --- a/src/main/java/com/guilherme/agendador_tarefas/infrastructure/security/SecurityConfig.java +++ b/src/main/java/com/guilherme/agendador_tarefas/infrastructure/security/SecurityConfig.java @@ -1,5 +1,7 @@ package com.guilherme.agendador_tarefas.infrastructure.security; +import io.swagger.v3.oas.annotations.enums.SecuritySchemeType; +import io.swagger.v3.oas.annotations.security.SecurityScheme; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -18,8 +20,12 @@ @Configuration @EnableWebSecurity +@SecurityScheme(name = SecurityConfig.SECURITY_SCHEME, type = SecuritySchemeType.HTTP, + bearerFormat = "JWT", scheme = "bearer") public class SecurityConfig { + public static final String SECURITY_SCHEME = "bearerAuth"; + // Instâncias de JwtUtil e UserDetailsService injetadas pelo Spring private final JwtUtil jwtUtil; private final UserDetailsService userDetailsService; @@ -40,6 +46,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti http .csrf(AbstractHttpConfigurer::disable) // Desativa proteção CSRF para APIs REST (não aplicável a APIs que não mantêm estado) .authorizeHttpRequests(authorize -> authorize + .requestMatchers("/v3/api-docs/**","/swagger-ui/","/swagger-ui.html").permitAll() .requestMatchers("/usuario/login").permitAll() // Permite acesso ao endpoint de login sem autenticação .requestMatchers(HttpMethod.GET, "/auth").permitAll()// Permite acesso ao endpoint GET /auth sem autenticação .requestMatchers(HttpMethod.POST, "/usuario").permitAll() // Permite acesso ao endpoint POST /usuario sem autenticação