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

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

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

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

  • Authentication With Remote LDAP Server in Spring Web MVC
  • How to Implement Two-Factor Authentication in a Spring Boot OAuth Server? Part 1: Configuration
  • Angular JWT Autorefresh With Spring Boot
  • Authentication with Spring Boot and Spring Security — JWT and Postgres

Trending

  • Why High-Performance AI/ML Is Essential in Modern Cybersecurity
  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • Transforming AI-Driven Data Analytics with DeepSeek: A New Era of Intelligent Insights
  • Kubeflow: Driving Scalable and Intelligent Machine Learning Systems
  1. DZone
  2. Coding
  3. Frameworks
  4. Validating JWT With Spring Boot and Spring Security

Validating JWT With Spring Boot and Spring Security

For my current project I will have a REST API set up with Spring Boot. To be able to use the API endpoint the application will check that the incoming request has a valid JWT token.

By 
$$anonymous$$ user avatar
$$anonymous$$
·
Jun. 15, 16 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
61.5K Views

Join the DZone community and get the full member experience.

Join For Free

For my current project I will have a REST API set up with Spring Boot (most likely running with BoxFuse). To be able to use the API endpoint the application will check that the incoming request has a valid JWT token provided earlier (by an API service that I trust).

To implement this functionality I want to make use of Spring Security as it fits nicely with Spring Boot. When Googling for information about this combination I ran into this site that describes the background information quite nicely, but didn’t give me all the necessary sources to get it running. So after some more investigating and trial & error, I finally came to a working solution. Note that in my situation I only needed to validate an incoming token, I don’t need to create or supply new tokens.

The source code of the example can be found here on GitLab. The example application has a REST Controller called MainController. After starting the application (by running the Application.main method) you can access the REST endpoint with: http://localhost:8888/hello?name=PalmApps. As you will see you will get an HTTP 401 error if you try this in your browser:
 Screenshot at May 27 16-16-34

To get access to the endpoint you will need to supply a JWT token so you can get through the JwtAuthenticationFilter. To generate a valid token open the sources of the class JwtTokenGenerator and run the ‘main’ method, which will print a token in the console:
Screenshot at May 27 16-25-02

Copy the token and open a tool with which you can send an HTTP request and add the token to the header like Postman:
Screenshot at May 27 16-28-09 With the token in place you will see the expected output:

{
  "id": 2,
  "content": "Hello, PalmApps!"
}

If you access the endpoint http://localhost:8888/me with a POST request (still with the ‘Authorization’ header in place) you will get the details of the Principal object in JSON format:

{
  "details": null,
  "authorities": [
    {
      "authority": "admin"
    }
  ],
  "authenticated": true,
  "principal": {
    "username": "Pascal",
    "token": "eyJhbGciOiJIUzUxMeJ9.eyJzdwIiOi....m72LpFADA",
    "authorities": [
      {
        "authority": "admin"
      }
    ],
    "password": null
  },
  "credentials": null,
  "name": "Pascal"
}

The ‘principal’ field in the returned object here is our AuthenticatedUser. If we get want to get more information from our JWT then we can simply add it to this object and fill it in the JwtAuthenticationProvider.

Spring Framework Spring Boot JWT (JSON Web Token)

Published at DZone with permission of $$anonymous$$. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Authentication With Remote LDAP Server in Spring Web MVC
  • How to Implement Two-Factor Authentication in a Spring Boot OAuth Server? Part 1: Configuration
  • Angular JWT Autorefresh With Spring Boot
  • Authentication with Spring Boot and Spring Security — JWT and Postgres

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!