Skip to content

Instantly share code, notes, and snippets.

View thiagofa's full-sized avatar

Thiago Faria de Andrade thiagofa

View GitHub Profile
@thiagofa
thiagofa / algafood-openapi2.yaml
Created August 14, 2020 23:06
Documento em OpenAPI 2 da API do AlgaFood (projeto desenvolvido no curso Especialista Spring REST)
swagger: '2.0'
info:
description: API aberta para clientes e restaurantes.
version: '1'
title: AlgaFood API
contact:
name: AlgaWorks
url: 'https://www.algaworks.com'
email: [email protected]
host: 'localhost:8080'
@thiagofa
thiagofa / login.html
Last active November 14, 2022 19:05
Templates de páginas de segurança do projeto AlgaFood
<!doctype html>
<html lang="pt" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Acesso à AlgaFood</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
@thiagofa
thiagofa / spring-security-oauth2-oauth_client_details-schema.sql
Last active September 19, 2024 21:13
DDL da tabela de clientes OAuth2 da implementação JDBC padrão do Spring Security OAuth2
create table oauth_client_details (
client_id varchar(255),
resource_ids varchar(256),
client_secret varchar(256),
scope varchar(256),
authorized_grant_types varchar(256),
web_server_redirect_uri varchar(256),
authorities varchar(256),
access_token_validity integer,
refresh_token_validity integer,
@thiagofa
thiagofa / AuthorizationServerConfig.java
Last active October 15, 2022 16:58
Suporte a PKCE com Authorization Code no Spring Security OAuth2
// Parte da configuração do Authorization Server, para adicionar o TokenGranter customizado
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints.tokenGranter(tokenGranter(endpoints));
}
@thiagofa
thiagofa / CorsConfig.java
Created December 27, 2019 19:49
Configuração de CORS no Authorization Server com CorsFilter
// Fonte: https://spring.io/blog/2015/06/08/cors-support-in-spring-framework#filter-based-cors-support
import java.util.Collections;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
@thiagofa
thiagofa / pom.xml
Created December 21, 2019 03:14
Dependências para resolver ClassNotFoundException (javax.xml.bind.JAXBException) com Spring Security OAuth2 e Java 11
<!--
Spring Security OAuth2 com Java 11+ lança exception:
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
Em um projeto com Spring Boot, adicione as dependências abaixo para resolver o problema.
Leia também: https://stackoverflow.com/questions/52502189/java-11-package-javax-xml-bind-does-not-exist
-->
<dependency>
<groupId>javax.xml.bind</groupId>
@thiagofa
thiagofa / HalCustomMediaTypeEnabler.java
Created December 15, 2019 00:48
Resolve o problema de custom media types com Spring HATEOAS (com formato HAL)
import java.util.Arrays;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.MediaTypes;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
@thiagofa
thiagofa / TomcatCustomizer.java
Created October 30, 2019 21:08
Customiza Tomcat embedado do Spring Boot para aceitar colchetes nas URLs
// Referências:
// - https://stackoverflow.com/a/53613678
// - https://tomcat.apache.org/tomcat-8.5-doc/config/http.html
// - https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-configure-webserver
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;
@Component
@thiagofa
thiagofa / ResourceUtils.java
Created October 17, 2019 20:52
Classe utilitária que retorna a string do conteúdo de um arquivo (recurso) do classpath
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.springframework.util.StreamUtils;
public class ResourceUtils {
public static String getContentFromResource(String resourceName) {
try {
@thiagofa
thiagofa / DatabaseCleaner.java
Last active October 6, 2024 23:35
Componente Spring para limpar os dados de todas as tabelas de um banco de teste (exceto a tabela de histórico do Flyway)
// Baseado em: https://brightinventions.pl/blog/clear-database-in-spring-boot-tests/
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;