Complete the code to enable HTTP Basic authentication in Spring Boot.
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .[1](); } }
The httpBasic() method enables HTTP Basic authentication in Spring Security.
Complete the code to configure an in-memory user with username 'user' and password 'password'.
@Bean
public UserDetailsService userDetailsService() {
UserDetails user = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.[1]();
return new InMemoryUserDetailsManager(user);
}The build() method finalizes the user creation in the builder pattern.
Fix the error in the method signature to override the configure method for authentication manager.
@Override protected void configure(AuthenticationManagerBuilder [1]) throws Exception { [1].inMemoryAuthentication() .withUser("admin") .password("adminpass") .roles("ADMIN"); }
The parameter name is commonly auth and matches the usage inside the method.
Fill both blanks to disable CSRF protection and enable HTTP Basic authentication.
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().[1]()
.and()
.[2]();
}Disabling CSRF is done by disable(). Enabling HTTP Basic is done by httpBasic().
Fill all three blanks to create a user with username 'guest', password 'guestpass', and role 'GUEST'.
UserDetails guest = User.withDefaultPasswordEncoder()
.username("[1]")
.password("[2]")
.roles("[3]")
.build();The username is 'guest', password is 'guestpass', and role is 'GUEST' to match the user details.