Scrivio Interview library
ScenarioAdvanced6 min read · updated 12 November 2026

Security review finds 30 services with hand-written authentication filters, each validating tokens slightly differently. What do you do?

I would first inventory the 30 filters and the token types they accept, because most of them reimplement JWT or API key validation with small differences. Then I would replace them with standard mechanisms, Spring's OAuth2 resource server for JWTs and one shared API key provider, delivered in a shared starter, backed by a common test suite for edge cases such as expired tokens, wrong audience, missing headers and clock skew. Internet-facing services and those with the weakest validation go first, and a policy requires security team review for any new authentication code.

Why this question is asked

Finding thirty hand-written authentication filters is not unusual in a company that grew quickly: someone wrote one, others copied it, each copy got its own fixes. The danger is that 'slightly differently' usually includes 'slightly wrongly' in a few places, and authentication bugs are among the most serious. Interviewers ask this scenario to see whether we respond with a plan rather than a rewrite: measure, standardise, test, prioritise and prevent recurrence. In this answer, we will build the inventory, look at what typically goes wrong, design the replacement, write the shared test suite, decide the order and set the policy.

1Start With an Inventory

Before changing anything we need facts. For each service we record which filter classes authenticate requests, which token types they accept (JWT, API key, a custom header), which library they use to parse tokens, which claims they check, how they handle failures, whether the service is reachable from the internet, and who owns it. Finding the filters is mostly code search: classes that extend OncePerRequestFilter or GenericFilterBean in security packages, code that reads the Authorization header, and direct use of JWT libraries.

We also send each service a small set of probe requests in a test environment, an expired token, a token for another audience, a token signed with the wrong key, and record what happens. Reading the code tells us what the authors intended; the probes tell us what the services actually do.

2What the Inventory Usually Finds

The differences fall into a few repeating patterns, and some of them are real vulnerabilities. A summary table like the one below is also what we show leadership to justify the work.

Typical findings across hand-written filters
FindingServices (example)Risk
Audience not checked11 of 30A token for any service is accepted
Expired tokens accepted or skew of hours4 of 30Stolen tokens stay valid
Parse errors swallowed, request continues7 of 30Failures become anonymous access
Algorithm taken from the token header2 of 30Signature checks can be bypassed
API keys compared with equals(), logged on failure5 of 30Timing leaks; keys in logs
Issuer not checked9 of 30Tokens from other environments accepted
Illustrative numbers; the pattern of small, repeated mistakes is typical.

3Replace With Standard Mechanisms

The goal is that no service validates tokens with its own code. For JWTs we use Spring Security's OAuth2 resource server, which checks the signature with keys from the issuer's JWKS endpoint, a fixed algorithm, issuer, expiry with 60 seconds of skew, and, once configured, audience. For API keys we write one AuthenticationProvider and converter, reviewed once by the security team, with hashed storage and constant-time comparison.

Both ship in a shared starter. A service adds the dependency, sets its audience and scopes, and deletes its filter. Migrating a service then becomes a small, reviewable change rather than a security project.

Java
// Before: 120 lines of custom JwtAuthFilter per service
// After: in each service
@Bean
SecurityFilterChain api(HttpSecurity http, StandardSecurity standard) throws Exception {
standard.apply(http); // from the starter: resource server, API keys,
// entry point, error format, stateless
http.authorizeHttpRequests(a -> a
.requestMatchers(HttpMethod.GET, "/orders/**").hasAuthority("SCOPE_orders.read")
.anyRequest().hasAuthority("SCOPE_orders.write"));
return http.build();
}

4One Test Suite for Every Edge Case

Standard code still needs proof that each service is configured correctly. The starter ships a reusable test kit that every service runs in its build. It mints test tokens with a local key and sends them to one protected endpoint, asserting the response for each edge case.

Java
@SecurityContractTest // from the starter's test kit
class OrdersSecurityTest {
// each case sends one request to a protected endpoint and checks the status
// valid token, right audience -> 200
// expired 5 minutes ago -> 401 invalid_token
// expired 30 seconds ago (inside 60 s skew) -> 200
// not before (nbf) 5 minutes in the future -> 401
// wrong audience / wrong issuer -> 401
// signed with an unknown key / alg "none" -> 401
// missing Authorization header -> 401 with WWW-Authenticate: Bearer
// malformed header ("Bearer" with no token) -> 401
// valid token without the required scope -> 403
}

