liveBook · Manning

Chapter 10: Safeguarding generative AI

What you will learn in chapter 10 of Spring AI in Action

The introduction explains why generative AI applications must protect access to documents and tools and guard against prompts that elicit sensitive responses.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why generative AI applications must protect access to documents and tools and guard against prompts that elicit sensitive responses.

Key terms: retrieval-augmented generation (RAG), tools, unauthorized users, indirect access, adversarial prompts, guardrails, user’s questions, LLM’s responses

10.1 Controlling document access with RAG

This section introduces the access-control problem in Board Game Buddy’s RAG-based game-rule system and the proposed standard and premium access levels.

Key terms: RAG, vector store, standard, premium, game rules loader

10.1.1 Designating premium content

The section explains how the game rules loader marks premium document chunks with metadata so vector searches can later distinguish them from standard content.

Key terms: Message<byte[]>, -premium, documentType, PREMIUM, metadata entry

10.1.2 Adding security to Board Game Buddy

This section adds basic Spring Security to Board Game Buddy, requiring authentication for API requests and assigning a premium role for later content filtering.

Key terms: SecurityConfig, @EnableWebSecurity, SecurityFilterChain, HTTP Basic authentication, CSRF, UserDetailsService, in-memory, USER, PREMIUM_USER, HTTPie

10.1.3 Filtering for premium content

This section explains how to filter vector-store searches so non-premium users cannot retrieve premium documents.

Key terms: FILTER_EXPRESSION, getDocumentMatchExpression(), getPremiumContentFilterExpression(), SecurityContextHolder, ROLE_ PREMIUM_USER, PREMIUM

10.1.4 Applying per-user conversational memory

This subsection shows why shared conversation IDs can leak or distort answers across users and how to isolate conversational memory by user.

Key terms: conversational memory, conversation ID, conversational context, premium document, AskController, ask(), UserDetails, @AuthenticationPrincipal, username

10.1.5 Trying it out

This subsection shows how to test Board Game Buddy’s RAG filtering by loading a premium rules document and comparing users’ access.

Key terms: RAG filtering, -premium suffix, loader directory, vector store, HTTPie, premium user

10.2 Securing tools

This section shows how Spring Security method-level authorization prevents users from invoking Spring AI tools they are not permitted to use.

Key terms: tools, LLM, calling application, @EnableMethodSecurity, SecurityConfig, @PreAuthorize, @Tool, Function, apply(), @PostAuthorize

10.3 Safeguarding against adversarial prompting

This section identifies common adversarial prompting techniques and explains why safeguards against them require ongoing vigilance.

Key terms: prompt injection, prompt leaking, jailbreaking, system message, nondeterministic nature, natural language, built-in safeguards, Spring AI advisors

10.3.1 Preventing prompts with sensitive terms

This subsection explains how to block prompts containing sensitive terms before they reach the LLM and how to customize the response users receive.

Key terms: SafeGuardAdvisor, sensitive words, prompt, sensitiveWords(), defaultAdvisors(), failureResponse(), ChatClient

10.3.2 Preventing prompt leaks

This section explains why system-message leaks are dangerous and demonstrates a canary-word advisor that can detect some attempts to expose the system message.

Key terms: system message, prompt leaks, safeguards, CanaryWordAdvisor, CallAdvisor, canary word, defaultAdvisors(), ChatClient, nondeterministic, adversarial prompts

10.4 Moderating user input

This section shows how to check user questions for moderation violations before sending them to an LLM and how to return a controlled error when a question is rejected.

Key terms: moderation, ModerationModel, moderation categories, ModerationService, Categories, ModerationException, AskController, BoardGameService, @RestControllerAdvice, @ExceptionHandler

10.5 Summary

Concepts explained in this chapter

← Chapter 9: Observing AI operations Chapter 11: Applying generative AI patterns →

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.