title: How can I authenticate a system user for scheduled processes in Spring?
body: <p>we have a Quartz/Spring Batch job, that for audit logging purposes we'd like to have it "authenticated" as a system user. Some of our methods rely on fetching the SecurityContext to do this. The ways of running this job are trusted (or authenticated). We don't want to actually use a password or other token (since the process is basically always spawned by quartz).</p>
  
  <p>I tried this</p>
  
  <pre><code>private void authenticate() {
      UserDetails admin = userDetailsService.loadUserByUsername( "admin" );
  
      RunAsUserToken token = new RunAsUserToken(
              UUID.randomUUID().toString(), admin, admin.getAuthorities(), null , null );
  
      Authentication user = authenticationManager.authenticate( token );
  
      if ( user.isAuthenticated() ) {
          SecurityContext sc = new SecurityContextImpl();
          sc.setAuthentication( user );
          SecurityContextHolder.setContext( sc );
      }
  }
  </code></pre>
  
  <p>but it resulted in</p>
  
  <pre><code>org.springframework.security.authentication.ProviderNotFoundException: No AuthenticationProvider found for org.springframework.security.access.intercept.RunAsUserToken
  </code></pre>
  
  <p>and I'm not sure what some of RunAsUserToken parameters do (e.g. key) or what I should be giving it in regards to Credentials.</p>
  
  <p>How can I authenticate or otherwise set the security context as if it was authenticated as this user?</p>