Search code examples
springaspectjspring-aop

How to define a pointcut on ServerSecurityContextRepository?


I'm trying to create an aspect to execute code before ServerSecurityContextRepository.save(ServerWebExchange exchange, SecurityContext context) is called, but I couldn't get my pointcut working.

Here is what I tried so far:

@Aspect
@Component
@Slf4j
public static class ServerSecurityContextRepositoryAspect {

    @Pointcut("within(org.springframework.security.web.server.context.ServerSecurityContextRepository+) && execution(* *.save(..))")
    public void securityContextSaved() {
    }

    @Before("securityContextSaved()")
    public void beforeSecurityContextSaved(JoinPoint jp) {
        log.error("%d".formatted(jp.getArgs().length));
        if (jp.getArgs().length != 2) {
            log.warn("oups");
        }
        if (jp.getArgs()[1] == null) {
            log.warn("SecurityContext destroyed");
        } else {
            log.warn("SecurityContext saved");
        }
    }
}

Any suggestion?


Solution

  • I just figured it out: ServerSecurityContextRepository is not exposed as a bean in my application (instantiated with a new in my code) => no chance that Spring can instrument it with AOP...