Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adicionado handler, swagger e dockerfile #8

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions DockerFile
Original file line number Diff line number Diff line change
@@ -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"]
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> handleResourceNotFoundException(ResourceNotFoundException ex){
return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
}

@ExceptionHandler(ConfigDataException.class)
public ResponseEntity<String> handleConflictException(ConflictException ex){
return new ResponseEntity<>(ex.getMessage(), HttpStatus.CONFLICT);
}

@ExceptionHandler(UnauthorizedException.class)
public ResponseEntity<String> handleUnauthorizedException(UnauthorizedException ex){
return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down
Loading