Java EE/JBoss AS 6 中的预身份验证用户

发布于 2024-12-09 18:38:18 字数 430 浏览 1 评论 0原文

我正在将一些 Java EE 模块从 Spring 迁移到 EJB,现在面临的问题是在调用服务方法之前需要某种预身份验证。

问题其实很简单。调用来自内部协议处理程序(某些线程启动专有协议处理程序并使用自定义 TCP 协议接收请求)。该连接尚未对用户进行身份验证,并且接下来想要调用服务方法。该服务方法需要主体信息(用户名)进行处理。

因此,在 Spring 中,我们只是将 SecurityContext 推送到本地线程,并在调用完成后将其删除。

协议处理程序 ->设置SecContext->致电-​​>删除 SexContext ->结束

Java EE/JBoss 中有类似的东西吗?我知道有“@RunAs”构造,但我不知道它们是否可以以编程方式使用。或者有没有办法使用 JAAS LoginContext 类“登录”?那么我该如何配置 JAAS 呢?

I am migrating some Java EE modules from Spring to EJB and are now facing the problem that I need some sort of pre-authentication prior to calling a service method.

The problem is actually quite easy. A call comes in from an internal protocol handler (some thread started the proprietary protocol handler and received requests using a custom TCP protocol). Not this connection already authenticated the user and wants to call a service method next. This service method requires a principal information (user name) for processing.

So in Spring we simply pushed the SecurityContext to the local thread and removed it when the call was done.

Protocol Handler -> Set SecContext -> Call -> Remove SexContext -> End

Is there anything similar to that in Java EE/JBoss? I know there are "@RunAs" constructs but I don't know if they can be used programmatically. Or is there a way to "log in" using the JAAS LoginContext class? But how do I configure JAAS then?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

断桥再见 2024-12-16 18:38:18

如果这纯粹是在 JAAS 上下文中获取身份的问题,那么您应该能够执行以下操作:

final String username; // get this from somewhere
Princpal principal = new Principal() {
    public String getName() {
        return username;
    }
};
Subject subject = new Subject(true, Collections.singleton(principal), Collections.emptySet(), Collections.emptySet());
Subject.doAs(subject, new PrivilegedAction<Void>() {
    public Void run() {
       // do your method call here
    }
});

请注意,您可以通过将 PrivilegedAction 绑定到 Void 以外的类型来从 PrivilegedAction 返回一个值,并通过以下方式抛出异常:相反,实施 PrivilegedExceptionAction。

显然,如果您对主体是什么有更复杂的了解,您可以使用它(实现 toString、hashCode 和 equals 将是一个好主意)。

If this is purely a matter of getting an identity into the JAAS context, you should be able to do something like this:

final String username; // get this from somewhere
Princpal principal = new Principal() {
    public String getName() {
        return username;
    }
};
Subject subject = new Subject(true, Collections.singleton(principal), Collections.emptySet(), Collections.emptySet());
Subject.doAs(subject, new PrivilegedAction<Void>() {
    public Void run() {
       // do your method call here
    }
});

Note that you can return a value from the PrivilegedAction by binding it to a type other than Void, and throw an exception by implementing PrivilegedExceptionAction instead.

Obviously if you have a more sophisticated idea of what a principal is, you could use that (implementing toString, hashCode, and equals would be a good idea).

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