java.lang.SecurityException: No security context bound to the current thread

Hi Team ,

Currently we have Bean inside that bean we are executing queries through and do some business logic with below exception

java.lang.SecurityException: No security context bound to the current thread
	at com.haulmont.cuba.core.sys.AppContext.getSecurityContextNN( ~[cuba-global-7.2.9.jar:7.2.9]
	at com.haulmont.cuba.core.sys.UserSessionSourceImpl.getUserSession( ~[cuba-core-7.2.9.jar:7.2.9]
	at ~[cuba-core-7.2.9.jar:7.2.9]
	at ~[cuba-core-7.2.9.jar:7.2.9]
	at ~[cuba-core-7.2.9.jar:7.2.9]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211]
	at sun.reflect.NativeMethodAccessorImpl.invoke( ~[na:1.8.0_211]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[na:1.8.0_211]
	at java.lang.reflect.Method.invoke( ~[na:1.8.0_211]
	at ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint( ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed( ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke( ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed( ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke( ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at com.sun.proxy.$Proxy440.load(Unknown Source) ~[na:na]
	at ~[cuba-core-7.2.9.jar:7.2.9]
	at$ ~[cuba-global-7.2.9.jar:7.2.9]
	at com.vtss.vtower.core.repo.CommittedCostRepository.loadProjectCommittedCostItemByProjectIdAndCostItemIdAndBudgetRequestId( [vtower-core-0.1-SNAPSHOT.jar:na]

I followed many topic related to authenticating background Threads

below is sample

 purchaseOrderItems.forEach(item ->
                Executors.newSingleThreadScheduledExecutor().submit(new SecurityContextAwareRunnable(() -> {

                    ProjectCommittedCost committed =
                                    item.getCostItem().getId(), budgetRequestId);

                    ProjectForecastingCost currentForecasting =
                            .filter(forecasting -> item.getCostItem().getId().equals(forecasting.getCostItem().getId()))

                    if (Objects.isNull(committed)) {
                        createNewCommittedCost(currentForecasting, orderRequest,item);
                    } else {
                        updateCurrentCommittedCost(currentForecasting, committed,item);

but i’m still getting No security context boun to the current thread

@Note :

I’m also adding
@Authenticated on the inside called functions like createNewCommittedCost

Where do you call purchaseOrderItems.forEach()?
Is it a screen controller’s method?

Hi @knstvk,

It is bean inside core module

OK, but how do you call this bean?
Are you sure it has a security context?

I’m calling it from ServiceBean

And the bean is inside Core module and it is not the only bean that i’m using form the service

The only difference between that bean and the others is that one i’m trying to trigger separate thread for each item inside the list of items While calling any function inside Thread Executor i’m getting the exception

OK, I see.
Your code looks correct to me. Could you make a test project where the problem is reproduced?

I can share the full @Bean which contains the code is that one is fine with you ,
cause It is middle of the application

Hi @knstvk

Is my suggestion is okay with you ?

No, I need a reproducible case to identify the problem.