JAX-RS和自定义授权

发布于 2024-10-19 04:26:29 字数 156 浏览 7 评论 0原文

我正在尝试保护 JAX-RS 端点,目前正在尝试弄清楚身份验证和授权是如何工作的。大多数示例都非常简单,因为它们仅通过 web.xml 搭载 Java EE 应用服务器角色。

我想知道如何使用 Java EE AS 角色之外的其他角色。例如:我想使用会话或某种令牌(或某种标识符)。

I'm trying to secure the JAX-RS endpoint and am currently trying to figure out how the authentication and authorization work. Most examples are quite simple as they only piggyback from Java EE App-Server role via web.xml.

I'm wondering how to use something else than the Java EE AS roles. For example: I'd like to use session or some sort of token (or some sort of identifier).

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

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

发布评论

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

评论(2

揪着可爱 2024-10-26 04:26:29

这完全取决于您使用的 JAX-RS 实现。我在嵌入式 Jetty

SecurityHandler sh = new SecurityHandler();

// the UserRealm is the collection of users, and a mechanism to determine if
// provided credentials are valid
sh.setUserRealm(new MyUserRealm());

// the Authenticator is a strategy for extracting authentication credentials
// from the request. BasicAuthenticator uses HTTP Basic Auth
sh.setAuthenticator(new BasicAuthenticator());

请参阅如何使用嵌入式 Jetty 配置安全性

一旦您在 HttpServletRequest 中获得了 Principal,您就可以将它们注入到 JAX-RS 请求的上下文中。

public abstract class AbstractResource {
    private Principal principal;
    @Context
    public void setSecurityContext(SecurityContext context) {
        principal = context.getUserPrincipal();
    }
    protected Principal getPrincipal() {
        return principal;
    }
}

@Path("/some/path")
public class MyResource extends AbstractResource {
    @GET
    public Object get() {
        Principal user = this.getPrincipal();
        // etc
    }
}

It all depends upon the JAX-RS implementation you're using. I'm using Jersey on embedded Jetty.

SecurityHandler sh = new SecurityHandler();

// the UserRealm is the collection of users, and a mechanism to determine if
// provided credentials are valid
sh.setUserRealm(new MyUserRealm());

// the Authenticator is a strategy for extracting authentication credentials
// from the request. BasicAuthenticator uses HTTP Basic Auth
sh.setAuthenticator(new BasicAuthenticator());

See How to Configure Security with Embedded Jetty

Once you have the Principal in the HttpServletRequest, you can inject these into the context of the JAX-RS request.

public abstract class AbstractResource {
    private Principal principal;
    @Context
    public void setSecurityContext(SecurityContext context) {
        principal = context.getUserPrincipal();
    }
    protected Principal getPrincipal() {
        return principal;
    }
}

@Path("/some/path")
public class MyResource extends AbstractResource {
    @GET
    public Object get() {
        Principal user = this.getPrincipal();
        // etc
    }
}
甜尕妞 2024-10-26 04:26:29

免责声明:除非您真的、真的、真的需要一个安全框架,否则不要使用自己的安全框架。

看看OAuth 过滤器 确实如此。它读取授权标头,该标头以与通常理解的格式(HTTP Basic)不同的格式保存凭据。如果您添加 角色允许过滤器 执行实际执行。尝试看看这些过滤器是如何工作的。

Disclaimer: Don't role your own security framework unless you really, really, really, need one.

Look at what the OAuth filter in Jersey does. It reads the Authorization header which holds credentials in a different format than those normally understood (HTTP Basic). It'll turn those credentials into roles which you can then use to implement security (@RolesAllowed) if you add in the Roles Allowed Filter which does the actually enforcement. Try looking at how those filters work.

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