Scrivio Interview library
LeadershipAdvanced6 min read · updated 12 November 2026

A team wants to implement audit logging, tenant filtering and authorisation checks with custom aspects across all services. What guidance would you give?

I would tell the team that aspects are a good fit for behaviour that must be applied the same way everywhere, but they hide control flow, so for authorisation and tenant filtering they should first use the mechanisms the frameworks already provide: Spring Security method security, Hibernate filters or row-level security, and Micrometer observations. Where a custom aspect is still the right tool, such as audit logging, its pointcut should match an explicit annotation rather than package wildcards, it should be tested both on its own and through integration tests that cover the self-invocation limit, and it should ship in a documented shared starter so every service behaves the same way.

Why this question is asked

A request like this usually comes from a good place: the team has seen the same audit, tenant and permission code copied into fifty services and wants to do it once. Aspects can do that well. But these three concerns are also the ones where a silent gap is most expensive, because a missed audit record fails a compliance check, a missed tenant filter leaks one customer's data to another, and a missed authorisation check is a security hole. Interviewers ask this to see whether we can weigh convenience against visibility and steer a team toward something that stays correct for years. In this answer, we will recap what aspects give us and what they cost, then go through each of the three concerns, and finish with the rules for pointcuts, testing and packaging.

1What Aspects Give and What They Cost

An aspect lets us write cross-cutting behaviour once and apply it to many methods with a pointcut, an expression that selects which methods are advised. In Spring, the aspect runs inside a proxy that wraps the bean. The benefit is consistency: every matched method gets exactly the same behaviour, and business code stays free of repeated boilerplate.

The cost is that the behaviour is invisible where it happens. A developer reading a service method cannot see that it is audited or filtered by tenant; they have to know that an aspect somewhere matches it. Spring's proxy model adds sharp edges on top: calls on this skip the proxy, private and final methods are never advised, and a change of package or class name can silently move a method out of a pointcut. For logging timing data, a silent gap is an annoyance. For security and tenant isolation, it is an incident. So the guidance depends on the concern.

2Authorisation: Use Spring Security Method Security

Authorisation checks already have a mature, aspect-based framework: Spring Security's method security, enabled with @EnableMethodSecurity. Annotations such as @PreAuthorize("hasRole('ADMIN')") or expressions that call a bean, like @PreAuthorize("@orders.canView(#id, authentication)"), are evaluated by Spring Security's own interceptors. It integrates with the security context, produces the standard access-denied responses, is documented and tested by the Spring team, and every Spring developer can recognise it.

A home-grown authorisation aspect would duplicate all of that, with less testing and less familiarity. So my guidance is to use method security for method-level rules, and request-level rules in the security filter chain for whole URL patterns. A custom AuthorizationManager can still hold company-specific logic, plugged into the framework rather than replacing it.

3Tenant Filtering: Push It Down to the Data Layer

Tenant filtering means making sure each query only returns rows that belong to the current tenant. Doing it with an aspect around service methods is fragile: it depends on every data access going through an advised method, and a single repository call from an unadvised path, a native query or a batch job returns every tenant's data.

It is safer to enforce it where the data is read. Hibernate filters, defined with @FilterDef and @Filter and enabled on the session for the current tenant, add the tenant condition to every query for that entity. Hibernate 6 also has @TenantId, which applies the tenant automatically through its multi-tenancy support. Stronger still, PostgreSQL row-level security makes the database refuse rows from other tenants regardless of what the application asks for. The aspect, if any, shrinks to setting the tenant at the start of the request, which is better done in a servlet filter anyway.

Recommended mechanism for each concern
ConcernPreferCustom aspect?
AuthorisationSpring Security @PreAuthorize, filter chain rulesNo, plug logic into the framework
Tenant filteringHibernate filters or @TenantId, database row-level securityNo, enforce at the data layer
Timing and tracingMicrometer @Observed, Observation API, tracingNo
Audit logging of business actionsexplicit @Audited annotation and a shared aspectYes, with narrow pointcuts and tests
Use the framework where one exists; write an aspect only for what is truly company-specific.

4Audit Logging: A Reasonable Place for a Custom Aspect

