How can you implement authentication and authorization in Spring Boot?

Authentication and authorization in Spring Boot are implemented through Spring Security, which verifies who a user is (authentication) and then decides what they're allowed to do (authorization), configured declaratively via a SecurityFilterChain bean.

Key Points

Example

A simple app defines a UserDetailsService backed by a JPA repository for authentication, and a SecurityFilterChain bean that restricts /admin/** to ROLE_ADMIN while leaving /public/** open to everyone.

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http.authorizeHttpRequests(auth -> auth
            .requestMatchers("/public/**").permitAll()
            .requestMatchers("/admin/**").hasRole("ADMIN")
            .anyRequest().authenticated())
        .formLogin(Customizer.withDefaults());
    return http.build();
}

Interview Tip

A concise interview answer is

In Spring Boot, Spring Security handles both concerns: authentication verifies identity through a source like a database or OAuth2 provider, and authorization is declared through authorizeHttpRequests() mapping URLs to required roles, optionally refined further with method-level annotations like @PreAuthorize.

Last updated: 16 July 2026

Previous question: How do you handle session management in Spring Boot in the context of security?

Next question: Can you explain how to use method-level security in Spring Boot?

More Spring Security - Level II interview questions

All Spring Security - Level II questions