Interface SystemAuthenticator

All Known Implementing Classes:
SystemAuthenticatorImpl

public interface SystemAuthenticator
Provides ad-hoc authentication, i.e. allows you to execute code on behalf of the 'system' or a specified user.

Example usage:

     authenticator.runWithSystem(() -> {
         // valid current thread's authentication presents here
     });
 
or
     authenticator.begin();
     try {
         // valid current thread's authentication presents here
     } finally {
         authenticator.end();
     }
 
See Also:
  • Method Details

    • begin

      org.springframework.security.core.Authentication begin(@Nullable String login)
      Begins an authenticated code block.
      Saves the current thread session on a stack to get it back on end(). Subsequent end() method must be called in "finally" section.
      Parameters:
      login - user login. If null, authenticates as the 'system' user.
      Returns:
      populated Authentication object
    • begin

      org.springframework.security.core.Authentication begin()
      Authenticate as the 'system' user.
      Same as begin(String) with null parameter.
    • end

      void end()
      End of an authenticated code block.
      The previous session (or null) is set to security context. Must be called in "finally" section of a try/finally block.
    • withUser

      <T> T withUser(@Nullable String login, SystemAuthenticator.AuthenticatedOperation<T> operation)
      Execute code on behalf of the specified user and return a result.
      Parameters:
      login - user login. If null, the system session is used.
      operation - code to execute
      Returns:
      result of the execution
    • runWithUser

      void runWithUser(@Nullable String login, Runnable operation)
      Execute code on behalf of the specified user.
      Parameters:
      login - user login. If null, the system session is used.
      operation - code to execute
    • withSystem

      <T> T withSystem(SystemAuthenticator.AuthenticatedOperation<T> operation)
      Execute code as the 'system' user and return a result.
      Parameters:
      operation - code to execute
      Returns:
      result of the execution
    • runWithSystem

      void runWithSystem(Runnable operation)
      Execute code as the 'system' user.
      Parameters:
      operation - code to execute