🔐 Spring Security — interview questions

12 questions. Filter by what the role asks for; open one for the six layers of the answer. The course is where the answers come from.

12 of 12

01A controller checks `if (SecurityContextHolder.getContext().getAuthentication() != null)` to decide whether the caller is logged in, and it is always true — even with no credentials. Separately, the same endpoint returns 401 to one caller and 403 to another. Which filters explain both observations?Filter chaineasy0-2y02A security config has `requestMatchers("/api/**").authenticated()` followed by `requestMatchers("/api/admin/**").hasRole("ADMIN")`, and any signed-in user can call the admin endpoints. After reordering, admins get 403 too — their user records grant the authority `ADMIN`. What are the two bugs?Filter chaineasy0-2y03A code review flags `csrf(csrf -> csrf.disable())` in two services. One is a stateless JSON API that authenticates with a bearer token in the `Authorization` header; the other is a server-rendered app that logs users in with a session cookie. Is the flag right for both, one, or neither?CSRFmedium0-2y04A service method reads the current user with `SecurityContextHolder.getContext().getAuthentication()` and works in the request. The same method, called from an `@Async` method or inside `CompletableFuture.supplyAsync(...)`, sees no user. A teammate proposes `MODE_INHERITABLETHREADLOCAL`. Explain the empty context, and why that proposal can make things worse.Security contextmedium3-5y05An SPA on `https://app.example.com` calls a Spring Boot API on `https://api.example.com` with `Authorization: Bearer ...` and JSON bodies. Without Spring Security it worked with `@CrossOrigin`; with Spring Security added, every call fails in the browser with a CORS error, yet `curl` with the same token works. Walk through what the browser is sending and what is rejecting it.CORSmedium3-5y06A Spring Boot resource server is configured with only `spring.security.oauth2.resourceserver.jwt.issuer-uri`. In testing, a token the same identity provider issued for a *different* internal service is accepted, and `@PreAuthorize("hasRole('ADMIN')")` rejects every admin even though their tokens carry `"roles": ["ADMIN"]`. Explain both, in terms of what Spring actually configured.Resource servermedium3-5y07A front-end team signs users in with OpenID Connect and sends the **ID token** to your Spring Boot API as the bearer token, because "it has the user's email and it's a signed JWT". Your API accepts it. What is wrong with the arrangement, which party is your API in OAuth terms, and what should the API check instead?OAuth2 and OIDCmedium3-5y08`AccountService.closeAccount(id)` is guarded with `@PreAuthorize("hasRole('ADMIN')")` and tests confirm a regular user gets 403 from the controller. Months later an audit finds accounts closed by non-admins. The call paths now include a Kafka listener and an `AccountService.processRequests()` method that calls `closeAccount` for each pending request. What are the ways this guard can be bypassed or misapplied, and how would you make the coverage provable?Method securityhard5-8y09A confidential server-side web app already authenticates to the token endpoint with a client secret, and a reviewer asks why it should also use PKCE. Explain exactly what PKCE sends at each step, what an attacker who captures the authorization code can and cannot do with and without it, and what `state` protects that PKCE does not.OAuth2 and OIDChard5-8y10A service method guarded by `@PreAuthorize` used to return 403 to unauthorised callers and 401 to anonymous ones. After someone added a `@RestControllerAdvice` with `@ExceptionHandler(Exception.class)` that returns a generic 500 body, those calls now return 500. The exception is thrown long after the security filters have "run". How did it ever become a 403, and what changed?Filter chainhard5-8y11Your resource servers validate RS256 access tokens (15-minute lifetime) using `issuer-uri`. During an identity-provider key rotation, some pods reject valid tokens for a few minutes; a month later, a 20-minute identity-provider outage causes a partial outage of your API even though no tokens changed. Explain how the resource server finds keys, what went wrong in each incident, and what rotation procedure and settings you would agree with the identity team.Resource serversenior8+y12One Spring Boot application serves a server-rendered admin UI (users log in with a session), a public JSON API used by mobile apps with OAuth2 access tokens, and Actuator endpoints for the platform team. Legal adds a requirement: when an employee is terminated, their access must end within one minute. Design the security configuration, and justify the token validation choice given that requirement.Architecturesenior8+y