Hey ho,
lately i got following exception on my local instance during the startup:
025-01-20 11:45:30,122 [restartedMain ][WARN ][model.bulkload.BulkImportProblemReporter][u:] - Error while importing models
java.lang.NullPointerException: Cannot invoke "org.springframework.security.core.Authentication.getPrincipal()" because "authentication" is null
at com.mgmtp.a12.uaa.authorization.AuthorizationService.createNewExecutionEnvironment(AuthorizationService.java:161)
at com.mgmtp.a12.uaa.authorization.AuthorizationService.lambda$resolveExecutionEnvironment$4(AuthorizationService.java:152)
at java.base/java.util.Optional.orElseGet(Optional.java:364)
The Authentication in the Spring SecurityContextHolder is null. This happens within my Eventlisteners which all listen to the event DataServicesInitializationFinishedEvent. They are using the BackendAuthenticationService with the superUser.
With debugging i can see, that the superUser will be set into the SecurityContextHolder. Unfortunately i can’t debug more, because this problem is non-deterministic and even marking a debug-point will “fix” the problem.
But following the code i can see, that the BackendAuthenticationService will set the Authentication to null in the finally block. I’m not quite sure if the SecurityContextHolder is threadsafe. If not then this can explain my problem, because the BackendAuthenticationService sets the Authentication to null, after another Eventlistener set it to the superUser but did not run their function. Then the function will be run with Authentication = null and the Exception happens. When disabling one of my three Eventlisteners, then the application starts up normally. This means these Eventlisteners are disturbing others, which supports my suggestion.
Does somebody else got a similar problem and already made an analysis ? I know there are possibilities to change the SecurityContextHolder behavior, so could this be a solution ? Is this known within the UAA component ?
Happy regards,
Tjorben