提问者:小点点

如何在@WebFLuxTest中禁用oAuth2客户端注册?


已配置oauth2:

security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: ${keycloak.server-url}/realms/${keycloak.realm}
      client:
        provider:
          keycloak:
            issuer-uri: ${keycloak.server-url}/realms/${keycloak.realm}
            user-name-attribute: email
        registration:
          keycloak:
            client-id: ${keycloak.client-id}
            client-secret: ${keycloak.client-secret}
            scope: openid

我想知道如何为一个控制器运行一个< code>@WebFluxTest而不必运行一个实际的keycloak服务器。当我运行本地keycloak服务器时,下面的测试运行良好:

@WebFluxTest(TaskController::class)
class TaskControllerTest {

  @Autowired
  private lateinit var client: WebTestClient

  @MockkBean
  private lateinit var taskService: TaskServiceImpl

  @Test
  @WithMockUser(roles = ["user"])
  fun getByIds() {
    val ids = setOf<Long>(1, 2)
    every { taskService.getByIds(ids, any()) } returns flowOf(
      TaskWithRating(
        Task(
          id = 1,
          title = "title",
          description = "desc"
        ),
        Rating(0, 0),
      )
    )
    client
      .get()
      .uri { uriBuilder ->
        uriBuilder
          .path("/tasks/byIds")
          .queryParam("ids", ids)
          .build()
      }
      .exchange()
      .expectStatus().isOk
      .expectBody()
      .jsonPath("$.length()").isEqualTo(1)
      .jsonPath("$.1.task.id").isEqualTo(1)
  }
}

当我停止本地 keycloak 服务器时,测试失败,因为 spring 在创建 ApplicationContext 时尝试在启动期间读取 oauth2 配置:

Failed to load ApplicationContext
...
Factory method 'clientRegistrationRepository' threw exception; nested exception is java.lang.IllegalArgumentException: Unable to resolve Configuration with the provided Issuer of "https://keycloak.local.com/auth/realms/myrealm"
...
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://keycloak.local.com/auth/realms/myrealm/.well-known/openid-configuration": Connection refused; nested exception is java.net.ConnectException: Connection refused
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:784)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:669)
    at org.springframework.security.oauth2.client.registration.ClientRegistrations.lambda$oidc$0(ClientRegistrations.java:156)
    at org.springframework.security.oauth2.client.registration.ClientRegistrations.getBuilder(ClientRegistrations.java:209)
    ... 175 more
Caused by: java.net.ConnectException: Connection refused

测试不需要它,因为用户无论如何都是被嘲笑的。所以我想在< code>@WebFluxTest中禁用auth2配置,但是我不知道这是一种有效的方法吗?

我试过这个。

@WebFluxTest(
  controllers = [TaskController::class],
  excludeAutoConfiguration = [ReactiveSecurityAutoConfiguration::class],
)

还有这个

@WebFluxTest(
  controllers = [TaskController::class],
  excludeAutoConfiguration = [ReactiveOAuth2ClientAutoConfiguration::class],
)

但错误是一样的。


共1个答案

匿名用户

事实证明我走在正确的道路上。必须禁用oAuth2客户端和资源服务器自动配置。这很有效:

@WebFluxTest(
  controllers = [TaskController::class],
  excludeAutoConfiguration = [
    ReactiveOAuth2ClientAutoConfiguration::class,
    ReactiveOAuth2ResourceServerAutoConfiguration::class,
  ],
)

请注意,这绕过了潜在的禁用特性,因此,例如,需要在测试客户端上进行< code>csrf()变异:

client
  .mutateWith(SecurityMockServerConfigurers.csrf()) 

另一种选择是使用@导入(TestSecurityConfiguration::class)为安全设置配置测试配置。