DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Set up OAuth JWT Flow and mTLS in the Salesforce Connector for a MuleSoft App
  • Migrating MuleSoft System API to AWS Lambda (Part 1)
  • Using JSON Web Encryption (JWE)
  • Using OKTA as Client Provider in Mulesoft

Trending

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Scaling InfluxDB for High-Volume Reporting With Continuous Queries (CQs)
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Emerging Data Architectures: The Future of Data Management
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Decode JWT Tokens With Dataweave and MuleSoft

Decode JWT Tokens With Dataweave and MuleSoft

A developer explains the concepts behind JSON Web Tokens (JWTs) and how to decode them in your application using Java, MuleSoft, and DataWeave.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Updated Jan. 03, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
15.2K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

JWT stands for JSON Web Tokens and it represents the claims that needs to be securely transferred between two parties. It's a light weight, JSON-based, URL safe token and claim that is encoded as a JSON object which is digitally signed with a JSON Web Signature (JWS) and encrypted using JSON Web encryption (JWE).

JSON Web Token is Base64 encoded and it consists of three parts separated by the dots (.).

  • Header
  • Body
  • Signature

JWT Tokens look like the following:

Plain Text
 




x


 
1
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c


Header - eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9

Body - eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ

Signature - SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

You can use jwt.io for decoding the JWT token.

It is also possible to decode JWT programmatically using Java or any other languages. Below is some Java code to create JWTs.

Java Code

Java
 




xxxxxxxxxx
1
34


 
1
package com.jwt.decode;
2
import org.apache.commons.codec.binary.Base64;
3
 
          
4
public class DecodeJWT {
5
    
6
    public static String decodeJWTHeader(String jwtToken)
7
    {
8
        String[] splitToken = jwtToken.split("\\.");
9
        String encodedHeader= splitToken[0];
10
        Base64 base64Url = new Base64(true);
11
        String header = new String(base64Url.decode(encodedHeader));
12
        return header;
13
    }
14
    
15
    public static String decodeJWTBody(String jwtToken)
16
    {
17
        String[] splitToken = jwtToken.split("\\.");
18
        String encodedBody= splitToken[1];
19
        Base64 base64Url = new Base64(true);
20
        String body = new String(base64Url.decode(encodedBody));
21
        return body;
22
    }
23
    
24
    public static String decodeJWTSignature(String jwtToken)
25
    {
26
        String[] splitToken = jwtToken.split("\\.");
27
        String encodedSignature= splitToken[2];
28
        Base64 base64Url = new Base64(true);
29
        String signature = new String(base64Url.decode(encodedSignature));
30
        return signature;
31
    }
32
 
          
33
}


POM Dependency:

XML
 




xxxxxxxxxx
1


 
1
<dependency>
2
    <groupId>commons-codec</groupId>
3
    <artifactId>commons-codec</artifactId>
4
    <version>1.10</version>
5
</dependency>


In the above Java code, there are three functions for decoding and extracting the Header, Body, and Signature.

Decode JWT Token With MuleSoft Dataweave

MuleSoft provides the dw::core::Binaries library that can be used to decode the JWT Token. This library has a function, fromBase64, that can be used to decode the JWT Token. Below is the Dataweave code showing how to decode a JWT token and extract the header and body.

Dataweave

JSON
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
import fromBase64 from dw::core::Binaries
3
output application/json
4
var splitPayload = (payload.token splitBy ".")
5
---
6
{
7
    header:read(fromBase64(splitPayload[0]),"application/json"),
8
    body:read(fromBase64(splitPayload[1]),"application/json")
9
}


The read function basically reads the string or binary and returns the parsed content. In this case, fromBase64 will return a string and, using the read function, it will be converted into JSON.

Input

JSON
 




xxxxxxxxxx
1


 
1
{  "token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
2
}


Output

JSON
 




xxxxxxxxxx
1
11


 
1
{
2
    "header": {
3
        "alg": "HS256",
4
        "typ": "JWT"
5
    },
6
    "body": {
7
        "sub": "1234567890",
8
        "name": "John Doe",
9
        "iat": 1516239022
10
    }
11
}


This shows how easily you can decode the JWT token using MuleSoft Dataweave.

JWT (JSON Web Token) MuleSoft JSON

Opinions expressed by DZone contributors are their own.

Related

  • How to Set up OAuth JWT Flow and mTLS in the Salesforce Connector for a MuleSoft App
  • Migrating MuleSoft System API to AWS Lambda (Part 1)
  • Using JSON Web Encryption (JWE)
  • Using OKTA as Client Provider in Mulesoft

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!