0

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?

ch4mp
  • 6,622
  • 6
  • 29
  • 49

1 Answers1

0

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...

ch4mp
  • 6,622
  • 6
  • 29
  • 49
  • 1
    Yes, that would also have been my first guess in the comment I intended to write. Generally, please always share at least aspect, target class (both with package names) and Spring configuration when asking Spring AOP questions. Ideally, share a full, minimal reproducer on GitHub, too. It will yield good, spot-on answers quicker than speculative ones based on educated guesses. It is kind of hard to reason about invisible code targeted by aspects. There are dozens of reasons why an aspect might not match. But I am glad you found out by yourself in this case. – kriegaex Aug 11 '23 at 00:04