@Override
  public Object invoke(MethodInvocation invocation) throws Throwable {

    Authentication savedAuthContext = SecurityContextHolder.getContext().getAuthentication();
    try {
      RunWithInternalSecurityContext securityAuthContextAnnotation = invocation.getMethod().getAnnotation(RunWithInternalSecurityContext
        .class);
      InternalAuthenticationToken authenticationToken = new InternalAuthenticationToken(securityAuthContextAnnotation
        .token());
      authenticationToken.setAuthenticated(true);
      SecurityContextHolder.getContext().setAuthentication(authenticationToken);
      return invocation.proceed();
    } finally {
      SecurityContextHolder.getContext().setAuthentication(savedAuthContext);
    }

  }
Allows running a given code within a security context authenticated with InternalAuthenticationToken.
This only works for instances created by Guice. If there's already an Authentication in current security context
that will be restored after calling the annotated method.
ambari-server/src/main/java/org/apache/ambari/server/security/authorization/internal/InternalAuthenticationInterceptor.java