Skip to content

GabrielCzar/Spring-REST-API-with-OAuth2-and-JWT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring REST API with OAuth2 and JWT

Para executar a aplicação

  • Maven
mnv clean spring-boot:run
  • Docker
mvn clean package docker:build
mvn docker:start

Informações adicionais

Exemplos de request

  • Gerar token de acesso:
curl [CLIENT-ID]:[CLIENT-SECRET]@localhost:8080/oauth/token -d grant_type=password -d username=[USER] -d password=[PASSWORD]
{
  "access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiREEyRDUzMkQxQkVqd3RyZXNvdXJjZWlkIl0sInVzZXJfbmFtZSI6ImdhYnJpZWxjemFyIiwic2NvcGUiOlsicmVhZCIsIndyaXRlIl0sImV4cCI6MTcyNzMwNTQ3OCwiYXV0aG9yaXRpZXMiOlsiUk9MRV9VU0VSIl0sImp0aSI6ImY4MTJkYzBlLWZiZmItNDQ1My04ZDI2LTY5NmM2NWQ0MzA5ZiIsImNsaWVudF9pZCI6InNwcmluZy1yZXN0LW9hdXRoMi1qd3QifQ.ExobK5qYHzSxVpoPUvT8uQwBfZwsefYWsEjsxJopni0",
  "token_type":"bearer",
  "refresh_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiREEyRDUzMkQxQkVqd3RyZXNvdXJjZWlkIl0sInVzZXJfbmFtZSI6ImdhYnJpZWxjemFyIiwic2NvcGUiOlsicmVhZCIsIndyaXRlIl0sImF0aSI6ImY4MTJkYzBlLWZiZmItNDQ1My04ZDI2LTY5NmM2NWQ0MzA5ZiIsImV4cCI6MTUyOTg5NjQ5MCwiYXV0aG9yaXRpZXMiOlsiUk9MRV9VU0VSIl0sImp0aSI6IjZlNWMyNDRhLTU5NDQtNDNhMy05ZjJlLTkwMWQ1ZDFkNTJjYiIsImNsaWVudF9pZCI6InNwcmluZy1yZXN0LW9hdXRoMi1qd3QifQ.E3w8BWVdtFyw6eBwBokXIXRtE3-oxdwH0JgLf-13wfo",
  "expires_in":604800,
  "scope":"read write",
  "jti":"f812dc0e-fbfb-4453-8d26-696c65d4309f"
}
  • Renovar Token:
curl [CLIENT-ID]:[CLIENT-SECRET]@localhost:8080/oauth/token -d grant_type=refresh_token -d refresh_token=[REFRESH_TOKEN] 
  • Acessar recurso:
curl http://localhost:8080/api/users -H "Authorization: Bearer [ACCESS_TOKEN]"
[
  {
    "id":1,
    "username":"gabrielczar.adm",
    "firstName":"Gabriel",
    "lastName":"Czar",
    "authorities":[
      {
        "id":1,
        "name":"ROLE_ADMIN",
        "description":null,
        "authority":"ROLE_ADMIN"
      }
     ]
  },
  {
    "id":2,
    "username":"gabrielczar",
    "firstName":"Gabriel",
    "lastName":"Czar",
    "authorities":[
      {
        "id":2,
        "name":"ROLE_USER",
        "description":null,
        "authority":"ROLE_USER"
      }
    ]
  }
]