Authentication is null while implementing TaskListener

Hi guys,

Our use-case is: We have some business cases when a property of a certain task changes. Therefore, we created a class, which implements org.camunda.bpm.engine.delegate.TaskListener, and override the notify method to call some business code. In this method, we need to retrieve the current authenticated user and we are retrieving it from SecurityContextHolder

private String getCurrentUsername() {
	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
	ExtendedUser user = (ExtendedUser) authentication.getPrincipal();
	return user.getUsername();
}

Everything works perfectly in version 2023.02, however, after upgrading to version 2023.06, there is an exception:

Exception while invoking TaskListener: Cannot invoke \"org.springframework.security.core.Authentication.getPrincipal()\" because \"authentication\" is null'","code":0}"

Do you know how to handle this problem? Thanks in advance.

Hi @hieu-far-field,

Thanks for your question. Could you provide some more info:

  1. Which A12WF version exactly are you using?
  2. Are you using old or new architecture (e.g. workflows-camunda-distribution or workflows-camunda-cbr-distribution)?
  3. Which user would you expect here? A technical user or an actual user?
  4. How is the task with the listener reached?
  5. Did you change anything with respect to the asynchronous continuations in the BPMN model (async before / after) between your working version and your current version?

Thanks,
Peter

Hi @peter-smooth-cursor,

  1. I am using version 10.2.4
  2. I am using new architecture
  3. I expect an actual user (the currently logged-in user)
  4. Here is my BPMN model: OrderProcessWithoutInput.bpmn (6.3 KB), I am defining a task listener in “EnterOrder” task for the assignment event with a delegate expression. Inside my code, I already created a bean, which implements TaskListener to handle my logic
  5. My project is using provided BPMN models from the full stack template project. Here is my old version model: ParallelGatewayProcess.bpmn (7.6 KB) and my current model is:
    OrderProcessWithoutInput.bpmn (6.3 KB). Could you please help me have a look at it since I do not understand totally what asynchronous continuations are?

Besides that, I found a way to get the currently logged-in user by using AuthenticationRestClient from UAA.

Hi @hieu-far-field,

Thanks for all the info! Your BPMN model looks fine so I’d suggest looking at the following:

  1. Just to make sure: the currently logged in user triggers the assignment, right? For example, by clicking a button in the UI.

  2. Are you using DELEGATED mode for communication from Data Services to Camunda?

    mgmtp.a12.uaa.authentication.client.rest.authentication-type=DELEGATED
    

    Delegated mode means that the user token will be forwarded from Data Services to Camunda (the UAA REST Client will send along it as header).

Hope that helps!
Peter

Hi @peter-smooth-cursor,

Thanks a lot for your information. Here are my answers to your questions:

  1. Yes, the currently logged-in user triggers the assignment.
  2. No, we are using LOCAL mode. Here is our config:
mgmtp.a12.uaa.authentication.client.rest.authentication-type=LOCAL
mgmtp.a12.uaa.authentication.client.rest.authentication-configuration.username=admin
mgmtp.a12.uaa.authentication.client.rest.authentication-configuration.password=admin

Dear @hieu-far-field , has the issue been solved? Or do you need some additional information before the thread can be closed? In case your issue is solved, please, use a checkbox icon under the right proposed solution so that other users can see what exactly helped.
Thanks ahead!
Katerina form the Discourse Team

Hi @katerina-icy-token, we solved this problem and I selected the right solution by using the checkbox icon. Thanks for reminding me.