liveBook · Manning

Spring Security

As explained in Spring AI in Action

Spring Security is a Spring framework project that provides authentication, authorization, and protection against common application security threats.

Spring Security secures Spring applications by authenticating users, authorizing access, and protecting documents, tools, and premium content from unauthorized use. section 10.0 section 10.1.2

A typical implementation grows in three steps:

  • Add the Spring Security starter dependency to the application build. section 10.1.2
  • Create a SecurityConfig class with @Configuration and @EnableWebSecurity, then define a SecurityFilterChain and UserDetailsService. section 10.1.2
  • Enable method security and use @PreAuthorize with hasRole('PREMIUM_USER') to protect tool methods or Function implementations. section 10.2

Restricting a game-complexity tool

Annotate the @Tool method with @PreAuthorize("hasRole('PREMIUM_USER')"). An authorized request returns the game's complexity; an unauthorized request receives a reserved-for-premium-users response instead of invoking the method.

Listing 10.2 Authenticating users and filtering premium content
package com.example.boardgamebuddy;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.
HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.
EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.
AbstractHttpConfigurer;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity  #1
public class SecurityConfig {

  @Bean
  SecurityFilterChain securityFilterChain(HttpSecurity http) #2
        throws Exception {
    http
        .httpBasic(Customizer.withDefaults())
        .csrf(AbstractHttpConfigurer::disable)
        .authorizeHttpRequests(authorizeRequests ->
            authorizeRequests
                .anyRequest().authenticated());
    return http.build();
  }

  @Bean
  UserDetailsService userDetailsService() {   #3
    var user1 = User.builder()
        .username("mickey")
        .password("{noop}password")
        .roles("USER", "PREMIUM_USER")
        .build();

    var user2 = User.builder()
        .username("donald")
        .password("{noop}password")
        .roles("USER")
        .build();

    return new InMemoryUserDetailsManager(user1, user2);
  }

}

Inspect the complete Java configuration for authenticating users and filtering premium content. section 10.1.2

How Spring AI in Action covers it

How deep it goes
Configures dependencies, web filters, users, roles, and method annotations while keeping the framework's broader configuration possibilities out of scope.
Examples
Demonstrates Board Game Buddy, premium game content, the getGameComplexity tool, and authenticated HTTP requests.
What it assumes
Assumes a Spring application and Java methods or tools, while teaching dependency setup, security configuration, users, roles, and authorization checks.
What you can do after
Operate/configure Spring applications with HTTP Basic authentication and role-based restrictions on web requests and tool methods.
How it connects
Connects authentication, authorization, method-level security, Spring Expression Language, Spring AI tools, vector stores, and security advisors.

Also explained in

Compare how 4 books explain Spring Security →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring AI in Action. The section links point to the parts of the book it draws on.