JAX-RS和自定义授权
我正在尝试保护 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这完全取决于您使用的 JAX-RS 实现。我在嵌入式 Jetty。
请参阅如何使用嵌入式 Jetty 配置安全性
一旦您在
HttpServletRequest
中获得了Principal
,您就可以将它们注入到 JAX-RS 请求的上下文中。It all depends upon the JAX-RS implementation you're using. I'm using Jersey on embedded Jetty.
See How to Configure Security with Embedded Jetty
Once you have the
Principal
in theHttpServletRequest
, you can inject these into the context of the JAX-RS request.免责声明:除非您真的、真的、真的需要一个安全框架,否则不要使用自己的安全框架。
看看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.