Blog
  • AI Gateway
  • AI Security
  • AIOps
  • API Security
  • API Gateway
    • API Management
    • API Development
    • API Design
    • Automation
    • Service Mesh
    • Insomnia
  1. Home
  2. Blog
  3. Engineering
  4. Dynamic Routing Based on JWT Token’s Claim with Kong Konnect
Engineering
November 3, 2022
5 min read

Dynamic Routing Based on JWT Token’s Claim with Kong Konnect

Shlomi Tubul
Topics
Kong KonnectJWTAPI Authentication
Share on Social

See Kong in action

Accelerate deployments, reduce vulnerabilities, and gain real-time visibility. 

Get a Demo

A common use case that is frequently requested is how to dynamically route requests based on authentication attributes. An example of this technique is routing requests to relevant upstream services based on claims contained in a JWT token.

Admins would like all their clients to go to the same URI as it makes the implementation easier. But behind the scenes they might need to tailor specific configuration/applications for them, hence routing their request based on their identity.

Kong Gateway is typically ran with a set of ancillary services, including a backend datastore, Kong Manager, Analytics, and Developer Portal. Traditionally, users of Kong Gateway have been required to self host these additional services to experience the full capabilities of the Kong platform.

In this post, we’ll use Kong Konnect, the new Kong SaaS API platform. Konnect hosts the Kong platform’s management capabilities (backend datastore, Kong Manager, Analytics, Developer Portal, and more) in a multi-cloud environment. This frees the customer from managing these ancillary services and allows them to focus on hosting the data planes (aka, proxy runtimes) and building their business logic.

As we’ve seen in a previous blog post, Kong thrives in routing requests dynamically. However, in this post, we’ll take things a step further by dynamically routing requests based on the authentication process.

While we’re going to be covering everything using Konnect, it’s important to highlight that you can use the same procedure below in your own self-managed environment, as the same configuration applies to all deployments.

To start, let’s outline the prerequisites:

  1. A Konnect account and a connected runtime. This is documented in the Get started with Konnect page, steps 1 and 2.
  2. A Konnect personal access token (PAT). To generate the token, log in to your account, click on your username, then “Personal access token”, then press the “Generate token” button. (See figure 1 below.)
  3. decK, installed locally, decK is a tool used to declaratively configure Kong Gateway. Using decK with Kong Konnect contains the instructions for connecting your local decK installation to your Konnect account.
  4. 3rd party Identity Provider (IdP). For this demo, I am using Keycloak. I’ve set up two users that will access the "/bank" route in our proxy port. The user bob will use the "great-discount" bank and suzy will use the "low-interest" bank. This off course can be implemented with any Identity provider support oauth2 you are using.

Figure 1

High Level Architecture

For our example use case, we are managing a banking application that serves different bank's customers.

