如何在同一应用程序中实现多个 Silhouette JWT 身份验证器?
How to implement multiple Silhouette JWT Authenticators in same application?
有在同一个 Play Framework 应用程序中使用多种类型的身份验证器的示例但是我想要的是,使用 2 个 JWT 身份验证器,它们具有不同的 headerNames,发布者声称和 crypter 在同一个应用程序中使用单独的 Silhouette 环境。
更新:
我为 Silhouette 创建了 2 个环境,但两个签名相同,只是名称不同,如下所示:
trait DefaultEnv extends Env {
type I = User
type A = JWTAuthenticator
}
trait CustomEnv extends Env {
type I = User
type A = JWTAuthenticator
}
MyModule extends AbstractModule with ScalaModule {
...
@Provides
def provideAuthenticatorService(crypter: Crypter,
idGenerator: IDGenerator,
configuration: Configuration,
clock: Clock): AuthenticatorService[JWTAuthenticator] = {
val encoder = new CrypterAuthenticatorEncoder(crypter)
new JWTAuthenticatorService(JWTAuthenticatorSettings(
fieldName = configuration.underlying.getString("silhouette.authenticator.headerName"),
issuerClaim = configuration.underlying.getString("silhouette.authenticator.issuerClaim"),
authenticatorExpiry = FiniteDuration(configuration.underlying.getLong("silhouette.authenticator.authenticatorExpiry"), "seconds"),
sharedSecret = configuration.underlying.getString("application.secret")
), None, encoder, idGenerator, clock)
}
}
这实际上提供了相同的AuthenticatorService
,如何为不同的命名环境提供不同的AuthenticatorService
,而它们实际上都是AuthenticatorService[JWTAuthenticator]
?
终于设法在单个播放剪影应用程序中允许 2 个 JWTAuthenticators:
AuthenticatorService[JWTAuthenticator]
和另一个
CustomAuthenticatorService[CustomJWTAuthenticator]
与 Environment
和 CustomEnvironment
一起存在
与 Silhouette[DefaultEnv]
和 CustomSilhouette[CustomEnv]
其中
trait DefaultEnv extends Env {
type I = User
type A = JWTAuthenticator
}
trait CustomEnv extends Env {
type I = User
type A = CustomJWTAuthenticator
}
要求是允许 2 组不同的 api 到同一后端服务的 2 个不同客户端,其中一组 api 的 jwt 令牌不能用于验证另一组 api,即使在同一控制器内也是如此。此解决方案以这种方式实现,以防止在 2 个不同的客户端使用相同的数据库和事件总线时破坏模型或代码库。
有在同一个 Play Framework 应用程序中使用多种类型的身份验证器的示例但是我想要的是,使用 2 个 JWT 身份验证器,它们具有不同的 headerNames,发布者声称和 crypter 在同一个应用程序中使用单独的 Silhouette 环境。
更新: 我为 Silhouette 创建了 2 个环境,但两个签名相同,只是名称不同,如下所示:
trait DefaultEnv extends Env {
type I = User
type A = JWTAuthenticator
}
trait CustomEnv extends Env {
type I = User
type A = JWTAuthenticator
}
MyModule extends AbstractModule with ScalaModule {
...
@Provides
def provideAuthenticatorService(crypter: Crypter,
idGenerator: IDGenerator,
configuration: Configuration,
clock: Clock): AuthenticatorService[JWTAuthenticator] = {
val encoder = new CrypterAuthenticatorEncoder(crypter)
new JWTAuthenticatorService(JWTAuthenticatorSettings(
fieldName = configuration.underlying.getString("silhouette.authenticator.headerName"),
issuerClaim = configuration.underlying.getString("silhouette.authenticator.issuerClaim"),
authenticatorExpiry = FiniteDuration(configuration.underlying.getLong("silhouette.authenticator.authenticatorExpiry"), "seconds"),
sharedSecret = configuration.underlying.getString("application.secret")
), None, encoder, idGenerator, clock)
}
}
这实际上提供了相同的AuthenticatorService
,如何为不同的命名环境提供不同的AuthenticatorService
,而它们实际上都是AuthenticatorService[JWTAuthenticator]
?
终于设法在单个播放剪影应用程序中允许 2 个 JWTAuthenticators:
AuthenticatorService[JWTAuthenticator]
和另一个
CustomAuthenticatorService[CustomJWTAuthenticator]
与 Environment
和 CustomEnvironment
一起存在
与 Silhouette[DefaultEnv]
和 CustomSilhouette[CustomEnv]
其中
trait DefaultEnv extends Env {
type I = User
type A = JWTAuthenticator
}
trait CustomEnv extends Env {
type I = User
type A = CustomJWTAuthenticator
}
要求是允许 2 组不同的 api 到同一后端服务的 2 个不同客户端,其中一组 api 的 jwt 令牌不能用于验证另一组 api,即使在同一控制器内也是如此。此解决方案以这种方式实现,以防止在 2 个不同的客户端使用相同的数据库和事件总线时破坏模型或代码库。