Class SecurityContextPropagatingExecutor

java.lang.Object
com.fayupable.logged.spring.security.SecurityContextPropagatingExecutor
All Implemented Interfaces:
Executor

public final class SecurityContextPropagatingExecutor extends Object implements Executor
Executor decorator that carries the submitting thread's Spring Security SecurityContext over to whatever thread actually runs the submitted task.

com.fayupable.logged.spring.aspect.FlowContextPropagatingExecutor solves the equivalent problem for this library's own call-chain tracking. That class deliberately does not also propagate SecurityContext, so that it stays usable in applications with no Spring Security on the classpath at all. This class fills that specific gap for applications that do have Spring Security: without it, a nested @Logged call made from inside work submitted through a plain, unwrapped executor loses the authenticated user entirely on the executor thread, and this library's caller identity resolution silently falls back to an IP address or "unknown" for that nested call — losing *who* made the call, not just a secondary detail.

Wrap once, alongside FlowContextPropagatingExecutor if both are needed, by nesting decorators around the same delegate:


 Executor propagating = new FlowContextPropagatingExecutor(
         new SecurityContextPropagatingExecutor(realExecutor));
 

This class is deliberately not wired in automatically anywhere in this library's auto-configuration: a consuming application decides which executor should carry the current authenticated user across a thread hand-off, the same way it decides which methods get @Logged in the first place.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Wraps delegate so that every task submitted through this executor carries the submitting thread's SecurityContext into whichever thread delegate actually runs it on.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    execute(@NonNull Runnable command)
    Captures the calling thread's current SecurityContext and submits a wrapped task to the delegate executor that restores it before running command and restores the executor thread's own previous security context again afterward, regardless of whether command completes normally or throws.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SecurityContextPropagatingExecutor

      public SecurityContextPropagatingExecutor(Executor delegate)
      Wraps delegate so that every task submitted through this executor carries the submitting thread's SecurityContext into whichever thread delegate actually runs it on.
      Parameters:
      delegate - the executor that will actually run submitted tasks
  • Method Details

    • execute

      public void execute(@NonNull Runnable command)
      Captures the calling thread's current SecurityContext and submits a wrapped task to the delegate executor that restores it before running command and restores the executor thread's own previous security context again afterward, regardless of whether command completes normally or throws.
      Specified by:
      execute in interface Executor
      Parameters:
      command - the task to run