gpt4 book ai didi

java - 是/如何从托管 Spring bean 向线程提供 SecureContext

转载 作者:行者123 更新时间:2023-12-03 15:24:18 40 4
gpt4 key购买 nike

我希望能够异步启动 Alfresco 节点的创建,以防止创建瓶颈。

为此,我尝试使用初始化为 10 的 ScheduledExecutorService

执行过程如下:

来 self 的UploadServlet --> (public) UploadManager#createNodeRef(SOME_PARAMS) --> (private synchronized) UploadManager#createNodeRef( SOME_PARAMS)

事务在 UploadServlet 中管理

代码:

private synchronized NodeRef createNodeRef(ARGS) {
//
// Creation Code using other Alfresco/Spring beans
//
return RETURN_VALUE;
}

异步代码(我正在尝试做的)

    private NodeRef makeAsyncNodeCreation(ARGS) {
final ScheduledFuture<NodeRef> scheduledFuture = nodeCreationExecutor.schedule(new Callable<NodeRef>() {
@Override
public NodeRef call() throws Exception {
// HERE I MAKE THE EFFECTIVE NODE CREATION
return createNodeRef(filename, type, parentNode, label, kinematic);
}
}, MAX_NODE_CREATION_DELAY_IN_SECONDS, SECONDS);

try {
return scheduledFuture.get();
} catch (InterruptedException e) {
throw new AsyncNodeCreationException(e);
} catch (ExecutionException e) {
throw new AsyncNodeCreationException(e);
}
}

然后是公共(public)方法

public NodeRef create(ARGS) {
return makeAsyncNodeCreation(ARGS);
}

问题

我有以下异常

net.sf.acegisecurity.AuthenticationCredentialsNotFoundException: A valid SecureContext was not provided in the RequestContext

我的问题为什么我的异步调用中不再存在 SecureContext?

最佳答案

因为 SecurityContextSecurityContextHolder 保存在 ThreadLocal 变量(默认策略)中,因此是当前线程,您需要传递 SecurityContext 到 Callable,因为它在不同的线程中运行,然后将它注入(inject)到 SecurityContextHolder 中它自己的 ThreadLocal。

我希望这个解决方案适用于 Acegi Security:

 // Get the SecurityContext hold by the main thread
final SecurityContext securityContext = SecurityContextHolder.getContext();

final ScheduledFuture<NodeRef> scheduledFuture = nodeCreationExecutor.schedule(new Callable<NodeRef>() {

@Override
public NodeRef call() throws Exception {

// Inject the securityContext
SecurityContextHolder.setContext(securityContext);

// HERE I MAKE THE EFFECTIVE NODE CREATION
NodeRef noderef = createNodeRef(filename, type, parentNode, label, kinematic);

// Cleaning...the thread may be recycled
SecurityContextHolder.setContext(null);

return noderef;

}
}, MAX_NODE_CREATION_DELAY_IN_SECONDS, SECONDS);

关于java - 是/如何从托管 Spring bean 向线程提供 SecureContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27838631/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com