Class SecurityContextHelper

java.lang.Object
io.jmix.core.security.SecurityContextHelper

@NullMarked public class SecurityContextHelper extends Object
Helper class to get/set Authentication in the current SecurityContext.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static @Nullable org.springframework.security.core.Authentication
    Returns current Authentication or null if the current context has no Authentication
    static void
    installContext(org.springframework.security.core.context.SecurityContext context)
    Makes the given context current for this thread only, without modifying the previously current context instance.
    static void
    restoreContext(@Nullable org.springframework.security.core.context.SecurityContext previous)
    Reverts the matching installContext(SecurityContext) call.
    static void
    setAuthentication(@Nullable org.springframework.security.core.Authentication authentication)
    Sets the Authentication in the current SecurityContext.

    Methods inherited from class java.lang.Object

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

    • SecurityContextHelper

      public SecurityContextHelper()
  • Method Details

    • getAuthentication

      public static @Nullable org.springframework.security.core.Authentication getAuthentication()
      Returns current Authentication or null if the current context has no Authentication
    • setAuthentication

      public static void setAuthentication(@Nullable org.springframework.security.core.Authentication authentication)
      Sets the Authentication in the current SecurityContext.

      Note that this method modifies the current SecurityContext instance, which may be shared with other threads of the same HTTP session. To execute code on behalf of another user for a limited time, use SystemAuthenticator instead.

    • installContext

      public static void installContext(org.springframework.security.core.context.SecurityContext context)
      Makes the given context current for this thread only, without modifying the previously current context instance. If the installed SecurityContextHolderStrategy supports ThreadSecurityContextOverride, the context takes precedence over other sources such as the Vaadin session. Also sets up the logging MDC.

      Must be paired with restoreContext(SecurityContext) in a "finally" block:

           SecurityContext previous = SecurityContextHolder.getContext();
           SecurityContextHelper.installContext(context);
           try {
               // ...
           } finally {
               SecurityContextHelper.restoreContext(previous);
           }
       
    • restoreContext

      public static void restoreContext(@Nullable org.springframework.security.core.context.SecurityContext previous)
      Reverts the matching installContext(SecurityContext) call.
      Parameters:
      previous - the context that was current before installContext(), or null to clear the context