liveBook · Manning

Chapter 18: Testing security configurations

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

The chapter introduction explains why regression tests and Spring Security integration tests are necessary when code or dependencies change.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction explains why regression tests and Spring Security integration tests are necessary when code or dependencies change.

Key terms: unit tests, integration tests, regression testing, continuous integration, integration, Spring Security, security configurations, definition of done

18.1 Using mock users for tests

This section shows how to test endpoint authorization with mock users while leaving the authentication process out of those tests.

Key terms: mock user, security context, authentication, authorization, MockMvc, SecurityContext, /hello, 401 Unauthorized, 200 OK, @WithMockUser

18.2 Testing with users from a UserDetailsService

This section shows how to test an endpoint using a user loaded from a UserDetailsService instead of a mock user.

Key terms: UserDetailsService, mock user, data source, UserDetailsService bean, @WithUserDetails, username

18.3 Using custom Authentication objects for testing

Shows how to give a security test a custom Authentication object by building its mock SecurityContext, and explains why that setup does not test authentication.

Key terms: SecurityContext, Authentication, factory class, @WithCustomUser, WithSecurityContextFactory, createSecurityContext(), @WithSecurityContext, RetentionPolicy.RUNTIME, custom AuthenticationProvider, mock SecurityContext

18.4 Testing method security

The section shows how to test method security by calling a Spring-managed bean directly and checking the results for different authenticated users.

Key terms: method security, SecurityContext, @WithMockUser, @WithUserDetails, MockMvc, NameService, getName(), @PreAuthorize, write, AuthenticationException

18.5 Testing authentication

Shows how to test authentication through client-like requests, including HTTP Basic, form login, and mock tokens for resource servers.

Key terms: authentication, httpBasic(), AuthenticationProvider, 401 Unauthorized, form login, AuthenticationFailureHandler, AuthenticationSuccessHandler, read authority, resource server, with(jwt())

18.6 Testing CSRF configurations

This section shows how to test that CSRF protection rejects a POST request without a token and accepts one with a token.

Key terms: CSRF token, RequestPostProcessor, csrf(), 403 Forbidden, 200 OK, CSRF vulnerability, CSRF protection, mutating operation

18.7 Testing CORS configurations

The section explains how to test CORS policies by making a preflight-style request and checking the response headers.

Key terms: CORS policies, preflight request, HTTP OPTIONS, Access-Control-Allow-Origin, Access-Control-Allow-Methods, origin, response headers

18.8 Testing reactive Spring Security implementations

This section explains two ways to test security in reactive Spring applications: mock users with @WithMockUser and test-context configuration with WebTestClientConfigurer.

Key terms: @WithMockUser, WebTestClient, MockMvc, SecurityContext, mock user, WebTestClientConfigurer, RequestPostProcessor, CSRF token, test context

18.9 Summary

Concepts explained in this chapter

← Chapter 17: Implementing security in reactive applications

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.