MockCertMockCert
Menu

Test yourself in exam conditions

A full Spring Certified Professional mock exam: 60 questions in 130 minutes, scored against the real exam's pass mark. Your first one is free.

Start a free mock exam
Domain 5 · 3 topics · about 19 minTopics in this domainTopics

Domain 5 · Overview

Overview

Up next: Explain basic security concepts · 11 steps + check · 6 min

Domain 5 · Topic 1 of 3

Explain basic security concepts

Step 1 of 11

Think first When a request reaches a Spring Boot app with Spring Security, which pieces decide whether it gets through, and what do they do by default?

Why it matters: Questions describe a secured app and ask what Spring Security does by default or which component handles a step.

Spring Security plugs into the Servlet container as filters. A DelegatingFilterProxy hands each request to FilterChainProxy, which picks one SecurityFilterChain. Spring Boot adds a default chain that makes every request log in first.

Step 2 of 11

Term Meaning
DelegatingFilterProxy A filter registered with the Servlet container that passes all work to a Spring bean implementing Filter.
FilterChainProxy The Spring Security filter bean that delegates to many filters through a SecurityFilterChain.
SecurityFilterChain A set of security filters run in a fixed order; only the first chain matching a request runs.
AuthenticationManager The component that authenticates a token such as UsernamePasswordAuthenticationToken.
UserDetailsService A bean type that supplies users; Boot's default one holds a single user named user with a random password.

Step 3 of 11

Spring Security's Servlet support rests on Servlet filters. A filter affects only the filters after it and the Servlet, so order matters a great deal. Authentication filters run before authorization filters. Because it uses a standard filter, it protects any Servlet application, even one without Spring.

Step 4 of 11

DelegatingFilterProxy links the container to Spring beans. The container registers it like any filter, but it passes the work to a Spring bean. That bean is typically FilterChainProxy. It also clears the SecurityContext to avoid memory leaks and applies the HttpFirewall.

The three filter-level pieces compared

Feature DelegatingFilterProxy FilterChainProxy SecurityFilterChain
What it is A filter registered through standard Servlet container mechanisms A special Spring Security filter that is a Spring bean A group of security filters run in a set order
What it does Passes every request on to a Spring bean of type Filter Chooses the chain to use and delegates to its filters Runs its filters, such as authentication before authorization

Step 5 of 11

Only the first matching SecurityFilterChain runs. By default a chain matches every request; a securityMatcher overrides that. A chain bean with no @Order is considered last. If no chain matches a request, Spring Security leaves that request unprotected.

Exam trap Several matching chains do not all run. FilterChainProxy invokes only the first chain that matches the request.

Step 6 of 11

With Spring Security on the classpath, web apps are secured. That includes Boot's /error endpoint. Auto-configuration adds @EnableWebSecurity, which publishes the default filter chain as a bean. That chain makes every request authenticate, by form login or HTTP Basic. Web requests go to the login page, while service requests get 401 Unauthorized.

Step 7 of 11

Boot's default user is user, with a random password. The password is printed at WARN level when the app starts. Set spring.security.user.password to change the password, and spring.security.user.name for the name.

Step 8 of 11

A SecurityFilterChain bean replaces Boot's default web security. It does not disable the default UserDetailsService setup. To switch that off as well, add a UserDetailsService, AuthenticationProvider or AuthenticationManager bean.

Exam trap Adding your own SecurityFilterChain leaves the default UserDetailsService configuration in place.

Step 9 of 11

BasicAuthenticationFilter turns credentials into a token. It reads the username and password from the HttpServletRequest into a UsernamePasswordAuthenticationToken. The AuthenticationManager then authenticates that token. On failure, the SecurityContextHolder is cleared. The BasicAuthenticationEntryPoint sends a WWW-Authenticate header.

Exam trap HTTP Basic is on by default only until you provide servlet-based configuration. After that, you must configure HTTP Basic explicitly.

Step 10 of 11

CSRF protection is on by default for unsafe methods like POST. You write no extra code for it. A basic Java configuration also adds session fixation protection, a generated login form and logout. Java configuration arrived in Spring Security 3.2, so no XML is needed.

Exam trap An unauthenticated request to a protected resource is not silently passed on. The AuthorizationFilter denies it by throwing an AccessDeniedException.

Step 11 of 11

  • Spring Security works through Servlet filters, so the order of those filters matters.
  • FilterChainProxy runs only the first SecurityFilterChain that matches a request.
  • Boot's default chain makes every request authenticate, with form login and HTTP Basic.
  • The default user is user, with a random password logged at startup.
  • A SecurityFilterChain bean switches off Boot's default web security, not the default user setup.

Sources: Architecture :: Spring Security (opens in a new tab) · Java Configuration :: Spring Security (opens in a new tab) · Basic Authentication :: Spring Security (opens in a new tab) · Spring Security :: Spring Boot (opens in a new tab) · Servlet Applications :: Spring Security (opens in a new tab) · Hello Spring Security :: Spring Security (opens in a new tab) · Cross Site Request Forgery (CSRF) :: Spring Security (opens in a new tab)

