我在try 创建自定义OpenIdAuthenticationMapper时遇到此错误.此代码将向OAuth服务器发出请求,以获取识别码和令牌.我不知道怎么把AuthenticationResponse.success路换成Publisher<AuthenticationResponse>路.

作者:4.2.1

ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Failed to inject value for parameter [authenticationMapper] of class: io.micronaut.security.oauth2.endpoint.authorization.response.DefaultOpenIdAuthorizationResponseHandler

Message: Multiple possible bean candidates found: [DefaultOpenIdAuthenticationMapper, MyOpenIdAuthenticationMapper]

@Singleton
@Named("my")
class MyOpenIdAuthenticationMapper(
    ...
) : OpenIdAuthenticationMapper {
    @NonNull
    override fun createAuthenticationResponse(
        providerName: String,
        tokenResponse: OpenIdTokenResponse,
        openIdClaims: OpenIdClaims,
        @Nullable state: State?
    ): Publisher<AuthenticationResponse> {
       # Make request to Oauth server
        ...

        val attributes = mapOf(
            ...
        )

        return Mono.create { AuthenticationResponse.success(identificationCode, emptyList(), attributes) }
    }
}

推荐答案

Micronaut在您的应用程序上下文中发现了两个类型为OpenIdAuthenticationMapper的Bean候选者,因此它失败了,因为它不知道如何解决该冲突.

要解决此冲突,您必须使用以下选项帮助Micronaut:

Mark your bean as the primary

使用@Primary告诉Micronaut Select 您的Bean,以解决多个可能的Bean候选冲突.

@Singleton
@Primary
class MyOpenIdAuthenticationMapper : OpenIdAuthenticationMapper {}

Replace the DefaultOpenIdAuthenticationMapper

您可以通过添加@Replace来用您的实现替换DefaultOpenIdAuthenticationMapper

@Singleton
@Replaces(DefaultOpenIdAuthenticationMapper)
class MyOpenIdAuthenticationMapper : OpenIdAuthenticationMapper {}

给你的班级.这将确保应用程序上下文中只有一个OpenIdAuthenticationMapper Bean.

进一步阅读

Kotlin相关问答推荐

Kotlin—从列表中枚举属性计算不同值的数量

为什么Kotlin函数参数名会 destruct 方法调用?

Kotlin Coroutine()是如何工作的?S阻止了.

如何使用成员引用在 Kotlin 中创建属性的分层路径

Kotlin SAM/功能接口抛出 AbstractMethodError

从 HashMap 检索时的 NPE,即使 containsKey() 在多线程环境中返回 true

从带有 Room 和 Flows 的 SQLite 表中获取祖先树

如何通过 compose 处理剪切区域?

我什么时候可以在 LazyList 或 Column 的范围内使用 Composable?

如何在 Kotlin 中使用具有继承的泛型

Android 导航组件 - 向上导航打开相同的片段

@InlineOnly 注释是什么意思?

对列表中数字的子集求和

Kotlin 和 Java 字符串拆分与正则表达式的区别

使用 Hilt 注入 CoroutineWorker

将 Kotlin 类属性设置器作为函数引用

Android插件2.2.0-alpha1无法使用Kotlin编译

Android 上的 Kotlin:将map到list

在 Kotlin 中创建 Spinner 时,如何在 Fragment 中的旋转屏幕上修复指定为非空的参数为空?

Kotlin类型安全类型别名