Coder Social home page Coder Social logo

kuldeepsingh99 / microservice-authorization-sidecar-proxy Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 1001 KB

Microservice Authorization with Side car proxy using Spring Security oAuth2

Dockerfile 3.56% Java 96.44%
authentication authorization authorization-server authorizationservice keycloak keycloak-server keycloak-server-spi kubernetes-deployment microservice microservices sidecar-container sidecar-injector sidecar-proxy spring-boot

microservice-authorization-sidecar-proxy's Introduction

Microservice Authorization with SideCar Proxy using Spring Security oAuth 2

In this example, I will show you how to handle Authorization for the microservices using Side Car.

There are other ways to handle Authorization like

  1. API Gateway
  2. Istio Side Car Container
  3. Microservice itself handles the Authorization
  4. Preparing a library that handles Authorization

But every solution has pros and cons as per project needs, like for example if we develop a library in Java, it can only be used for microservices that are developed in Java, if in the organization we have Nodejs based microservice, then we have developed a similar library in node.

Prerequisites

  • Java 17
  • Docker compose or colima
  • Keycloak 21

Keycloak

Run this command to start the keycloak Instance

docker run -it -p "8081:8080" -e 'KEYCLOAK_ADMIN=admin' -e 'KEYCLOAK_ADMIN_PASSWORD=admin' quay.io/keycloak/keycloak:21.0.2 start-dev

Login to keycloak with URL - http://localhost:8080/ with username - admin and password - admin

Step 1:- Create Realm

Create Realm


Create Realm


Step 2:- Create Client

Create Realm


Create Realm


Create Realm


Make Sure the Service Account is enabled

Create Realm


Create all the necessary Roles for the client

Create Realm


Create Realm


Map all the roles with the Service Account

Create Realm


Create Realm


Now Try to get the access token and view the roles

Create Realm


Create Realm


Deploying Service

Make sure Kubernetes is enabled

Step 1 - Compile the customer and proxy project

mvn package

Step 2 - Create images for customer and proxy project

docker build -t customer:latest .
docker build -t proxy:latest .

Step 3 - Deploy the yml file

Run the file to deploy services

kubectl apply -f deploy.yml

the most important part of the file is the container section

containers:
        - name: ms-customer
          image: customer:latest
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 8085
        - name: proxy
          image: proxy:latest
          imagePullPolicy: IfNotPresent
          env:
            - name: ALLOWED_URLS
              value: "/api/v1/customer:USER,/api/v2/customer:USER,/api/v3/customer:ADMIN,/api/v4/customer:MANAGER"
            - name: CLIENT
              value: "demo-client"
            - name: SERVICE_PORT
              value: "8085"
          ports:
            - name: http
              containerPort: 9090

Here we can see we have two containers

  • main
    • Main container is running on port 8085
  • proxy
    • proxy container is running on port 9090

    • Proxy has three environment variables ALLOWED_URLS, CLIENT and SERVICE_PORT

    • In ALLOWED_URLS we specify the URL and Role Mapping ex. This URL /api/v1/customer can only be accessed by someone who has USER Role.

    • CLIENT is the name of the keycloak client where Roles are mapped with the service account.

    • SERVICE_PORT is the port on which main container is running, this is required because on this port only Side car container will forward the request.

microservice-authorization-sidecar-proxy's People

Contributors

kuldeepsingh99 avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

sunilpentapati

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.