2.15. 保护 Jakarta RESTful Web 服务 Web 服务


RESTEasy 支持 Jakarta RESTful Web 服务方法上的 @RolesAllowed、@PermitAll 和 @DenyAll 注释。但是,您必须启用基于角色的安全性,才能识别这些注释。

2.15.1. 启用基于角色的安全性

按照以下步骤配置 web.xml 文件,以启用基于角色的安全性。

警告

如果应用使用 Jakarta Enterprise Beans,则不要激活基于角色的安全性。Jakarta Enterprise Beans 容器将提供功能,而非 RESTEasy。

为 RESTEasy Jakarta RESTful Web 服务启用基于角色的安全性

  1. 在文本编辑器中打开应用的 web.xml 文件。
  2. < web-app> 标签内将以下 <context-param > 添加到该文件中。

    <context-param>
      <param-name>resteasy.role.based.security</param-name>
      <param-value>true</param-value>
    </context-param>
    Copy to Clipboard Toggle word wrap
  3. 使用 <security-role> 标签声明 RESTEasy Jakarta RESTful Web Services WAR 文件中使用的所有角色。

    <security-role>
      <role-name>ROLE_NAME</role-name>
    </security-role>
    <security-role>
      <role-name>ROLE_NAME</role-name>
    </security-role>
    Copy to Clipboard Toggle word wrap
  4. 授权对所有角色的 Jakarta RESTful Web Services 运行时处理的所有 URL 进行访问。

    <security-constraint>
      <web-resource-collection>
        <web-resource-name>Resteasy</web-resource-name>
        <url-pattern>/PATH</url-pattern>
      </web-resource-collection>
      <auth-constraint>
        <role-name>ROLE_NAME</role-name>
        <role-name>ROLE_NAME</role-name>
      </auth-constraint>
    </security-constraint>
    Copy to Clipboard Toggle word wrap
  5. 为此应用定义适当的登录配置。

    <login-config>
      <auth-method>BASIC</auth-method>
      <realm-name>jaxrs</realm-name>
    </login-config>
    Copy to Clipboard Toggle word wrap

基于角色的安全性已在应用内启用,包含一组定义的角色。

示例:基于角色的安全配置

<web-app>

  <context-param>
    <param-name>resteasy.role.based.security</param-name>
    <param-value>true</param-value>
  </context-param>

  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Resteasy</web-resource-name>
      <url-pattern>/security</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>admin</role-name>
      <role-name>user</role-name>
    </auth-constraint>
  </security-constraint>

  <login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>jaxrs</realm-name>
  </login-config>

  <security-role>
    <role-name>admin</role-name>
  </security-role>
  <security-role>
    <role-name>user</role-name>
  </security-role>

</web-app>
Copy to Clipboard Toggle word wrap

2.15.2. 使用注释保护 Jakarta RESTful Web 服务

若要使用注释保护 Jakarta RESTful Web 服务,请完成以下步骤:

  1. 启用基于角色的安全性.
  2. 向 Jakarta RESTful Web Services Web 服务添加安全注释。RESTEasy 支持以下注解:

    @RolesAllowed
    定义哪些角色可以访问该方法。所有角色都应在 web.xml 文件中定义。
    @PermitAll
    允许 web.xml 文件中定义的所有角色访问该方法。
    @DenyAll
    拒绝对该方法的所有访问。

以下是使用 @RolesAllowed 注释来指定 admin 角色可以访问 Web 服务的示例:

@RolesAllowed("admin")
@Path("/test")
public class TestService {
  ...
}
Copy to Clipboard Toggle word wrap

2.15.3. 设置编程安全性

Jakarta RESTful Web 服务包含一个用于收集有关安全请求的安全信息的编程 API。javax.ws.rs.core.SecurityContext 界面有一个方法来确定进行安全 HTTP 调用的用户的身份。它还允许您检查当前用户是否属于某个角色:

public interface SecurityContext {

   public Principal getUserPrincipal();
   public boolean isUserInRole(String role);
   public boolean isSecure();
   public String getAuthenticationScheme();
}
Copy to Clipboard Toggle word wrap

您可以通过利用 @ Context 注释将它注入字段、setter 方法或资源方法参数来访问 SecurityContext 实例。

@Path("test")
public class SecurityContextResource {
    @Context
    SecurityContext securityContext;

    @GET
    @Produces("text/plain")
    public String get() {
        if (!securityContext.isUserInRole("admin")) {
            throw new WebApplicationException(Response.serverError().status(HttpResponseCodes.SC_UNAUTHORIZED)
                    .entity("User " + securityContext.getUserPrincipal().getName() + " is not authorized").build());
        }
        return "Good user " + securityContext.getUserPrincipal().getName();
    }
}
Copy to Clipboard Toggle word wrap
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部