HTTP Basic authentication helps protect your web app by asking users for a username and password before they can access certain pages.
HTTP Basic authentication in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Spring Boot
http
.authorizeHttpRequests(auth -> auth
.anyRequest().authenticated()
)
.httpBasic();This code goes inside your Spring Security configuration class.
It tells Spring Boot to require authentication for all requests and use HTTP Basic.
Examples
Spring Boot
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/admin/**").authenticated()
.anyRequest().permitAll()
)
.httpBasic();Spring Boot
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
.httpBasic()
.and()
.build();
}Sample Program
This Spring Boot app requires HTTP Basic authentication for all pages. When you visit any URL, the browser will ask for username and password.
Spring Boot
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { return http .authorizeHttpRequests(auth -> auth.anyRequest().authenticated()) .httpBasic() .and() .build(); } }
Important Notes
HTTP Basic sends credentials encoded but not encrypted. Use it only over HTTPS to keep passwords safe.
Spring Boot by default creates a user with a generated password printed in the console when the app starts.
Summary
HTTP Basic authentication is a simple way to protect web resources with username and password.
Spring Boot makes it easy to enable with just a few lines in your security config.
Always use HTTPS with HTTP Basic to keep credentials secure.
Practice
1. What does HTTP Basic authentication do in a Spring Boot application?
easy
Solution
Step 1: Understand HTTP Basic authentication purpose
HTTP Basic authentication requires users to provide a username and password to access protected resources.Step 2: Identify what it does in Spring Boot
Spring Boot uses HTTP Basic to prompt for credentials before allowing access to endpoints.Final Answer:
It protects web resources by requiring a username and password. -> Option AQuick Check:
HTTP Basic authentication = username and password protection [OK]
Hint: Remember HTTP Basic always asks for username and password [OK]
Common Mistakes:
- Thinking HTTP Basic encrypts data by itself
- Assuming it allows access without credentials
- Confusing it with disabling security
2. Which of the following is the correct way to enable HTTP Basic authentication in a Spring Security configuration?
easy
Solution
Step 1: Recall Spring Security method for HTTP Basic
The correct method to enable HTTP Basic ishttpBasic()on the HttpSecurity object.Step 2: Match the exact method name
Onlyhttp.httpBasic();matches the official Spring Security syntax.Final Answer:
http.httpBasic(); -> Option CQuick Check:
Enable HTTP Basic = http.httpBasic() [OK]
Hint: Look for exact method name: httpBasic() [OK]
Common Mistakes:
- Using incorrect method names like enableBasicAuth()
- Confusing method names with similar words
- Missing parentheses in method call
3. Given this Spring Security configuration snippet, what happens when a user accesses a protected endpoint?
http .authorizeHttpRequests(auth -> auth.anyRequest().authenticated()) .httpBasic();
medium
Solution
Step 1: Analyze the configuration
The configuration requires authentication for any request and enables HTTP Basic authentication.Step 2: Understand HTTP Basic behavior
HTTP Basic triggers a browser popup asking for username and password when accessing protected resources.Final Answer:
The user is prompted to enter username and password via browser popup. -> Option DQuick Check:
httpBasic() = browser login popup [OK]
Hint: httpBasic() triggers browser popup for credentials [OK]
Common Mistakes:
- Thinking it redirects to a login page
- Assuming no credentials are needed
- Confusing 404 error with authentication failure
4. Identify the error in this Spring Security configuration for HTTP Basic authentication:
http .authorizeHttpRequests(auth -> auth.anyRequest().authenticated()) .httpbasic();
medium
Solution
Step 1: Check method names carefully
The method to enable HTTP Basic ishttpBasic()with a capital B, nothttpbasic().Step 2: Verify other methods
authorizeHttpRequests()is correct in Spring Security 6+, andauthenticated()is appropriate to require login.Final Answer:
Method name should be httpBasic() with capital B. -> Option AQuick Check:
Method names are case-sensitive = httpBasic() [OK]
Hint: Check method capitalization carefully [OK]
Common Mistakes:
- Using wrong method case like httpbasic()
- Confusing authorizeHttpRequests with older authorizeRequests
- Changing authenticated() to permitAll() incorrectly
5. You want to secure your Spring Boot REST API with HTTP Basic authentication but only for the endpoints under
/admin/**. Which configuration snippet correctly applies HTTP Basic only to those endpoints?hard
Solution
Step 1: Understand the requirement
Only endpoints matching/admin/**should require authentication; others should be open.Step 2: Analyze each option
http.authorizeHttpRequests(auth -> auth.requestMatchers("/admin/**").authenticated().anyRequest().permitAll()).httpBasic(); correctly requires authentication for/admin/**and permits all other requests. Other options either require authentication for all requests, permit all requests, or incorrectly permit the/admin/**paths.Final Answer:
http.authorizeHttpRequests(auth -> auth.requestMatchers("/admin/**").authenticated().anyRequest().permitAll()).httpBasic(); -> Option BQuick Check:
Secure only /admin/** = authenticated() on matcher + permitAll() others [OK]
Hint: Use requestMatchers for specific paths, then set auth [OK]
Common Mistakes:
- Applying authentication to all endpoints instead of specific ones
- Permitting admin endpoints by mistake
- Misordering authorizeHttpRequests and httpBasic calls
