如何在代码中的任意点访问 JAAS 角色?
我想访问用户的完整模型及其在 SOAP 应用程序中的角色。 例如,我可能想知道名为“Fred”的用户的角色。
如何访问某种全局 JAAS 注册表并执行(伪代码)globalRegistry.getUser("Fred").getPrincipals()? (请注意,在 JAAS 中,角色由 Principal 表示。)
我知道如何从 Subject 中获取 Principal >LoginContext,但这有两个问题。
- 仅在登录时,我不想自己编写上述注册表并存储 Subject 和 Principal 对象,因为它们已经由应用程序服务器。
- 最好,即使 Fred 不是当前用户,我也希望能够访问此信息。
我正在使用 Jetty,但我认为这些行为是 JAAS 的标准行为。
I want to access the full model of users with their roles in my SOAP app. For example, I might want to know the role of a user called "Fred."
How do I reach into some sort of global JAAS registry and do (pseudocode) globalRegistry.getUser("Fred").getPrincipals()? (Note that in JAAS, a role is represented by a Principal.)
I know how to get the Principal of the Subject from the LoginContext, but that has two problems.
- It is only at the moment of login, and I'd prefer not to code the aforementioned registry and store the Subject and Principal objects myself, as they are already stored by the appserver.
- Preferably, I want to be able to access this information even when Fred is not the current user.
I am using Jetty, but I presume that these behaviors are standard to JAAS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我看到的一种模式是:
本质上,这会找到当前与当前线程关联的主题,并询问其主体。
使用此功能的一个示例是 Apache Jackrabbit 的 RepositoryImpl。 它位于
extendAuthentication
方法中,该方法的作用是确定当前线程在创建新会话时拥有哪些 Jackrabbit 权限(我认为)。然而,我应该指出,这可能不一定真正有效,至少在 J2EE 上下文中是这样。 我在 JBoss AS7 下使用此代码,但它没有找到主题。 不过,这可能只是一个错误。
A pattern i have seen is:
Essentially, this finds the subject currently associated with the current thread, and asks for its principals.
One example of the use of this is in Apache Jackrabbit's RepositoryImpl. It's in the
extendAuthentication
method, whose job is to determine what Jackrabbit rights the current thread has when creating a new session (i think).However, i should note that this may not necessarily actually work, at least in J2EE contexts. I'm using this code under JBoss AS7, and it doesn't find a subject. That might just be a bug, though.
我们使用 ThreadLocal 变量来引用当前用户,该用户已在系统入口点(在我们的例子中为 servlet 或 ejb)进行了身份验证。 这允许对当前用户进行“全局”访问。 这不直接与 JAAS 或任何其他安全协议相关,但可以从它们初始化。
编辑:ThreadLocal 的返回是当前用户的主题。
访问其他用户通常是通过某种类型的管理模块来完成的。
We use a ThreadLocal variable to reference the current user as has been authenticated at the system entrypoint (a servlet or ejb in our case). This allows 'global' access to the current user. This is not directly tied to JAAS or any other security protocol, but can be initialized from them.
EDIT: The return from the ThreadLocal is the Subject for the current user.
Accessing other users would typically be done via some type of admin module.
对我来说,这似乎将 appsever 的用户、组等与 J2EE 应用程序角色混合在一起。
To me, it seems this mizes appsever's users, groups etc. with J2EE application roles.
我相信 JAAS 的设计目的并不是真正允许您尝试做的事情。 我知道在我构建的应用程序中,我需要那种功能,我必须回避 JAAS 并直接编程到任何实际的身份存储库,无论是 LDAP、ActiveDirectory 还是其他。
I believe that JAAS was designed to not really allow what you are trying to do. I know in the apps I've built that I needed that sort of functionality I had to side-step JAAS and program directly to whatever the actual identity repository was, be it LDAP, ActiveDirectory or whatever.
在 EJB 中使用
并在任何时候尝试使用
context.lookup("java:comp/EJBContext"
) 。此代码适用于 JBoss 服务器系列,供其他人在其 JNDI 中查找。
In a EJB use
And try with
context.lookup("java:comp/EJBContext"
) at any point.This code is for JBoss server family, for others look in their JNDI to find it.