【问题标题】:Apache Shiro integration and Netty ExecutionHandler/OrderedMemoryAwareThreadPoolExecutorApache Shiro 集成和 Netty ExecutionHandler/OrderedMemoryAwareThreadPoolExecutor
【发布时间】:2012-04-11 14:45:35
【问题描述】:

我刚刚按照文档中的建议在我的主要业务逻辑处理程序之前向我的服务器管道添加了一个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 的实现。

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

有没有人想过我可以在哪里/如何让 Shiro 参与进来?

谢谢, 马特

【问题讨论】:

  • 看看你如何集成 shiro 和 Netty 会很有趣。如果是开源的,可以分享一下链接吗?也许将其粘贴为 github gist。

标签: multithreading netty shiro


【解决方案1】:

Shiro 可以为您自动执行线程切换。

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

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

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

例如,调用useThis.submit(someRandomRunnable) 的未知组件不知道 Shiro 正在使用中,但 Shiro 主题仍将跨线程保留。查看相应的 JavaDoc 页面了解更多信息。

HTH!

莱斯

【讨论】:

  • 我已经在我的应用程序上下文中连接了SubjectAwareExecutorService,现在一切正常。非常感谢。这正是我一直在寻找的。干杯,马特
猜你喜欢
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
  • 2014-10-13
相关资源
最近更新 更多