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 WebFlux
  • Authentication With Remote LDAP Server in Spring Web MVC
  • How to Implement Two-Factor Authentication in A Spring Boot OAuth Server? Part 2: Under the Hood
  • Spring Security With LDAP Authentication

Trending

  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  1. DZone
  2. Coding
  3. Frameworks
  4. How Does HTTP Basic Authentication Work in Spring Security?

How Does HTTP Basic Authentication Work in Spring Security?

Want to learn more about how to HTTP basic authentication works in Spring Security? Check out this post to learn more about HTTP basic authentication.

By 
Javin Paul user avatar
Javin Paul
·
Aug. 23, 18 · Presentation
Likes (9)
Comment
Save
Tweet
Share
62.1K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will attempt to understand exactly how HTTP basic authentication works in Spring Security. If you remember, when you use HTTP basic for authentication purposes, the client, e.g. your browser or a REST client, sends login credentials in the HTTP request header.

The header is aptly named as "Authorization," and it contains Base64-encoded string, which is created by concatenating the username and password using a colon.

For example, if the username is "johnsmith"  and the password is "JOHN3214," then they will be concatenated as"johnsmith:JOHN3214" before encoded using Base64 encoding algorithms.

The server, when it receives such a request, it extracts the value of the "Authorization" header and decodes the content of this header using the same algorithm Base64 for authenticating the user.

In a previous post, we used <http-basic>l; in the XML configuration or the httpBasic() method on the HttpSecurity object to enable basic authentication.

Now, let's see exactly how Spring Security supports HTTP basic authentication and how things move inside the Spring Security space when it receives a login request and HTTP basic authentication is enabled at the server end.

How Spring Security Processes HTTP Basic Authentication Requests

When you use the <http-basic>l; configuration element, Spring Security's BasicAuthenticationFitler comes into the picture, which basically checks to see if the incoming HTTP request contains the "Authorization" header or not and its value starts with "Basic."

A BasicAuthenticationEntryPoint strategy is also configured into the  ExceptionTranslationFilter  on startup, which is required to handle requests that do not contain the "Authorization" header.

When you make an HTTP request to a protected URL, e.g. /admin/users from the browser without adding the "Authorization" header, then Spring Security throws an access-denied exception that is handled by the ExceptionTranslationFilter .

This filter then delegates to a particular implementation strategy of the AuthenticationEntryPoint   interface, which is the BasicAuthenticationEntryPoint in our case.

This class adds the header "WWW-Authenticate: Basic real="Spring Security Application" to the response and then sends an HTTP status code of 401 (Unauthorized) to the client, e.g. to your browser, which knows how to handle this code and work accordingly. Because of this, it shows a dialog box prompting for username and password, as shown below:

How Http Basic Authentication works in Spring Security?

When you put the username and password and submit the request, the request again follows the filter chain until it reaches the BasicAuthenticationFilter.

This filter checks the request headers and the location for the Authorization header, starting with "Basic." It will look something like this: Authorization: Basic CDWhZGRpbjpvcGVuc2AzYW1l .

The BasicAuthentictionFilter then extracts the content of the Authorization header and uses the Base64 algorithm to decode the login credentials to extract the username and password from the decoded string.

Once it has that information, the filter creates a UsernamePasswordAuthenticationToken object and sends it to the authentication manager for authentication in the standard way.

Image title

The authentication manager will ask the authentication provider ( in memory, JDBC backed or LDAP based) to retrieve the user and then create an Authentication object with it. This process is standard and independent of using HTTP basic for authentication and is applicable for digest authentication, as well.

If you are working in RESTful web services, you can also use the curl command to send the HTTP request with the Authorization error for HTTP basic authentication. I have found curl to be an easy way to test web services by sending various HTTP command from the command line.

 You can also see my post on how to test RESTful web services to check out some practical examples of curl, like sending post requests, a request with HTTP basic and digest authentication, etc.

As I have said before, basic authentication is not secure. Anyone who can intercept the request can decode the password, hence it is only used for testing purposes, while more sophisticated digest authentication and OAuth  is used in the real-world application, particularly if you are want to secure your REST API.

How Http Basic Authentication works in Spring Security and REST API

That's all about how HTTP basic authentication works inside Spring Security. You have seen the full workflow of what happens when an HTTP request hits a protected URL ,which requests basic authentication. Basically, the BasicAuthenticationFilter does most of the job along withBasicAuthenticationEntryPoint.

Spring Security authentication Spring Framework

Published at DZone with permission of Javin Paul, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Authentication With Remote LDAP Server in Spring WebFlux
  • Authentication With Remote LDAP Server in Spring Web MVC
  • How to Implement Two-Factor Authentication in A Spring Boot OAuth Server? Part 2: Under the Hood
  • Spring Security With LDAP Authentication

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!