0
0
Spring Bootframework~10 mins

HTTP Basic authentication in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to enable HTTP Basic authentication in Spring Boot.

Spring Boot
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .[1]();
    }
}
Drag options to blanks, or click blank then click option'
AhttpBasic
BformLogin
Ccsrf
Dlogout
Attempts:
3 left
💡 Hint
Common Mistakes
Using formLogin() instead of httpBasic() will enable form-based login, not HTTP Basic.
Forgetting to call any authentication method will leave the app unsecured.
2fill in blank
medium

Complete the code to configure an in-memory user with username 'user' and password 'password'.

Spring Boot
@Bean
public UserDetailsService userDetailsService() {
    UserDetails user = User.withDefaultPasswordEncoder()
        .username("user")
        .password("password")
        .roles("USER")
        .[1]();
    return new InMemoryUserDetailsManager(user);
}
Drag options to blanks, or click blank then click option'
Amake
Bcreate
Cget
Dbuild
Attempts:
3 left
💡 Hint
Common Mistakes
Using create() or get() will cause compilation errors as these methods do not exist.
Forgetting to call build() will leave the user incomplete.
3fill in blank
hard

Fix the error in the method signature to override the configure method for authentication manager.

Spring Boot
@Override
protected void configure(AuthenticationManagerBuilder [1]) throws Exception {
    [1].inMemoryAuthentication()
        .withUser("admin")
        .password("adminpass")
        .roles("ADMIN");
}
Drag options to blanks, or click blank then click option'
Amanager
Bauth
Cauthentication
Dbuilder
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the one used inside the method causes errors.
Not matching the parameter name with its usage inside the method.
4fill in blank
hard

Fill both blanks to disable CSRF protection and enable HTTP Basic authentication.

Spring Boot
@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().[1]()
        .and()
        .[2]();
}
Drag options to blanks, or click blank then click option'
Adisable
Benable
ChttpBasic
DformLogin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'enable()' instead of 'disable()' for CSRF disables nothing.
Using 'formLogin()' instead of 'httpBasic()' changes the authentication method.
5fill in blank
hard

Fill all three blanks to create a user with username 'guest', password 'guestpass', and role 'GUEST'.

Spring Boot
UserDetails guest = User.withDefaultPasswordEncoder()
    .username("[1]")
    .password("[2]")
    .roles("[3]")
    .build();
Drag options to blanks, or click blank then click option'
Aguest
Bguestpass
CGUEST
DADMIN
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up roles or passwords causes authentication failures.
Using 'ADMIN' role instead of 'GUEST' changes user permissions.