Check

Check: Explain basic security concepts

Cards to recall, then questions on this topic. About 2 minutes. Nothing is saved without an account.

Loading the cards and questions…

Up next: Use Spring Security to configure Authentication and Authorization · 10 steps + check · 6 min

Domain 5 · Topic 2 of 3

Use Spring Security to configure Authentication and Authorization

Step 1 of 10

Think first When a request reaches your app, which Spring Security parts decide who the user is and what they may do?

Why it matters: Questions trace a request through authentication and authorization and ask which component acts, decides or fails.

Authentication proves who the user is, and the result is kept in the SecurityContextHolder. Authorization then checks that user's granted authorities, and an AuthorizationManager makes the final access decision.

Step 2 of 10

Term Meaning
SecurityContextHolder Where Spring Security keeps the details of the currently authenticated user.
ProviderManager The usual AuthenticationManager; it delegates to a list of AuthenticationProvider instances.
GrantedAuthority A permission held by the user, usually application-wide rather than tied to one domain object.
AuthenticationEntryPoint The component that asks an unauthenticated client for credentials.

Step 3 of 10

The SecurityContextHolder holds the authenticated user. If it contains a value, Spring Security treats it as the current user, however it got there. By default it uses a ThreadLocal, so methods on the same thread can read it without receiving it as an argument.

Exam trap Spawned threads do not share the identity by default. Use the MODE_INHERITABLETHREADLOCAL strategy when child threads must act as the same user.

Step 4 of 10

An Authentication carries credentials into the manager. As input to the AuthenticationManager, its isAuthenticated() method returns false. Credentials are often a password. After a successful login, ProviderManager clears sensitive credentials from the returned object by default, so they are not leaked.

Step 5 of 10

ProviderManager delegates to a list of providers. Each AuthenticationProvider supports one kind of authentication. DaoAuthenticationProvider handles a username and password, while JwtAuthenticationProvider authenticates a JWT token. If no provider can authenticate the request, it fails with a ProviderNotFoundException.

Exam trap A ProviderNotFoundException is not a wrong-password error. It means no configured provider supports that type of Authentication.

Step 6 of 10

A simple user setup wires several pieces for you. It registers an in-memory UserDetailsService and adds DaoAuthenticationProvider to the default AuthenticationManager. It also turns on HTTP Basic and Form Login. InMemoryUserDetailsManager implements UserDetailsService for users kept in memory.

Exam trap User#withDefaultPasswordEncoder belongs in sample applications only. It is considered unsafe for production.

Step 7 of 10

Role rules add the ROLE_ prefix by default. A rule that requires role USER looks for the authority ROLE_USER. AuthorizationManager instances make the final decision for request-based, method-based and message-based authorization.

Exam trap Granting an authority named USER does not satisfy a rule for role USER. By default that rule checks for ROLE_USER.

Step 8 of 10

A missing login and a denied call end differently. An unauthenticated client meets an AuthenticationEntryPoint, which asks for credentials. When method authorization fails, an AccessDeniedException is thrown and the client gets a 403.

Not authenticated versus denied by method authorization

Feature Not authenticated Method access denied
Component that acts AuthenticationEntryPoint Method interceptor, then ExceptionTranslationFilter
Typical response Redirect to a login page or a WWW-Authenticate header A 403 status code
Event or exception Credentials are requested AuthorizationDeniedEvent and AccessDeniedException

Step 9 of 10

Spring Boot leaves method authorization off by default. Add @EnableMethodSecurity to any @Configuration class to turn it on. It enables @PreAuthorize, @PostAuthorize, @PreFilter and @PostFilter. Unannotated methods stay unsecured, so also declare a catch-all rule in HttpSecurity.

Exam trap @EnableGlobalMethodSecurity is deprecated. Migrate to @EnableMethodSecurity instead.

Step 10 of 10

  • ProviderManager delegates to providers and throws ProviderNotFoundException when none supports the request.
  • The SecurityContextHolder uses a ThreadLocal by default, so the user is bound to the current thread.
  • Role rules check for the ROLE_ prefix by default, so role USER means authority ROLE_USER.
  • Method security stays off in Spring Boot until you add @EnableMethodSecurity.

Sources: Servlet Authentication Architecture :: Spring Security (opens in a new tab) · Authorization Architecture :: Spring Security (opens in a new tab) · Username/Password Authentication :: Spring Security (opens in a new tab) · In-Memory Authentication :: Spring Security (opens in a new tab) · Method Security :: Spring Security (opens in a new tab)

Check

Check: Use Spring Security to configure Authentication and Authorization

Cards to recall, then questions on this topic. About 2 minutes. Nothing is saved without an account.

Loading the cards and questions…

Up next: Define Method-level Security · 10 steps + check · 6 min

Domain 5 · Topic 3 of 3

