OAuth 2.0 資源伺服器多租戶
多租戶
當有多種策略來驗證 Bearer 令牌時,資源伺服器會被視為多租戶,這些策略以某種租戶識別符號作為索引。
例如,您的資源伺服器可以接受來自兩個不同授權伺服器的 Bearer 令牌。或者,您的授權伺服器可以代表多個發行者。
在每種情況下,都需要完成兩件事,並且權衡與您選擇如何執行它們的方式相關聯
-
解析租戶。
-
傳播租戶。
依宣告解析租戶
區分租戶的一種方法是透過發行者宣告。由於發行者宣告伴隨簽署的 JWT,您可以使用 JwtIssuerReactiveAuthenticationManagerResolver
來做到這一點
-
Java
-
Kotlin
JwtIssuerReactiveAuthenticationManagerResolver authenticationManagerResolver = JwtIssuerReactiveAuthenticationManagerResolver
.fromTrustedIssuers("https://idp.example.org/issuerOne", "https://idp.example.org/issuerTwo");
http
.authorizeExchange(exchanges -> exchanges
.anyExchange().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.authenticationManagerResolver(authenticationManagerResolver)
);
val customAuthenticationManagerResolver = JwtIssuerReactiveAuthenticationManagerResolver
.fromTrustedIssuers("https://idp.example.org/issuerOne", "https://idp.example.org/issuerTwo")
return http {
authorizeExchange {
authorize(anyExchange, authenticated)
}
oauth2ResourceServer {
authenticationManagerResolver = customAuthenticationManagerResolver
}
}
這很棒,因為發行者端點是延遲載入的。事實上,對應的 JwtReactiveAuthenticationManager
只有在發送具有對應發行者的第一個請求時才會實例化。這允許應用程式啟動獨立於那些授權伺服器的啟動和可用性。
動態租戶
您可能不希望每次新增新租戶時都重新啟動應用程式。在這種情況下,您可以將 JwtIssuerReactiveAuthenticationManagerResolver
配置為 ReactiveAuthenticationManager
實例的儲存庫,您可以在運行時編輯它
-
Java
-
Kotlin
private Mono<ReactiveAuthenticationManager> addManager(
Map<String, ReactiveAuthenticationManager> authenticationManagers, String issuer) {
return Mono.fromCallable(() -> ReactiveJwtDecoders.fromIssuerLocation(issuer))
.subscribeOn(Schedulers.boundedElastic())
.map(JwtReactiveAuthenticationManager::new)
.doOnNext(authenticationManager -> authenticationManagers.put(issuer, authenticationManager));
}
// ...
JwtIssuerReactiveAuthenticationManagerResolver authenticationManagerResolver =
new JwtIssuerReactiveAuthenticationManagerResolver(authenticationManagers::get);
http
.authorizeExchange(exchanges -> exchanges
.anyExchange().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.authenticationManagerResolver(authenticationManagerResolver)
);
private fun addManager(
authenticationManagers: MutableMap<String, ReactiveAuthenticationManager>, issuer: String): Mono<JwtReactiveAuthenticationManager> {
return Mono.fromCallable { ReactiveJwtDecoders.fromIssuerLocation(issuer) }
.subscribeOn(Schedulers.boundedElastic())
.map { jwtDecoder: ReactiveJwtDecoder -> JwtReactiveAuthenticationManager(jwtDecoder) }
.doOnNext { authenticationManager: JwtReactiveAuthenticationManager -> authenticationManagers[issuer] = authenticationManager }
}
// ...
var customAuthenticationManagerResolver = JwtIssuerReactiveAuthenticationManagerResolver(authenticationManagers::get)
return http {
authorizeExchange {
authorize(anyExchange, authenticated)
}
oauth2ResourceServer {
authenticationManagerResolver = customAuthenticationManagerResolver
}
}
在這種情況下,您可以使用一種策略來建構 JwtIssuerReactiveAuthenticationManagerResolver
,該策略用於取得給定發行者的 ReactiveAuthenticationManager
。這種方法讓我們可以在運行時從儲存庫(在前述程式碼片段中顯示為 Map
)新增和移除元素。
簡單地接受任何發行者並從中建構 |