Package io.jmix.core.security
Class SecurityContextHelper
java.lang.Object
io.jmix.core.security.SecurityContextHelper
Helper class to get/set Authentication in the current
SecurityContext.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable org.springframework.security.core.AuthenticationReturns current Authentication or null if the current context has no Authenticationstatic voidinstallContext(org.springframework.security.core.context.SecurityContext context) Makes the given context current for this thread only, without modifying the previously current context instance.static voidrestoreContext(@Nullable org.springframework.security.core.context.SecurityContext previous) Reverts the matchinginstallContext(SecurityContext)call.static voidsetAuthentication(@Nullable org.springframework.security.core.Authentication authentication) Sets the Authentication in the currentSecurityContext.
-
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 currentSecurityContext.Note that this method modifies the current
SecurityContextinstance, which may be shared with other threads of the same HTTP session. To execute code on behalf of another user for a limited time, useSystemAuthenticatorinstead. -
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 installedSecurityContextHolderStrategysupportsThreadSecurityContextOverride, 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 matchinginstallContext(SecurityContext)call.- Parameters:
previous- the context that was current beforeinstallContext(), or null to clear the context
-