1.7. 使用 PostgreSQL 的 Dev Services 测试应用程序


在生产模式下运行应用程序之前,请先使用 PostgreSQL 的 Dev Services for PostgreSQL 完成应用程序的集成测试和原生模式。

首先,在您的测试项目中添加以下依赖项:

  • 使用 Maven:

    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <scope>test</scope>
    </dependency>
    Copy to Clipboard Toggle word wrap
  • 使用 Gradle :

    testImplementation("io.rest-assured:rest-assured")
    Copy to Clipboard Toggle word wrap
  • 以 dev 模式运行您的应用程序:
  • 使用 Quarkus CLI:

    quarkus dev
    Copy to Clipboard Toggle word wrap
  • 使用 Maven:

    ./mvnw quarkus:dev
    Copy to Clipboard Toggle word wrap
  • 使用 Gradle :

    ./gradlew --console=plain quarkusDev
    Copy to Clipboard Toggle word wrap
  • 以下属性配置演示了如何使 PostgreSQL 测试仅在生产(prod)模式下运行。在这种情况下,PostgreSQL 的 Dev Services 会启动并配置 PostgreSQL 测试容器。
%prod.quarkus.datasource.db-kind=postgresql
%prod.quarkus.datasource.username=quarkus
%prod.quarkus.datasource.password=quarkus
%prod.quarkus.datasource.jdbc.url=jdbc:postgresql:elytron_security_jpa

quarkus.hibernate-orm.database.generation=drop-and-create
Copy to Clipboard Toggle word wrap
  • 如果您添加了 %prod. 配置集前缀,则数据源属性对 PostgreSQL 的 Dev Services 不可见,且仅在生产模式下运行的应用程序中看到。
  • 要编写集成测试,请使用以下代码示例:
package org.acme.security.jpa;

import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.Is.is;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class JpaSecurityRealmTest {

    @Test
    void shouldAccessPublicWhenAnonymous() {
        get("/api/public")
                .then()
                .statusCode(HttpStatus.SC_OK);

    }

    @Test
    void shouldNotAccessAdminWhenAnonymous() {
        get("/api/admin")
                .then()
                .statusCode(HttpStatus.SC_UNAUTHORIZED);

    }

    @Test
    void shouldAccessAdminWhenAdminAuthenticated() {
        given()
                .auth().preemptive().basic("admin", "admin")
                .when()
                .get("/api/admin")
                .then()
                .statusCode(HttpStatus.SC_OK);

    }

    @Test
    void shouldNotAccessUserWhenAdminAuthenticated() {
        given()
                .auth().preemptive().basic("admin", "admin")
                .when()
                .get("/api/users/me")
                .then()
                .statusCode(HttpStatus.SC_FORBIDDEN);
    }

    @Test
    void shouldAccessUserAndGetIdentityWhenUserAuthenticated() {
        given()
                .auth().preemptive().basic("user", "user")
                .when()
                .get("/api/users/me")
                .then()
                .statusCode(HttpStatus.SC_OK)
                .body(is("user"));
    }
}
Copy to Clipboard Toggle word wrap

正如您在此代码示例中看到的,您不需要从测试代码启动测试容器。

注意

当您以 dev 模式启动应用程序时,PostgreSQL 的 Dev Services 会启动一个 PostgreSQL dev 模式容器,以便您可以开始开发应用程序。在开发应用程序时,您可以使用 Continuous Testing 功能单独添加和运行测试。在提供与 dev 模式容器不冲突的独立 PostgreSQL 测试容器时,用于 PostgreSQL 的 dev Services 支持测试。

1.7.1. 使用 curl 或浏览器来测试应用程序

  • 使用以下示例启动 PostgreSQL 服务器:
docker run --rm=true --name security-getting-started -e POSTGRES_USER=quarkus \
           -e POSTGRES_PASSWORD=quarkus -e POSTGRES_DB=elytron_security_jpa \
           -p 5432:5432 postgres:14.1
Copy to Clipboard Toggle word wrap

1.7.2. 编译并运行应用程序

  • 使用以下方法之一编译并运行 Quarkus 应用程序:

    • JVM 模式

      1. 编译应用程序:

        • 使用 Quarkus CLI:

          quarkus build
          Copy to Clipboard Toggle word wrap
        • 使用 Maven:

          ./mvnw install
          Copy to Clipboard Toggle word wrap
        • 使用 Gradle :

          ./gradlew build
          Copy to Clipboard Toggle word wrap
      2. 运行应用程序:

        java -jar target/quarkus-app/quarkus-run.jar
        Copy to Clipboard Toggle word wrap
    • 原生模式

      1. 编译应用程序:

        • 使用 Quarkus CLI:

          quarkus build --native
          Copy to Clipboard Toggle word wrap
        • 使用 Maven:

          ./mvnw install -Dnative
          Copy to Clipboard Toggle word wrap
        • 使用 Gradle :

          ./gradlew build -Dquarkus.package.type=native
          Copy to Clipboard Toggle word wrap
      2. 运行应用程序:

        ./target/security-jpa-quickstart-1.0.0-SNAPSHOT-runner
        Copy to Clipboard Toggle word wrap

1.7.3. 访问并测试应用程序安全性

当应用运行时,您可以使用以下 Curl 命令之一访问其端点。

  • 匿名连接到受保护的端点:

    $ curl -i -X GET http://localhost:8080/api/public
    
    HTTP/1.1 200 OK
    Content-Length: 6
    Content-Type: text/plain;charset=UTF-8
    
    public
    Copy to Clipboard Toggle word wrap
  • 匿名连接到受保护的端点:

    $ curl -i -X GET http://localhost:8080/api/admin
    
    HTTP/1.1 401 Unauthorized
    Content-Length: 14
    Content-Type: text/html;charset=UTF-8
    WWW-Authenticate: Basic
    
    Not authorized
    Copy to Clipboard Toggle word wrap
  • 以授权用户身份连接到受保护的端点:

    $ curl -i -X GET -u admin:admin http://localhost:8080/api/admin
    
    HTTP/1.1 200 OK
    Content-Length: 5
    Content-Type: text/plain;charset=UTF-8
    
    admin
    Copy to Clipboard Toggle word wrap

您还可以使用浏览器访问相同的端点 URL。

注意

如果您使用浏览器匿名连接到受保护的资源,则会显示基本身份验证表单,提示您输入凭证。

1.7.4. 结果

当您提供授权用户的凭证时,如 admin:admin,Jakarta Persistence 安全扩展会验证和加载该用户的角色。admin 用户有权访问受保护的资源。

如果资源通过 @RolesAllowed ("user") 进行保护,则用户 admin 不会被授权访问该资源,因为它没有分配给"user"角色,如下例所示:

$ curl -i -X GET -u admin:admin http://localhost:8080/api/users/me

HTTP/1.1 403 Forbidden
Content-Length: 34
Content-Type: text/html;charset=UTF-8

Forbidden
Copy to Clipboard Toggle word wrap

最后,名为 user 的用户被授权,安全上下文包含主体详情,例如 username。

$ curl -i -X GET -u user:user http://localhost:8080/api/users/me

HTTP/1.1 200 OK
Content-Length: 4
Content-Type: text/plain;charset=UTF-8

user
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2026 Red Hat