Notice that the clock skew case tests both sides: a token expired by 30 seconds must still pass, so we do not break clients with slightly slow clocks, and one expired by minutes must fail. Running the same kit before and after migration also shows exactly which behaviours change, which helps when a client depended on a bug.

5Deciding the Order

Thirty services cannot migrate at once, and they are not equally urgent. We score each by exposure (internet-facing or internal) and by the severity of its findings, and work from the top.

Step throughOrdering the migration
  1. Internet-facing with a critical finding?
    YesFix this sprint; hotfix the filter if neededNoContinue
  2. Internet-facing or critical finding?
    YesWave 1: migrate in the next 4 weeksNoContinue
  3. Handles sensitive data or high privilege?
    YesWave 2NoWave 3: normal upgrade cycle

3 steps in order. Play them, or step through them one at a time.

6Preventing It From Happening Again

Without a rule, the thirty-first filter appears next quarter. So we set a policy that any new authentication code, any class that creates an Authentication or sets the SecurityContext, needs a review from the security team. We make it enforceable: a CODEOWNERS entry for security packages, and an ArchUnit test in the starter's test kit that fails the build if a service class outside the starter calls SecurityContextHolder.setContext() or extends a filter base class in a security package, unless it carries an approved exemption.

Equally important is offering the alternative: a documented way to request a new mechanism in the starter. Teams write custom filters when the standard does not cover their case, so the platform team must respond quickly when it does not.

7Follow-up questions

Should we fix the custom filters first or migrate straight away?

For critical findings on internet-facing services, patch the filter immediately, because a migration takes longer and the risk is live. For everything else, migrate directly; fixing thirty filters only to delete them later is wasted effort.

How do you convince teams to delete code that works?

Show them their own probe results; seeing their service accept an expired token is more persuasive than a policy. Then make the migration small, with the starter, a guide and a pairing session, and track it in the same forum as other security findings.

What if a service needs a mechanism the starter does not have?

The team proposes it to the platform team, which adds it to the starter with a security review, so the next team gets it for free. A temporary custom filter is allowed only with an exemption and an expiry date.

8Conclusion

In this answer, we handled thirty hand-written authentication filters by first inventorying them with code search and probe requests, which typically reveals repeated validation mistakes. We replaced them with the OAuth2 resource server and one shared API key provider in a starter, proved each service with a shared test kit covering expiry, skew, audience, issuer, keys and headers, migrated in order of exposure and severity, and prevented new custom code with a review policy enforced by code owners and an architecture test.

What to say in the interviewAbout 100 seconds aloud

Open with

I'd treat it as a programme with five steps. First, an inventory: for each of the thirty services, which filter authenticates, which token types it accepts, what it checks, how it fails, whether it's internet-facing and who owns it.

Then, if they want more

Code search finds most of it, and I'd also send probe requests in test, expired token, wrong audience, wrong key, because behaviour matters more than intent.

Typically that shows the same handful of mistakes: no audience or issuer check, huge or no expiry tolerance, parse errors swallowed so the request continues anonymous, algorithm taken from the header, keys compared with equals and logged.

Second, replace rather than fix: Spring's OAuth2 resource server for JWTs and one reviewed API key provider, both in a shared starter, so a migration is adding a dependency, setting the audience and deleting the filter.

Third, a shared test kit every service runs: valid token, expired, inside and outside the sixty-second skew, nbf in the future, wrong audience and issuer, unknown key, alg none, missing and malformed header, missing scope. Running it before and after also shows which clients depended on a bug.

Fourth, prioritise by exposure and severity: internet-facing services with critical findings get a hotfix now, then waves. Fifth, prevent recurrence with a policy that new authentication code needs security review, enforced by code owners and an ArchUnit rule, plus a fast path for teams to get new mechanisms added to the starter.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Security reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Security review finds 30 services with hand-written authentication filters, each...": https://scrivio-interview-library.netlify.app/spring-boot/security-architecture/dedf61f8ea/