Implementing HTTP Basic Authentication for Spring Boot Security

Mansoor Aldosari
2 min readAug 22, 2023

--

Photo by Jason Dent on Unsplash

In modern web applications, security is of paramount importance. One common authentication mechanism is HTTP Basic authentication, where users provide their credentials (username and password) to access protected resources. In this blog post, we’ll explore how to implement HTTP Basic authentication in a Spring Boot application using Spring Security.

What is HTTP Basic Authentication? HTTP Basic authentication is a simple authentication mechanism where the client sends its credentials in the form of a username and password with each request. The server verifies these credentials to grant access to protected resources.

Setting Up a Spring Boot Project: Start by creating a new Spring Boot project using your preferred IDE or Spring Initializr. Include the Spring Web and Spring Security dependencies in your pom.xml or build.gradle file.

Implementing HTTP Basic Authentication with Spring Security: Create a configuration class that extends WebSecurityConfigurerAdapter to configure Spring Security:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/public-endpoint").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}

Customizing User Details and Roles: For basic testing purposes, Spring Boot provides default user credentials. You can customize these by adding the following properties in application.properties or application.yml:

spring.security.user.name=admin
spring.security.user.password=adminpassword

In a real application, you’d create a custom UserDetailsService to authenticate against a database or external authentication provider.

Testing the HTTP Basic Authentication: Create a couple of endpoints in your controller. For instance:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {

@GetMapping("/public-endpoint")
public String publicEndpoint() {
return "This is a public endpoint.";
}

@GetMapping("/secure-endpoint")
public String secureEndpoint() {
return "This is a secure endpoint accessible via HTTP Basic authentication.";
}
}

6. Conclusion: HTTP Basic authentication provides a simple yet effective way to secure your Spring Boot applications. By setting up Spring Security and customizing user details, you can control access to your protected resources. Always ensure secure transmission of credentials by using HTTPS when implementing this authentication mechanism.

In this blog post, we’ve covered the basics of implementing HTTP Basic authentication with Spring Boot Security. You can further enhance security by integrating with more advanced authentication providers and configuring authorization rules for different user roles.

--

--