In my example , all customers go to the same service address (http://kong.bankingservice.com) which is basically a DNS entry where the Kong service resides. Within Kong we will have the following configured:

  • A service — A representation of our application
  • A route — A route will let you expose how a service is being requested.

During our demo, we will show how we can route 2 requests that sent to the same route to 2 different paths based on authentication properties.

For our use case, we will use the authentication mechanism to determine which path needs to be used, based on the JWT claims of the authenticated user. The claim we’ll use will be labeled “bank”. An architectural diagram of the scenario is shown below:

Here is an example of a JWT token. The “bank” attribute contains the information we will use to route the request.

Setup

To make things easier to identify, we’ll create a simple web application that will just print the path of the requested URL. This will allow us to see where exactly the user has been redirected.

To make things as simple as possible, we’ll use a simple python script that creates a web server and prints the path. This python script is shown below:

To test our app , we can just run it:

Once the app is running, we can look at our browser (with the address of the local server) to see how it works:

So as we haven’t put our application behind Kong and the OIDC authentication, there is no bank name yet.

Implement configuration to Kong GW

To bulk load the Kong configuration, we will use a tool called decK, which will enable us to configure our complete setup with just one call.

For our use case, the declarative file (demo-deployment.yaml) looks as follows:

There are a couple of notes to highlight from our YAML file:

  • We created a service named "bank-demo service" which basically represents our backend application which is the simple python application.
  • We created a route with the path /bank to allow access to the service.
  • We attached the openid-connect plugin to the bank service. The plugin is configured to integrate with a keycloak demo instance. We have also transformed the "bank" claim in the JWT token to a header attribute, for routing purposes.
  • We attached the request-transformer-advanced plugin to replace the backend url with a new one that consists with the value of the "bank" header.\

To completely configure the gateway, we need one simple decK command:

For additional info on how to use decK with our Konnect Cloud offer, you can follow Kong's official documentation.

If we want to take a look on how this implementation looks in konnect UI:

Now, let’s test our application.

When we try to access our service through the runtime instance (in my case running locally on my laptop), we are redirected to keycloak for authentication. The first user we will try is Bob:

And the result is:

Now lets try with our other user, Suzy:

And the result is similar, but we are redirected to a new URL, as it was expected:

Summary

In this blog, we saw how easy it is to leverage Kong in order to create complex dynamic routing based on specific attributes (in this case, JWT token's claims).

All users have the same Login process, and according to their attributes they are routed to the relevant application/path for their service. This makes the maintenance of the deployed application much easier and cleaner.

In this demo, we used Kong’s Konnect (SaaS) which makes deployment and maintenance much easier. As Kong can be deployed in the vast majority of architectures (on-prem, cloud, hybrid, docker, k8s, etc), you can reproduce these capabilities no matter which form you choose.

Ready to try Kong Konnect?

Accelerate your journey to microservices, secure and govern APIs and services, and boost developer productivity with Kong Konnect, the easiest way to get started with Kong Enterprise

Start a Free Trial >

Developer agility meets compliance and security. Discover how Kong can help you become an API-first company.

Get a DemoStart for Free
Topics
Kong KonnectJWTAPI Authentication
Share on Social
Shlomi Tubul

Recommended posts

The Postman Data Breach: How to Stay Ahead with Kong

Kong Logo
EngineeringJanuary 1, 1970

On December 23, 2024, the security research team at CloudSEK completed a year-long investigation of the cloud-based API testing tool Postman. CloudSEK’s findings revealed that more than 30,000 publicly accessible Postman workspaces had been leakin

Adam Jiroun

Federated Deployments with Control Plane Groups

Kong Logo
EngineeringSeptember 24, 2025

In this blog post, we'll talk about the significant challenge of managing and governing a growing number of APIs across multiple teams in an organization — and how Control Plane Groups are a clear solution to avoid the chaos of inconsistent policies

Declan Keane

Unlocking API Analytics for Product Managers

Kong Logo
EngineeringSeptember 9, 2025

Meet Emily. She’s an API product manager at ACME, Inc., an ecommerce company that runs on dozens of APIs. One morning, her team lead asks a simple question: “Who’s our top API consumer, and which of your APIs are causing the most issues right now?”

Christian Heidenreich

Kong Acquires OpenMeter to Bring API and AI Monetization to the Agentic Era

Kong Logo
NewsSeptember 3, 2025

Today, we’re announcing that Kong has acquired  OpenMeter , the open source and SaaS leader for real-time usage metering and billing. OpenMeter’s capabilities will be integrated into Kong Konnect, enabling usage-based pricing, entitlements, and invo

Saju Pillai

Level Up Your Digital Health Platform with Kong, SMART on FHIR, Okta

Kong Logo
EngineeringSeptember 2, 2025

The healthcare industry is buzzing about FHIR (Fast Healthcare Interoperability Resources). Pronounced “fire,” this widely adopted data standard has been revolutionizing how healthcare information is exchanged. But building a truly modern, secure, a

Biswa Mohanty

Guide to API Testing: Understanding the Basics

Kong Logo
EngineeringSeptember 1, 2025

Behind every smooth user experience is a maze of APIs quietly handling requests, responses, and data flows. This makes APIs critical connectors that enable applications to communicate and share data seamlessly. When these vital conduits fail, the

Adam Bauman

AI Guardrails: Ensure Safe, Responsible, Cost-Effective AI Integration

Kong Logo
EngineeringAugust 25, 2025

As enterprises increasingly embed AI and Large Language Models (LLMs) into their digital experiences, enforcing robust AI guardrails becomes paramount to safeguard users, protect data, manage operational costs, and comply with regulatory and ethical

Jason Matis

Ready to see Kong in action?

Get a personalized walkthrough of Kong's platform tailored to your architecture, use cases, and scale requirements.

Get a Demo
Powering the API world

Increase developer productivity, security, and performance at scale with the unified platform for API management, AI gateways, service mesh, and ingress controller.

Sign up for Kong newsletter

Platform
Kong KonnectKong GatewayKong AI GatewayKong InsomniaDeveloper PortalGateway ManagerCloud GatewayGet a Demo
Explore More
Open Banking API SolutionsAPI Governance SolutionsIstio API Gateway IntegrationKubernetes API ManagementAPI Gateway: Build vs BuyKong vs PostmanKong vs MuleSoftKong vs Apigee
Documentation
Kong Konnect DocsKong Gateway DocsKong Mesh DocsKong AI GatewayKong Insomnia DocsKong Plugin Hub
Open Source
Kong GatewayKumaInsomniaKong Community
Company
About KongCustomersCareersPressEventsContactPricing
  • Terms•
  • Privacy•
  • Trust and Compliance•
  • © Kong Inc. 2025