Audit logging of business actions, such as who approved a refund and when, is company-specific and often has no ready-made framework, so a shared aspect is reasonable. For technical observations like timings and traces, Micrometer's Observation API and @Observed already do the job and feed metrics and tracing systems, so the audit aspect should not try to be a monitoring tool as well.

The audit aspect should be driven by an explicit annotation, so each audited operation is visibly marked in the code.

Java
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Audited { String action(); }
@Aspect
public class AuditAspect {
private final AuditWriter writer;
AuditAspect(AuditWriter writer) { this.writer = writer; }
@AfterReturning("@annotation(audited)")
public void record(JoinPoint jp, Audited audited) {
writer.write(audited.action(), currentUser(), jp.getArgs());
}
}
// In a service:
@Audited(action = "refund.approve")
public void approveRefund(long refundId) { ... } // audit record written after success

Notice the pointcut: it matches the annotation, not a package such as execution(* com.company..*(..)). A package wildcard proxies almost every bean, costs CPU on every call, and silently stops matching when code moves. An annotation is explicit, survives renames and tells the reader what happens.

5Testing and Packaging the Aspects

Each aspect needs two kinds of tests. Unit tests exercise the aspect directly, for example with AspectJProxyFactory to wrap a small test object, checking that the right record is written and that failures behave as intended. Integration tests start a real Spring context for a sample service and assert the end result: an audit row exists after approveRefund(). The integration tests should include the self-invocation case, a method that calls an @Audited method on this, so the team sees and documents that it is not audited.

Finally, the aspect lives in a shared starter: a small library with auto-configuration that registers the aspect, its annotation and its configuration properties. The starter comes with a short page describing what each annotation does, its ordering relative to transactions, and the known limits. Services opt in by adding the dependency, and every service gets the same version of the behaviour.

6Follow-up questions

Should the audit record be written inside or outside the business transaction?

It depends on the requirement. If the audit must exist only when the action committed, write it in the same transaction or after commit with a TransactionalEventListener. If attempts must be audited even when they fail, write it in a separate transaction. The aspect's order relative to the transaction advisor must match that decision and be documented.

Why not let each team write its own aspects?

Because then the company has fifty slightly different audit formats and security behaviours, and nobody can say with confidence what is covered. A shared starter gives one behaviour to test, review and improve, while teams still choose which methods to annotate.

Is an aspect ever right for authorisation?

Spring Security method security is itself built on the same interceptor mechanism, so the idea is sound. The guidance is not to rebuild that framework; when company logic is needed, plug it in as a bean called from @PreAuthorize or as a custom AuthorizationManager.

7Conclusion

In this answer, we saw that aspects give consistency but hide control flow, which matters most for audit, tenant isolation and security. For authorisation we prefer Spring Security method security, for tenant filtering Hibernate filters or database row-level security, and for timing Micrometer observations. A custom aspect fits company-specific audit logging, with an explicit annotation pointcut instead of package wildcards, unit and integration tests that include the self-invocation limit, and a documented shared starter so every service behaves the same.

What to say in the interviewAbout 80 seconds aloud

Open with

I would support the goal, which is consistent cross-cutting behaviour, but steer the how. Aspects are great at doing the same thing everywhere, but they hide control flow, and for authorisation and tenant isolation a silent gap is a security incident.

Then, if they want more

So first I would ask them to use what the frameworks already provide. Authorisation should use Spring Security method security with @PreAuthorize and, where needed, a custom bean or AuthorizationManager for company rules.

Tenant filtering should be enforced at the data layer with Hibernate filters or @TenantId, ideally backed by database row-level security, because an aspect around service methods misses native queries and batch jobs. Timing and tracing should use Micrometer observations. Audit logging of business actions is a reasonable custom aspect.

For that, I would insist on an explicit annotation like @Audited and an @annotation pointcut rather than package wildcards, which are slow and break silently when code moves. I would want unit tests of the aspect and integration tests that check the audit record actually appears, including a test showing that a self-call is not audited.

And I would package it as a shared starter with auto-configuration and a short document covering ordering with transactions and known limits, so every service gets the same behaviour.

About this answer

Last updated
12 November 2026
Versions it names
Hibernate 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.
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 "A team wants to implement audit logging, tenant filtering and authorisation checks with...": https://scrivio-interview-library.netlify.app/spring-boot/aop/40dc0c0ce2/