[英]How to get the role from the current user?
我正在嘗試使用 JWT實現AuthN/AuthZ,如下所示:
class MainVerticle : CoroutineVerticle() {
private suspend fun initConfig(): JsonObject {
val yamlConfigOpts = ConfigStoreOptions()
.setFormat("yaml")
.setType("file")
.setConfig(JsonObject().put("path", "config.yaml"))
val configRetrieverOpts = ConfigRetrieverOptions()
.addStore(yamlConfigOpts)
val configRetriever = ConfigRetriever.create(vertx, configRetrieverOpts)
return configRetriever.config.await()
}
private suspend fun createJwtAuth(client: WebClient, config: JsonObject): JWTAuth? {
val issuer = config.getJsonObject("jwt").getString("issuer")
// derive JWKS uri from Keycloak issuer URI
val jwksUri = URI.create("${issuer}/protocol/openid-connect/certs")
// The exception will be caught above
val res = client.get(jwksUri.host, jwksUri.path).send().await()
return res.bodyAsJsonObject()?.let {
val keys = it.getJsonArray("keys")
val jwtOpt = JWTOptions()
jwtOpt.issuer = issuer
// configure JWTAuth
val jwtAuthOptions = JWTAuthOptions()
jwtAuthOptions.jwks = (keys.list as List<Map<String, *>>)
.map { json -> JsonObject(json) }
.map { json -> json.put("permissionsClaimKey", "realm_access/roles") }
jwtAuthOptions.jwtOptions = jwtOpt
JWTAuth.create(vertx, jwtAuthOptions)
} ?: throw AuthenticationException("Can not receive the token")
}
private fun createRoutes(router: Router, jwtAuth: JWTAuth): Unit {
router.route("/api/*").handler(JWTAuthHandler.create(jwtAuth))
router.route("/api/greet").handler {
val token = it.request().getHeader(HttpHeaders.AUTHORIZATION).substring("Bearer ".length)
jwtAuth.authenticate(JsonObject().put("jwt", token))
.onSuccess { user ->
val res = it.response()
res.putHeader("content-type", "text/plain")
// Write to the response and end it
res.end("I am interests path")
}
.onFailure { err -> it.response().setStatusCode(403).end(err.message) }
}
}
private suspend fun server(router: Router): HttpServer {
val server = vertx.createHttpServer()
return server.requestHandler(router)
.listen(8080)
.onSuccess {
println("HTTP server started on port ${it.actualPort()}")
}
.onFailure {
println("Failed to start the server. Reason ${it.message}")
}
.await()
}
override suspend fun start() {
val config = initConfig()
val webClient = WebClient.create(vertx)
val router = Router.router(vertx)
createJwtAuth(webClient, config)?.let {
createRoutes(router, it)
server(router)
}
}
}
在/api/greet
路由處理程序中,我想讀出用戶的聲明,例如角色或名稱。 但不幸的是,這就是我所擁有的:
問題是,如何讀取用戶的聲明?
我使用 Keycloak 作為身份提供者和 Vertx 版本4.0.0.CR1
。
更新
我改成了OAuth2 auth provider ,對代碼做了如下調整:
class MainVerticle : CoroutineVerticle() {
private suspend fun createJwtAuth(): OAuth2Auth =
KeycloakAuth.discover(
vertx,
OAuth2Options()
.setFlow(OAuth2FlowType.AUTH_CODE)
.setClientID("svc")
.setClientSecret("secret")
.setSite("https://oic.example.io/auth/realms/vertx")
).await()
private fun createRoutes(router: Router, auth: OAuth2Auth): Unit {
val oauth2 = OAuth2AuthHandler.create(vertx, auth)
router.route("/api/*").handler(oauth2)
router.route("/api/greet").handler {
println(it.user().principal().getString("preferred_username"))
val res = it.response()
res.putHeader("content-type", "text/plain")
// Write to the response and end it
res.end("I am interests path")
}
}
private suspend fun server(router: Router): HttpServer {
val server = vertx.createHttpServer()
return server.requestHandler(router)
.listen(8080)
.onSuccess {
println("HTTP server started on port ${it.actualPort()}")
}
.onFailure {
println("Failed to start the server. Reason ${it.message}")
}
.await()
}
override suspend fun start() {
val router = Router.router(vertx)
createRoutes(router, createJwtAuth())
server(router)
}
}
線
println(it.user().principal().getString("preferred_username"))
打印null
而不是用戶名。 我究竟做錯了什么?
當您使用當前的最新版本時,讓我解釋一下新 API 中的更改。 在 4.0.0 中,我們將authn
和authz
分開,因此在您的示例中,您已經正確執行了authn
並獲得了一個User
對象實例。
現在您想提取權限,因為您在執行JWTAuth
您正在采用“低”級別路徑,而如果您使用OAuth2Auth
,則OAuth2Auth
某些事情(例如加載密鑰等...) .
現在您有一個用戶對象,您需要有一個authz
提取對象。 為此,我將使用 java API 來舉例說明,但在 Kotlin 中它應該非常相似:
// First create a JWTAuthorization object
JWTAuthorization.create("realm_access/roles")
.getAuthorizations(user)
.onSuccess(success -> {
// The authorizations have been successfully extracted from the user
// Now you can perform any kind of checks
if (PermissionBasedAuthorization.create("write").match(user)) {
// ... User is allowed to write...
}
});
因此,權限是從您傳遞給提取器的claimKey
下的attributes
中提取的。 簡而言之, attributes
是由框架生成的和/或解碼/驗證的數據,而principal
是為創建用戶而提供的源數據。 區別很重要,因為現在用戶對象可以用於服務器驗證和客戶端請求。
這也意味着在保持源不變的情況下,不存在解碼會覆蓋重要數據(例如到期日期等)的風險。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.