jax-rs - 没有 web.xml 的 JAX-RS 的 HTTP 基本身份验证
问题描述
我正在 JBoss EAP 7.1 上运行的 EAR 中的 EJB-JAR 中实现 REST 服务。
该服务的不安全版本运行良好,但即使添加基本的 HTTP 身份验证也是一个挑战,因为在 EJB-JAR 中我发现无法指定任何所需的 web.xml 条目,例如<auth-method>BASIC</auth-method>
所以我的问题是:
如何配置 JAX-RS 以在 EJB-JAR 中使用 HTTP 身份验证?
附加信息:
为了让事情更简单,我像这样使用默认的 ManagementRealm
<security-domain name="my-security-domain" cache-type="default"> <authentication> <login-module code="Remoting" flag="optional"> <module-option name="password-stacking" value="useFirstPass"/> </login-module> <login-module code="RealmDirect" flag="required"> <module-option name="realm" value="ManagementRealm"/> </login-module> </authentication> </security-domain>
在 EJB-jar 中:
@Stateless @Path("/my-rest") @SecurityDomain(value = "my-security-domain") @DenyAll public class MyRestStatelessBean { @PUT @RolesAllowed("admin") @Path("/doAdminStuff") public void doAdminStuff() { // Implementation } }
解决方案
解决方案是使用 Undertow 的主动身份验证功能,默认情况下实际上是打开的。在请求中指定 HTTP-BASIC-Authentication 标头,使 Undertow 即使通过我的 REST 服务也尝试登录用户,因为缺少web.xml
不需要任何类型的身份验证。
我的完整配置(使用来自的管理JBoss 用户mgmt-users.properties
):
# Define my security domain
/subsystem=security/security-domain=MY-SECURITY-DOMAIN:add(cache-type=default)
# Link Untertow to Elytron for authentication
/subsystem=undertow/application-security-domain=MY-SECURITY-DOMAIN:add( \
http-authentication-factory="management-http-authentication" \
)
# Add BASIC-HTTP-Authentication support to Elytron
/subsystem=elytron/http-authentication-factory=management-http-authentication:list-add( \
name=mechanism-configurations, \
value={mechanism-name="BASIC", \
mechanism-realm-configurations=[{realm-name="ManagementRealm"}] \
} \
)
# Not sure, why is this required...
/subsystem=ejb3/application-security-domain=MY-SECURITY-DOMAIN:add( \
security-domain="ManagementDomain")
推荐阅读
- c# - 如何将参数添加到 Telerik 报告源?
- php - Usort 被跳过,除非被观看
- tkinter - tkinter grid geom - 使用百分比而不是行数和列数
- javascript - 在 react-native 中根据平台显示/隐藏导航标题
- drupal - 从 Drupal 博客获取帖子的 API?
- laravel - 我已经下载了 laravel 项目,但它在根目录中没有工匠文件
- azure-active-directory - 身份验证期间无法验证重定向 uri
- azure - 从 Azure 逻辑应用中的 Onedrive 搜索文件夹中的文件
- javascript - 使用窗口消息的 Windows 服务和 Electron 本机 Windows 应用程序通信
- c# - 客户端-服务器通信和加密/解密