Giter VIP home page Giter VIP logo

spring-cloud-gateway-with-oauth2's Introduction

Gateway oauth2

Gateway implementado con Spring Cloud Gateway y Spring Security basado en oauth2.

Authorization Grant implementados:

API Gateway - Resource Owner Password Credentials

  1. Request resource (sin access-token)
  2. Request access-token
  3. Response access-token
  4. Request resource (con access-token)

Gateway

Implementacion de gateway basada en Spring Cloud Gateway. Es responsable de brindar un unico punto de acceso a los recursos desplegados en una red de externa/interna (outbound/inbound). Gestiona todo lo relacionado con seguridad, enrutamiento, reintentos, caching, etc.

Los predicados usados para matchear las rutas definidas, asi como los filtros a aplicar a los request/response pueden encontrarse aqui

Existe un global filter responsable de enrutar las rutas bajo los schemas http/https usando webclient

    @Bean 
	public WebClientHttpRoutingFilter webClientHttpRoutingFilter(WebClient webClient, ObjectProvider<List<HttpHeadersFilter>> headersFilters) { 
		return new WebClientHttpRoutingFilter(webClient, headersFilters);
	}

Security

Manejada por Spring Security e integrada con oauth2 usando Resource Owner Password Credentials y Client Credentials

Authorization Server

Componente encargado de generar access/refresh tokens para poder acceder a los recursos protegidos sobre el Resource Server

En esta demo el auth server se implemento sobre Okta. Hay algunas guias muy interesantes acerca de como desplegar y configurar un auth server sobre Okta

Los detalles del provider oauth2 configurado se puede encontrar aqui

Client

Componente encargado de consumir los recursos servidos por el Resource Server en nombre del Resource Owner

Esta implementacion esta basada en Reactive para poder integrarse con Spring Cloud Gateway (ver SecurityConfig)

El webclient utilizado por el WebClientHttpRoutingFilter esta configurado con oauth2 a traves de un ExchangeFilterFunction.

    @Bean
    WebClient webClient(ReactiveOAuth2AuthorizedClientManager authorizedClientManager) {
        ServerOAuth2AuthorizedClientExchangeFilterFunction oauth =
                new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
        oauth.setDefaultOAuth2AuthorizedClient(true);
        oauth.setDefaultClientRegistrationId(client-registration-id);
        return WebClient.builder()
                .filter(oauth)
                .build();
    }

De esta manera cada peticion que necesite ser enrutada por el WebClientHttpRoutingFilter se hara a traves de este webclient, y el mismo gestionara los tokens de acceso a traves de un ReactiveOAuth2AuthorizedClientManager

    @Bean
    public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
            ReactiveClientRegistrationRepository clientRegistrationRepository,
            ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {

        ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
                ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
                        //.refreshToken()
                		.password()
                        .clientCredentials()
                        .build();
        
        DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
                new DefaultReactiveOAuth2AuthorizedClientManager(
                        clientRegistrationRepository, authorizedClientRepository);
        authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

        // For the `password` grant, the `username` and `password` are supplied via request parameters,
        // so map it to `OAuth2AuthorizationContext.getAttributes()`.
        authorizedClientManager.setContextAttributesMapper(contextAttributesMapper());

        return authorizedClientManager;
    }

Los detalles de la configuracion de los clientes oauth2 se encuentra aqui

Resource Server

Componente encargado de servir los recursos protegidos y desplegado en http://resource:9000

La configuracion para validar la validez de los access tokens puede encontrarse aqui

Para acceder al endpoint /resource el cliente debera presentar un JWT con el scope 'openid' o 'my_scope' (ver SecurityConfig)

Pruebas

Hay un set de pruebas que puede utilizarse para:

  1. Probar el acceso a los recursos protegidos en http://resource:9000/resource a traves del gateway
  2. Probar el acceso a los recursos protegidos en http://resource:9000/resource sin pasar por el gateway

Run

Para levantar el resource server y el gateway (client) ejecutar docker-compose up -d

Pre-requisitos

Un authorization server y dos aplicaciones clientes registradas (una basada en client credentials y otra en password grant)

Configuracion

Por medio de la config client-registration-id es posible configurar cual cliente utilizar para la autorizacion

Referencias

spring-cloud-gateway-with-oauth2's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.