Define Method-level Security

Step 1 of 10

Think first How would you secure a Spring service method, and what does the caller see when the check fails?

Why it matters: Questions show annotated service methods and ask whether they are secured, which annotation applies and what the caller gets back.

Method security stays off until you switch it on with @EnableMethodSecurity. It is built on Spring AOP, so annotated methods are checked when they are called. A failed check ends in a 403 response.

Step 2 of 10

Term Meaning
@EnableMethodSecurity Annotation on a @Configuration class that turns on method-level authorization.
@PreAuthorize Recommended annotation for authorizing method calls; it supersedes the legacy @Secured annotation.
returnObject In a @PostAuthorize expression, the value the method is about to return.
filterObject In a @PreFilter expression, the collection element being tested.
hasRole Expression shortcut for hasAuthority that adds the ROLE_ prefix, or the configured default prefix.

Step 3 of 10

Method security stays off until you enable it. Spring Boot Starter Security does not activate it for you. Annotate any @Configuration class with @EnableMethodSecurity, or add the method-security element to an XML configuration file. The older @EnableGlobalMethodSecurity and the global-method-security element are deprecated.

Exam trap Adding the Spring Boot security starter alone does not turn on method-level authorization. You still have to enable it explicitly.

Step 4 of 10

Four pre and post annotations work by default. @EnableMethodSecurity enables @PreAuthorize, @PostAuthorize, @PreFilter and @PostFilter. For @RolesAllowed, @PermitAll and @DenyAll, set jsr250Enabled = true. @Secured is a legacy option, and @PreAuthorize is the recommended replacement.

Exam trap @Secured still exists, but it is the legacy choice. When a question asks for the recommended annotation, pick @PreAuthorize.

Step 5 of 10

Some annotations test the data, not only the caller. @PostAuthorize lets a method return only when its expression passes for the returned value. @PreFilter removes each collection element whose expression fails. In role checks, hasRole adds the ROLE_ prefix for you.

Two data-aware annotations compared

Annotation What it tests Expression variable
@PostAuthorize The value the method returns returnObject
@PreFilter Each element of a collection argument filterObject

Step 6 of 10

Every annotation on a method must pass. Different annotations on one method are checked one at a time, and the call is authorized only if all of them pass. You cannot repeat the same annotation on one method. A method-level annotation overrides one declared on its class.

Exam trap Placing @PreAuthorize twice on the same method is not supported, even though different annotations can be combined.

Step 7 of 10

Methods without an annotation are not secured. Declare a catch-all authorization rule in HttpSecurity to cover them. When a check fails, the interceptor throws an AccessDeniedException. The ExceptionTranslationFilter catches it and returns a 403 status code.

Step 8 of 10

The newer XML element turns pre and post annotations on by default. The older global-method-security element is deprecated, and method-security replaces it. Keeping the old one means adding the spring-security-access dependency to your build.

The two XML elements compared

Setting method-security global-method-security
Pre and post annotations pre-post-enabled, defaults to true pre-post-annotations, defaults to disabled
@Secured support secured-enabled, defaults to false secured-annotations, defaults to disabled
Status Current element Deprecated; needs spring-security-access

Step 9 of 10

AspectJ ignores security annotations on interfaces. Setting mode to aspectj makes Spring Security intercept calls with AspectJ. Java does not inherit interface annotations, so place them on the class. Secured methods must be woven with AnnotationSecurityAspect from spring-security-aspects.

Exam trap With AspectJ, annotating only the interface leaves those methods unsecured. Put the security annotation on the class.

Step 10 of 10

  • Method security is off by default; enable it with @EnableMethodSecurity on a @Configuration class.
  • Unannotated methods are not secured, so add a catch-all rule in HttpSecurity.
  • A failed check throws AccessDeniedException, which becomes a 403 response.
  • Prefer @PreAuthorize over the legacy @Secured annotation.

Sources: Method Security :: Spring Security (opens in a new tab) · Method Security :: Spring Security (opens in a new tab)

Check

Check: Define Method-level Security

Cards to recall, then questions on this topic. About 2 minutes. Nothing is saved without an account.

Loading the cards and questions…

Domain 5 · after the last topic

Domain 5 review

Each topic's Check tested it on its own; this mixes them.

Mixed quiz

Take the Domain 5 quiz

10 questions mixed from every topic in this domain, each answer explained. Mixing topics is harder than one at a time, and it is what the exam does. No account needed; with a free account your answers also build your estimated exam score.

Flashcards

Domain 5 flashcards

6 cards with this domain's key facts. Flip each one and check what you knew. No account needed.

Test yourself in exam conditions

A full Spring Certified Professional mock exam: 60 questions in 130 minutes, scored against the real exam's pass mark. Your first one is free.

Start a free mock exam
Next domain · 16% of topicsSpring Boot3 topics

Report a problem

On “”. Tell us what looks wrong; we read every report.

What is the problem?

Optional.