You need to secure a Spring Boot application to ensure that only authenticated users can access certain endpoints. Describe how you would configure Spring Security to set up a basic form-based authentication.

Spring Security provides form-based authentication to verify user identity before allowing access to protected resources. Users are redirected to a login page where they provide credentials, and only authenticated users can access secured endpoints.

Key Points

Example

Consider an Employee Portal application.

Public Endpoints

Protected Endpoints

Flow

User Requests /dashboard

↓

Spring Security Intercepts Request

↓

Redirect to Login Page

↓

User Enters Credentials

↓

Authentication Successful

↓

Access Granted

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(
            HttpSecurity http) throws Exception {

        http
            .authorizeHttpRequests(auth -> auth
                .requestMatchers(
                    "/login",
                    "/about").permitAll()
                .requestMatchers(
                    "/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated()
            )
            .formLogin(form -> form
                .loginPage("/login")
                .defaultSuccessUrl("/dashboard")
                .permitAll()
            )
            .logout(logout -> logout
                .logoutSuccessUrl("/login?logout")
            );

        return http.build();
    }
}

User Configuration Example

@Bean
public UserDetailsService userDetailsService() {
    UserDetails user = User.builder()
            .username("admin")
            .password(
                passwordEncoder().encode("admin123"))
            .roles("ADMIN")
            .build();

    return new InMemoryUserDetailsManager(user);
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

Authentication Options

Security Best Practices

Real-World Example

Banking Application

Public

Authenticated Users

Administrators

Interview Tip

A concise interview answer is: To configure basic form-based authentication in Spring Boot, I would add the Spring Security dependency, configure a SecurityFilterChain to protect endpoints, enable formLogin() for login handling, and configure users either in-memory or through a database using UserDetailsService. Passwords should always be stored using BCryptPasswordEncoder for security.

Last updated: 1 November 2026

Previous question: Your application needs to process notifications asynchronously using a message queue. Explain how you would set up the integration and send messages from your Spring Boot application.

Next question: How do you make a Spring Boot auto-configuration back away when a bean already exists?

More Spring Boot - Level II interview questions

All Spring Boot - Level II questions