Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 39 ยท Getting Started

Introduction to Spring Security

What Spring Security is, what problems it solves, how authentication differs from authorization, and how this course teaches it with runnable Java stand-ins.

Every web application has to answer two questions for every request: who is calling? and may they do this? The first question is authentication, the second is authorization. Spring Security is the framework that answers both for applications built with Spring. It is the de facto standard for securing Spring Boot services, and it is also one of the most widely used security libraries in the Java world.

This course takes Spring Security apart piece by piece. You will learn what the framework does for you automatically, how its filter chain works, how to configure it with the modern lambda DSL, and how to add passwords, sessions, JWTs, OAuth 2 and OpenID Connect login, CSRF protection and tests. If you have only copied a SecurityFilterChain bean from a blog post before, this is where it starts to make sense.

Honest note about the runner. The real Spring Security (and Spring Boot) cannot run in the in-browser Java runner on this site: there is no Maven, no dependency download and no web container. So every Spring snippet in this course is shown read-only, and every runnable example is a small, clearly labelled plain-Java stand-in that rebuilds the underlying idea (a filter chain, a password check, a role hierarchy) with only the JDK. The stand-ins teach the mechanics; the read-only snippets show the real API.

What Spring Security gives you

Spring Security is a collection of modules, and the headline features are:

  • Authentication: form login, HTTP Basic, remember-me, JWT bearer tokens, OAuth 2 / OIDC login, LDAP, SAML, one-time tokens and passkeys.
  • Authorization: URL rules, method-level rules (@PreAuthorize), role hierarchies and domain-object permissions.
  • Password storage: strong adaptive hashes (BCrypt, Argon2, SCrypt, PBKDF2) behind one interface.
  • Attack protection: CSRF tokens, session fixation protection, clickjacking and content-sniffing headers, HSTS, firewalling of malformed URLs.
  • Integration: Servlet and reactive (WebFlux) stacks, Spring MVC, Spring Data, WebSocket, and a test module.

The key idea is that all of this is built from small replaceable parts. You rarely write security code from scratch; you assemble and configure parts.

Authentication versus authorization

These two words are easy to mix up, so fix them in your head early.

Authentication Authorization
Question Who are you? What may you do?
Input Credentials: password, token, certificate An identity plus a rule
Failure response 401 Unauthorized 403 Forbidden
Spring Security type AuthenticationManager AuthorizationManager

The names of the HTTP status codes are famously misleading: 401 Unauthorized really means "unauthenticated" and 403 Forbidden really means "unauthorized". Remember it as "401: I don't know you, 403: I know you, and no."

flowchart LR
  A["Request"] --> B{"Credentials<br/>valid?"}
  B -- "no" --> C["401 Unauthorized"]
  B -- "yes" --> D{"Allowed to<br/>do this?"}
  D -- "no" --> E["403 Forbidden"]
  D -- "yes" --> F["Run the controller"]

The two gates every protected request passes.

A tiny gate in plain Java

The following program is not Spring: it is the smallest possible version of the two gates above. A tiny user store knows two people, and one rule says which role each URL needs. Run it and read the status codes.

Java Two gates: 401 versus 403

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Notice what is wrong with this toy: passwords are stored in plain text, credentials are checked inside the handler, and every handler would have to remember to call handle. Spring Security fixes all three: passwords are hashed, the checks live in filters that run before your code, and you declare rules in one place.

What the framework protects against

A secure application is more than a login page. Spring Security switches on defenses against well-known attacks by default:

  • Session fixation: a new session id is issued after login.
  • CSRF: a forged cross-site POST is rejected without a valid token. A later chapter covers it in depth.
  • Clickjacking and sniffing: the headers X-Frame-Options and X-Content-Type-Options are added.
  • Cache leaks: authenticated pages are sent with no-cache headers so a shared computer does not show them after logout.
  • Weird URLs: the StrictHttpFirewall rejects paths with encoded slashes, semicolons or backslashes.

None of these needs a line of configuration, and each one can be adjusted when you have a reason.

How the course is organized

The chapters follow the way a request travels through an application:

  1. Getting Started: what happens when you add the starter and the core types.
  2. Filter Chain and Configuration: the machinery and the SecurityFilterChain DSL.
  3. Authentication Mechanisms: users, password hashes, providers, form login, Basic, sessions.
  4. Authorization and Method Security: roles, rules, annotations, permissions, error handling.
  5. onward: JWT, OAuth 2 and OIDC, CSRF/CORS, testing and projects.

Prerequisites: you should be comfortable with Java (records, lambdas, generics) and have seen a Spring Boot controller. If servlet filters are new to you, skim Filters and Listeners first, and the shorter Spring Security overview gives a quick preview of this entire track.

Spring Security versions in brief

Spring Security 5.7 deprecated the old WebSecurityConfigurerAdapter, and 6.0 removed it. Modern code declares one or more SecurityFilterChain beans and configures them with lambdas; method security uses @EnableMethodSecurity; and request rules use authorizeHttpRequests. This course teaches that style, which is what current Boot versions expect. When you meet old tutorials with extends WebSecurityConfigurerAdapter or authorizeRequests(), you know to translate them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A caller sends a correct token but the endpoint needs the ADMIN role and the caller only has USER. Which status code is right?

    The caller is authenticated, so this is not a 401. They lack permission, which is 403 Forbidden.
  2. 2. Which statement describes authentication?

    Authentication establishes identity. Authorization decides what that identity may do, and TLS protects the transport.
  3. 3. Why can the real Spring Security not run in this site's Java runner?

    The runner compiles one file against the JDK only, so the course uses plain-Java stand-ins for the ideas.
  4. 4. Which of these is turned on by default when you add the Spring Security starter?

    The defaults are secure: every request needs authentication, state-changing requests need a CSRF token and headers are added.

Summary

  • Spring Security answers two questions: authentication (who?) and authorization (may they?).
  • 401 means not authenticated, 403 means authenticated but not allowed.
  • It adds defenses against session fixation, CSRF, clickjacking and cache leaks by default.
  • Modern configuration uses SecurityFilterChain beans and the lambda DSL, not WebSecurityConfigurerAdapter.
  • On this site, real Spring code is read-only and runnable examples are plain-Java stand-ins.

Next: Your first secured application