Package org.springframework.security.concurrent

# Class DelegatingSecurityContextRunnable

java.lang.Object
org.springframework.security.concurrent.DelegatingSecurityContextRunnable

All Implemented Interfaces:
  `Runnable`

public final class DelegatingSecurityContextRunnable extends Object implements Runnable

Wraps a delegate `Runnable` with logic for setting up a `SecurityContext` before invoking the delegate `Runnable` and then removing the `SecurityContext` after the delegate has completed.

If there is a `SecurityContext` that already exists, it will be restored after the `run()` method is invoked.

Since:
  3.2

## Constructor Summary

Constructors
Constructor
Description
`DelegatingSecurityContextRunnable(Runnable delegate)`
Creates a new `DelegatingSecurityContextRunnable` with the `SecurityContext` from the `SecurityContextHolder`.
`DelegatingSecurityContextRunnable(Runnable delegate, SecurityContext securityContext)`
Creates a new `DelegatingSecurityContextRunnable` with a specific `SecurityContext`.

## Method Summary

Modifier and Type
Method
Description
`static Runnable`
`create(Runnable delegate, SecurityContext securityContext)`
Factory method for creating a `DelegatingSecurityContextRunnable`.
`void`
`run()`
`void`
`setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy)`
Sets the `SecurityContextHolderStrategy` to use.
`String`
`toString()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Constructor Details

### DelegatingSecurityContextRunnable

public DelegatingSecurityContextRunnable(Runnable delegate, SecurityContext securityContext)
Creates a new `DelegatingSecurityContextRunnable` with a specific `SecurityContext`.

Parameters:
  `delegate` - the delegate `Runnable` to run with the specified `SecurityContext`. Cannot be null.
  `securityContext` - the `SecurityContext` to establish for the delegate `Runnable`. Cannot be null.

### DelegatingSecurityContextRunnable

public DelegatingSecurityContextRunnable(Runnable delegate)
Creates a new `DelegatingSecurityContextRunnable` with the `SecurityContext` from the `SecurityContextHolder`.

Parameters:
  `delegate` - the delegate `Runnable` to run under the current `SecurityContext`. Cannot be null.

## Method Details

### run

public void run()

Specified by:
  `run` in interface `Runnable`

### setSecurityContextHolderStrategy

public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy)
Sets the `SecurityContextHolderStrategy` to use. The default action is to use the `SecurityContextHolderStrategy` stored in `SecurityContextHolder`.

Since:
  5.8

### toString

public String toString()

Overrides:
  `toString` in class `Object`

### create

public static Runnable create(Runnable delegate, SecurityContext securityContext)
Factory method for creating a `DelegatingSecurityContextRunnable`.

Parameters:
  `delegate` - the original `Runnable` that will be delegated to after establishing a `SecurityContext` on the `SecurityContextHolder`. Cannot have null.
  `securityContext` - the `SecurityContext` to establish before invoking the delegate `Runnable`. If null, the current `SecurityContext` from the `SecurityContextHolder` will be used.
Returns: