如何在代码中的任意点访问 JAAS 角色?

发布于 2024-07-13 13:26:02 字数 468 浏览 10 评论 0原文

我想访问用户的完整模型及其在 SOAP 应用程序中的角色。 例如,我可能想知道名为“Fred”的用户的角色。

如何访问某种全局 JAAS 注册表并执行(伪代码)globalRegistry.getUser("Fred").getPrincipals()? (请注意,在 JAAS 中,角色由 Principal 表示。)

我知道如何从 Subject 中获取 Principal >LoginContext,但这有两个问题。

  1. 仅在登录时,我不想自己编写上述注册表并存储 SubjectPrincipal 对象,因为它们已经由应用程序服务器。
  2. 最好,即使 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.

  1. 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.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只想待在家 2024-07-20 13:26:02

我看到的一种模式是:

AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
Set<Principal> principals = subject.getPrincipals();

本质上,这会找到当前与当前线程关联的主题,并询问其主体。

使用此功能的一个示例是 Apache JackrabbitRepositoryImpl。 它位于 extendAuthentication 方法中,该方法的作用是确定当前线程在创建新会话时拥有哪些 Jackrabbit 权限(我认为)。

然而,我应该指出,这可能不一定真正有效,至少在 J2EE 上下文中是这样。 我在 JBoss AS7 下使用此代码,但它没有找到主题。 不过,这可能只是一个错误。

A pattern i have seen is:

AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
Set<Principal> principals = subject.getPrincipals();

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.

り繁华旳梦境 2024-07-20 13:26:02

我们使用 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.

羞稚 2024-07-20 13:26:02

对我来说,这似乎将 appsever 的用户、组等与 J2EE 应用程序角色混合在一起。

  • 获取特定用户的权限是一项管理任务,通常必须使用特定于应用程序服务器的 API 来完成。
  • JAAS 编程模型适用于更高层次的抽象。 它仅提供用户是否处于 J2EE 角色(在应用程序内定义)的信息

To me, it seems this mizes appsever's users, groups etc. with J2EE application roles.

  • Getting permissions of a certaion user is a administration task and usually has to be accomplished using appserver-specific APIs.
  • JAAS programming model works on higher level of abstratcion. It only provides the information whether a user is in a J2EE role (defined within the application)
爱要勇敢去追 2024-07-20 13:26:02

我相信 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.

凹づ凸ル 2024-07-20 13:26:02

在 EJB 中使用

@Resource(mappedName = "java:comp/EJBContext")
protected SessionContext sessionContext;

并在任何时候尝试使用 context.lookup("java:comp/EJBContext") 。


此代码适用于 JBoss 服务器系列,供其他人在其 JNDI 中查找。

In a EJB use

@Resource(mappedName = "java:comp/EJBContext")
protected SessionContext sessionContext;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文