liveBook · Manning

Chapter 6: Implementing authentications

What you will learn in chapter 6 of Spring Security in Action, Second Edition

Introduces where authentication decisions happen in Spring Security and what follows when a request is accepted or rejected.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces where authentication decisions happen in Spring Security and what follows when a request is accepted or rejected.

Key terms: AuthenticationProvider, AuthenticationManager, HTTP filter layer, HTTP 401 Unauthorized, authorization, SecurityContext

6.1 Understanding the AuthenticationProvider

Explains why applications may need authentication methods beyond username and password and identifies AuthenticationProvider as Spring Security’s way to define custom authentication logic.

Key terms: AuthenticationProvider, custom authentication logic

6.1.1 Representing the request during authentication

This section explains how Spring Security’s Authentication interface represents an authentication request and identifies the methods needed to inspect it.

Key terms: Authentication, principal, isAuthenticated(), getCredentials(), getAuthorities(), granted authorities

6.1.2 Implementing custom authentication logic

This section explains the AuthenticationProvider contract for custom authentication, including how a provider handles an Authentication request and how multiple providers can participate.

Key terms: AuthenticationProvider, UserDetailsService, PasswordEncoder, authenticate(), Authentication, AuthenticationException, isAuthenticated(), supports(), AuthenticationManager

6.1.3 Applying custom authentication logic

This section shows how to implement and register a custom AuthenticationProvider, then explains why using Spring Security’s contracts is preferable to rebuilding its features.

Key terms: AuthenticationProvider, supports(), Authentication, UsernamePasswordAuthenticationToken, authenticate(), UserDetailsService, UserDetails, PasswordEncoder, matches(), AuthenticationException

6.2 Using the SecurityContext

Introduces the security context as the place Spring Security keeps a successful authentication and outlines how its storage changes across threads.

Key terms: SecurityContext, Authentication, SecurityContextHolder, MODE_THREADLOCAL, MODE_INHERITABLETHREADLOCAL, MODE_GLOBAL, @Async, security context, threads that are not known by Spring

6.2.1 Using a holding strategy for the security context

This subsection explains how the default security-context holding strategy isolates contexts by thread in servlet applications and how endpoint code can access the authenticated user.

Key terms: MODE_THREADLOCAL, ThreadLocal, security context, SecurityContextHolder, getContext(), Authentication, method parameters

6.2.2 Using a holding strategy for asynchronous calls

This section explains why an asynchronous method can lose access to the request’s security context and how an inheritable thread-local strategy addresses that case.

Key terms: MODE_THREADLOCAL, @Async, security context, Authentication, MODE_INHERITABLETHREADLOCAL, SecurityContextHolder.setStrategyName(), spring.security.strategy

6.2.3 Using a holding strategy for standalone applications

This section explains when to use a global security context, how to select it, and the concurrency risk it creates.

Key terms: MODE_GLOBAL, security context, standalone application, SecurityContextHolder.setStrategyName(), spring.security.strategy, SecurityContext, thread safe, concurrent access

6.2.4 Forwarding the security context with DelegatingSecurityContextRunnable

This section explains why self-managed threads can lack the current security context and how task decorators propagate it.

Key terms: security context, self-managed threads, authentication, DelegatingSecurityContextRunnable, DelegatingSecurityContextCallable<T>, Runnable, Callable<T>

6.2.5 Forwarding the security context with DelegatingSecurityContextExecutorService

This section explains how to propagate the security context to threads started by application code by decorating an ExecutorService rather than each task.

Key terms: DelegatingSecurityContextExecutorService, ExecutorService, Callable<T>, security context, DelegatingSecurityContextScheduledExecutorService, ScheduledExecutorService, DelegatingSecurityContextExecutor, Executor

6.3 Understanding HTTP Basic and form-based login authentications

This introduction explains why HTTP Basic has been useful for examples while cautioning that it may not suit every real-world application.

Key terms: HTTP Basic, proof of concept

6.3.1 Using and configuring HTTP Basic

This section shows how to configure HTTP Basic’s realm and customize failed-authentication responses.

Key terms: HTTP Basic, Customizer, realmName(), WWW-Authenticate, AuthenticationEntryPoint, commence(), AuthenticationException, CustomEntryPoint, ExceptionTranslationManager, AccessDeniedException

6.3.2 Implementing authentication with form-based login

This section shows how to enable form-based login, serve a page after login, customize authentication outcomes, and optionally support HTTP Basic alongside the form.

Key terms: HttpSecurity, SecurityFilterChain, formLogin(), httpBasic(), home.html, /home, @Controller, @RestController, view, FormLoginConfigurer

6.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: A web app’s security begins with filters Chapter 7: Configuring endpoint-level authorization: Restricting access →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.