Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
HTTP Basic authentication
📖 Scenario: You are building a simple Spring Boot web application that needs to protect its endpoints with HTTP Basic authentication. This means users must provide a username and password to access the app.
🎯 Goal: Set up HTTP Basic authentication in a Spring Boot app to secure all endpoints with a username and password.
📋 What You'll Learn
Create a Spring Boot application class
Add a security configuration class to enable HTTP Basic authentication
Configure an in-memory user with username user and password password
Secure all HTTP endpoints so they require authentication
💡 Why This Matters
🌍 Real World
HTTP Basic authentication is a simple way to protect web applications by requiring a username and password. It is often used for internal tools or APIs.
💼 Career
Understanding how to configure HTTP Basic authentication in Spring Boot is important for backend developers to secure applications quickly and effectively.
Progress0 / 4 steps
1
Create the Spring Boot application class
Create a class called BasicAuthApplication with the @SpringBootApplication annotation and a main method that runs SpringApplication.run(BasicAuthApplication.class, args).
Spring Boot
Hint
This is the main class to start your Spring Boot app. Use @SpringBootApplication and a main method.
2
Add a security configuration class
Create a class called SecurityConfig annotated with @Configuration and @EnableWebSecurity. Inside, define a SecurityFilterChain bean method called filterChain that takes HttpSecurity http as a parameter.
Spring Boot
Hint
This class will hold your security setup. Use @Configuration and @EnableWebSecurity. Define a SecurityFilterChain bean method.
3
Configure HTTP Basic authentication and require authentication for all requests
In the filterChain method, configure http to require authentication for any request and enable HTTP Basic authentication by calling http.authorizeHttpRequests().anyRequest().authenticated() and http.httpBasic(). Then return http.build().
Spring Boot
Hint
Use http.authorizeHttpRequests().anyRequest().authenticated() to require login for all requests and http.httpBasic() to enable HTTP Basic authentication.
4
Add an in-memory user with username and password
In the SecurityConfig class, add a UserDetailsService bean method called users that returns an InMemoryUserDetailsManager with a user having username user, password password, and role USER. Use User.withDefaultPasswordEncoder() to create the user.
Spring Boot
Hint
Create a UserDetailsService bean that returns an InMemoryUserDetailsManager with a user named user and password password. Use User.withDefaultPasswordEncoder() to build the user.
Practice
(1/5)
1. What does HTTP Basic authentication do in a Spring Boot application?
easy
A. It protects web resources by requiring a username and password.
B. It encrypts all data sent between client and server automatically.
C. It allows users to log in without any credentials.
A. Method name should be httpBasic() with capital B.
B. authorizeHttpRequests() should be authorizeRequests().
C. authenticated() should be permitAll().
D. No error, configuration is correct.
Solution
Step 1: Check method names carefully
The method to enable HTTP Basic is httpBasic() with a capital B, not httpbasic().
Step 2: Verify other methods
authorizeHttpRequests() is correct in Spring Security 6+, and authenticated() is appropriate to require login.
Final Answer:
Method name should be httpBasic() with capital B. -> Option A
Quick 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
A. http.httpBasic().authorizeHttpRequests(auth -> auth.anyRequest().authenticated());
B. http.authorizeHttpRequests(auth -> auth.requestMatchers("/admin/**").authenticated().anyRequest().permitAll()).httpBasic();
C. http.authorizeHttpRequests(auth -> auth.anyRequest().permitAll()).httpBasic();
D. http.authorizeHttpRequests(auth -> auth.requestMatchers("/admin/**").permitAll()).httpBasic();
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 B
Quick 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