Apache Shiro集成和Netty ExecutionHandler / OrderedMemoryAwareThreadPoolExecutor

时间:2012-03-28 02:05:27

标签: multithreading netty shiro

我刚刚在我的主要业务逻辑处理程序之前向我的服务器管道添加了ExecutionHandler,如文档中所建议的那样。

我使用Apache Shiro http://shiro.apache.org/来保证安全。它工作正常,直到我添加了ExecutionHandler

问题
Shiro的执行上下文绑定到您获取Subject对象的当前线程。因此,如果在工作线程中获得Subject,但业务逻辑在单独的ExecutionHandler托管线程中执行,那么就Shiro而言,两个执行上下文将不会连接。因此,ExecutionHandler线程中的Shiro将无法知道Subject实际上已经过身份验证。所以我收到身份验证错误。

可以将给定的SubjectRunnable相关联,然后再将其传递给Executor.execute(),以便维护安全上下文。请参阅:http://shiro.apache.org/subject.html

基于此,我认为需要找到一种方法将当前的Shiro SubjectExecutionHandler Runnable相关联。

我仍在尝试完全理解ExecutionHandlerOrderedMemoryAwareThreadPoolExecutor实现。

基本上我需要在subject.associateWith(aRunnable)传递给aRunnable之前致电Executor.execute(aRunnable)

有没有人想过我在哪里/怎么把Shiro挂钩?

谢谢, 马特

1 个答案:

答案 0 :(得分:14)

Shiro可以为你自动化线程切换。

您应该可以使用开箱即用的SubjectAwareExecutorSubjectAwareExecutorServiceSubjectAwareScheduledExecutorService实现之一。你可以包装将执行Runnables的实际ExecutorService,你很好。例如:

ExecutorService myExistingExecutorService = //get from somewhere
ExecutorService useThis = new SubjectAwareExecutorService(myExistingExecutorService);

您可以在应用程序的任何位置“注入”或配置useThis实例,并且调用代码不需要知道Shiro存在。

例如,一个不知道的组件调用useThis.submit(someRandomRunnable)并不知道Shiro正在使用,但是Shiro Subject仍将保留在线程之间。查看相应的JavaDoc页面了解更多信息。

HTH!

莱斯