Quick revision
The short answer
We implement AuthorizationManager<RequestAuthorizationContext>, read the current time from an injected Clock, check the authentication's authorities for report:read, and return an AuthorizationDecision that is true or false. We plug it in with requestMatchers("/reports/**").access(manager). A cleaner version keeps the time rule in its own manager and combines it with AuthorityAuthorizationManager.hasAuthority("report:read") through AuthorizationManagers.allOf, the composable replacement for the AccessDecisionManager and voters of older Spring Security.
What to remember
In this answer, we wrote an AuthorizationManager<RequestAuthorizationContext> that reads time from an injected Clock, checks report:read and returns an AuthorizationDecision, noting that newer versions add an authorize method returning AuthorizationResult. We registered it with requestMatchers("/reports/**").access(), split it into a reusable business hours manager combined with AuthorizationManagers.allOf, tested it with fixed clocks, and saw how it replaced voters and AccessDecisionManager.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We implement AuthorizationManager<RequestAuthorizationContext>, read the current time from an injected Clock, check the authentication's authorities for report:read, and return an AuthorizationDecision that is true or false. We plug it in with requestMatchers("/reports/**").access(manager). A cleaner version keeps the time rule in its own manager and combines it with AuthorityAuthorizationManager.hasAuthority("report:read") through AuthorizationManagers.allOf, the composable replacement for the AccessDecisionManager and voters of older Spring Security.
Why this question is asked
Most authorization rules fit the built-in helpers: authenticated(), hasRole(), hasAuthority(). Sooner or later a rule arrives that does not, such as a time window, an IP range or a feature flag, and teams are tempted to write a servlet filter or put checks in controllers. Interviewers ask this coding question to see whether we know the proper extension point in Spring Security 6 and later, and whether we write it in a testable way. In this answer, we will recap what an AuthorizationManager is, write the business hours manager, plug it into the filter chain, split it into composable pieces, test it with a fixed clock, and see how it replaced the old voters.
1A Quick Recap: What an AuthorizationManager Does
In Spring Security 6 and later, every authorization decision is made by an AuthorizationManager<T>. It receives a Supplier of the current Authentication and an object describing what is being accessed, and returns a decision. For URL rules, that object is a RequestAuthorizationContext, which holds the HttpServletRequest and any path variables. Each rule we write in authorizeHttpRequests, such as hasRole("ADMIN"), is itself an AuthorizationManager built for us.
The Authentication arrives as a Supplier so that it is only looked up when a manager actually needs it. A rule like permitAll() never calls get(), which saves work for public endpoints.
2Writing the Business Hours Manager
Our rule: access to /reports/** is allowed on weekdays from 09:00 to 18:00 in the company's time zone, and only for users with the authority report:read. We inject a Clock instead of calling LocalTime.now() directly, so tests can choose the time.
Notice the time zone. A server in UTC and users in Berlin disagree about when 18:00 is, so the zone belongs in the Clock bean, not hidden in the code. Notice also that anonymous users are refused naturally: Spring Security gives them an anonymous authentication with only ROLE_ANONYMOUS, which never contains report:read.
The method signature depends on your version. Spring Security 6.4 added an authorize method returning AuthorizationResult, an interface that AuthorizationDecision implements, and deprecated check; newer versions expect us to implement authorize instead. The logic is identical, so check your version and implement the method it asks for.
3Plugging It Into Request Authorization
The access() method accepts any AuthorizationManager<RequestAuthorizationContext>, so we inject our bean into the filter chain configuration and attach it to the pattern.
6 messages in order. Play them, or step through them one at a time.
On a false decision the filter throws AccessDeniedException and the controller is never called.
A denied decision throws AccessDeniedException inside the AuthorizationFilter, which the ExceptionTranslationFilter turns into a 403 for an authenticated user or a login challenge for an anonymous one.
4Composing Small Managers Instead of One Big One
Our manager mixes two unrelated rules. If another endpoint needs business hours with a different authority, we would copy the time logic. Spring Security provides AuthorizationManagers.allOf and anyOf to combine managers, so each class can do one thing.
allOf grants only when every manager grants, and anyOf grants when at least one does. Built-in managers like AuthorityAuthorizationManager are already tested, so our own code shrinks to the one rule nobody else provides. The same managers can be reused in method security, which also runs on AuthorizationManager in Spring Security 6 and later.
5Testing With a Fixed Clock
Because time comes from the Clock, unit tests need no waiting and no mocking framework. 6 November 2026 is a Friday.
The boundaries deserve their own tests: exactly 09:00 should be allowed and exactly 18:00 refused, which is easy to get wrong with isAfter and isBefore. A MockMvc test with a test Clock bean then proves the wiring in the filter chain.
6How This Replaced AccessDecisionManager and Voters
Before Spring Security 6, the same rule would be an AccessDecisionVoter returning ACCESS_GRANTED, ACCESS_DENIED or ACCESS_ABSTAIN, registered with an AccessDecisionManager such as AffirmativeBased or UnanimousBased, which counted the votes. It worked, but the configuration was verbose and the voting strategy applied to everything at once.
AuthorizationManager replaced that model and is the standard in Spring Security 6; the voter classes are deprecated. A manager is a single function from authentication and object to a decision, it can be attached to one pattern, and combination is explicit with allOf and anyOf instead of a global voting strategy. When migrating old voters, each one usually becomes one small manager.
7Follow-up questions
Why not write a servlet filter that checks the time?
A filter works but sits outside Spring Security's authorization model. It does not appear next to the other rules, it produces errors in a different way, and it cannot be reused in method security. An AuthorizationManager gets the standard 403 handling, audit events and composition for free.
Should a denied business-hours request return 403?
Usually yes, but the user deserves an explanation. Since the decision object carries no message to the client, we add a custom AccessDeniedHandler that, for /reports/**, returns a body saying reports are available weekdays from 09:00 to 18:00.
What about public holidays?
Put a holiday calendar behind an interface and inject it alongside the Clock, so the manager asks isBusinessDay(date). The calendar can come from configuration or a service, and tests can supply a fixed one.
8Conclusion
In this answer, we wrote an AuthorizationManager<RequestAuthorizationContext> that reads time from an injected Clock, checks report:read and returns an AuthorizationDecision, noting that newer versions add an authorize method returning AuthorizationResult. We registered it with requestMatchers("/reports/**").access(), split it into a reusable business hours manager combined with AuthorizationManagers.allOf, tested it with fixed clocks, and saw how it replaced voters and AccessDecisionManager.
Open with
I would implement AuthorizationManager of RequestAuthorizationContext. In the check method, I get the current time from an injected Clock, configured with the business time zone, check that it is a weekday between 09:00 inclusive and 18:00 exclusive, and check the authentication's authorities for report:read, then return new AuthorizationDecision with the combined result.
Then, if they want more
In Spring Security 6.4 and later there is also an authorize method returning AuthorizationResult and check is deprecated, so I would implement whichever the version expects. To plug it in, I inject it into the SecurityFilterChain bean and write requestMatchers("/reports/**").access(reportAccess), before anyRequest.
Actually, I prefer to keep the time rule on its own and compose it: AuthorizationManagers.allOf with AuthorityAuthorizationManager.hasAuthority("report:read") and the business hours manager, so other endpoints can reuse the time rule with a different authority. Because of the Clock, unit tests use Clock.fixed for a Friday at 10:00, a Friday at 18:30 and a Saturday, including the exact boundaries.
This is the Spring Security 6 replacement for AccessDecisionManager and voters, which are deprecated: one small function per rule instead of a global voting strategy.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Security 6
- 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 "Write a custom AuthorizationManager that allows access to /reports/** only during...": https://scrivio-interview-library.netlify.app/spring-boot/authorization/bf1b7ca87a/