1 package org
.argeo
.security
.core
;
3 import org
.argeo
.security
.SystemExecutionService
;
4 import org
.springframework
.core
.task
.SimpleAsyncTaskExecutor
;
5 import org
.springframework
.core
.task
.TaskExecutor
;
6 import org
.springframework
.security
.Authentication
;
7 import org
.springframework
.security
.AuthenticationManager
;
8 import org
.springframework
.security
.context
.SecurityContext
;
9 import org
.springframework
.security
.context
.SecurityContextHolder
;
11 public class KeyBasedSystemExecutionService
implements SystemExecutionService
{
12 private AuthenticationManager authenticationManager
;
13 private String systemAuthenticationKey
;
15 public void executeAsSystem(Runnable runnable
) {
16 wrapWithSystemAuthentication(runnable
).run();
19 public TaskExecutor
createSystemAuthenticatedTaskExecutor() {
20 return new SimpleAsyncTaskExecutor() {
21 private static final long serialVersionUID
= -8126773862193265020L;
24 public Thread
createThread(Runnable runnable
) {
26 .createThread(wrapWithSystemAuthentication(runnable
));
32 protected Runnable
wrapWithSystemAuthentication(final Runnable runnable
) {
33 return new Runnable() {
36 SecurityContext securityContext
= SecurityContextHolder
38 Authentication auth
= authenticationManager
39 .authenticate(new InternalAuthentication(
40 systemAuthenticationKey
));
41 securityContext
.setAuthentication(auth
);
45 // remove the authentication
46 securityContext
.setAuthentication(null);
52 public void setAuthenticationManager(
53 AuthenticationManager authenticationManager
) {
54 this.authenticationManager
= authenticationManager
;
57 public void setSystemAuthenticationKey(String systemAuthenticationKey
) {
58 this.systemAuthenticationKey
= systemAuthenticationKey
;