vert.x JDBCAuth 不在 RoutingContext 中存储用户对象
vert.x JDBCAuth doesn't store User object in RoutingContext
我使用 vert.x 设置了 JDBCAuth,并且身份验证有效。我在 JDBCAuth.authenticate (adminLoginHandler) 之后获得了用户对象,但在下一个 HTTP GET 查询 (adminReadHandler) 的 RoutingContext 中它为空。
我查了一下,那里的RoutingContext有session和cookie对象。
我在设置路由之前添加了 BodyHandler、CookieHandler、SessionHandler.create(LocalSessionStore.create(vertx))、UserSessionHandler.create(adminAuth)。
我使用 OpenAPI3RouterFactory 来设置路由。
我想,我做了文档和示例代码中的所有操作。
我错过了什么?
suspend fun createJDBCAuth(vertx : Vertx, name : String) : JDBCAuth
{
val con = getConnection(vertx, name)
return JDBCAuth.create(vertx, con)
.setAuthenticationQuery("SELECT PASSWORD, PASSWORD_SALT FROM \"user\" WHERE USERNAME = ?")
.setHashStrategy(JDBCHashStrategy.createPBKDF2(vertx))
.setNonces(json {
array(45, 385)
})
}
adminAuth = createJDBCAuth(vertx, "adminreader")
OpenAPI3RouterFactory.create(vertx, "openapi.yaml")
{ ar ->
if (ar.succeeded())
{
// Spec loaded with success
var factory = ar.result()
logger.info("Factory succeed")
factory.setBodyHandler(BodyHandler.create().setBodyLimit(1024 * 1024))
.addGlobalHandler(CookieHandler.create())
.addGlobalHandler(SessionHandler.create(LocalSessionStore.create(vertx)))
.addGlobalHandler(UserSessionHandler.create(adminAuth))
.addGlobalHandler(CorsHandler.create("https://localhost:3000")
.allowCredentials(true)
.allowedHeader("Content-Type")
.allowedMethod(HttpMethod.GET)
.allowedMethod(HttpMethod.POST)
.allowedMethod(HttpMethod.OPTIONS)
)
inputMap.forEach { path ->
when {
path.key == "/admin/login" -> factory.addHandlerByOperationId(path.value.operationId, adminLoginHandler)
path.key.startsWith("/admin/") -> factory.addHandlerByOperationId(path.value.operationId, adminReadHandler)
}
}
更新函数。
private val adminLoginHandler = Handler<RoutingContext>
{ rc ->
adminAuth.authenticate(rc.bodyAsJson)
{ res ->
launch {
var retJson = json { obj() }
if (res.succeeded()) {
var user = res.result()
rc.session()?.regenerateId()
rc.setUser(user)
retJson = retJson.put("succeed", true)
if (user.isAuthorizedAwait("createadmin"))
retJson = retJson.put("createadmin", true)
} else {
retJson = retJson
.put("succeed", false)
.put("message", res.cause().message)
}
rc.response()
.putHeader("content-type", "application/json")
.end(retJson.encode())
}
}
}
private val adminReadHandler = Handler<RoutingContext>
{ rc ->
if (rc.user() == null)
rc.response().setStatusCode(403).end()
else
{
val user = rc.user()
val principal = user.principal()
}
}
事实证明,问题不在后端 (vertx) 端。
我在前端代码中使用浏览器获取 api,但我忘记包含凭据:"include" 选项。
Francesco:谢谢你的努力。你的要点帮助我一点一点地追溯问题。
P.s.: 虽然我的 authn/authz 现在工作了,但我仍然没有在 chrome 调试器的 network/cookies 面板上看到 cookies,使用 fetch api。如果我直接将 url 写入浏览器,我会看到所有内容。
我使用 vert.x 设置了 JDBCAuth,并且身份验证有效。我在 JDBCAuth.authenticate (adminLoginHandler) 之后获得了用户对象,但在下一个 HTTP GET 查询 (adminReadHandler) 的 RoutingContext 中它为空。
我查了一下,那里的RoutingContext有session和cookie对象。
我在设置路由之前添加了 BodyHandler、CookieHandler、SessionHandler.create(LocalSessionStore.create(vertx))、UserSessionHandler.create(adminAuth)。
我使用 OpenAPI3RouterFactory 来设置路由。
我想,我做了文档和示例代码中的所有操作。 我错过了什么?
suspend fun createJDBCAuth(vertx : Vertx, name : String) : JDBCAuth
{
val con = getConnection(vertx, name)
return JDBCAuth.create(vertx, con)
.setAuthenticationQuery("SELECT PASSWORD, PASSWORD_SALT FROM \"user\" WHERE USERNAME = ?")
.setHashStrategy(JDBCHashStrategy.createPBKDF2(vertx))
.setNonces(json {
array(45, 385)
})
}
adminAuth = createJDBCAuth(vertx, "adminreader")
OpenAPI3RouterFactory.create(vertx, "openapi.yaml")
{ ar ->
if (ar.succeeded())
{
// Spec loaded with success
var factory = ar.result()
logger.info("Factory succeed")
factory.setBodyHandler(BodyHandler.create().setBodyLimit(1024 * 1024))
.addGlobalHandler(CookieHandler.create())
.addGlobalHandler(SessionHandler.create(LocalSessionStore.create(vertx)))
.addGlobalHandler(UserSessionHandler.create(adminAuth))
.addGlobalHandler(CorsHandler.create("https://localhost:3000")
.allowCredentials(true)
.allowedHeader("Content-Type")
.allowedMethod(HttpMethod.GET)
.allowedMethod(HttpMethod.POST)
.allowedMethod(HttpMethod.OPTIONS)
)
inputMap.forEach { path ->
when {
path.key == "/admin/login" -> factory.addHandlerByOperationId(path.value.operationId, adminLoginHandler)
path.key.startsWith("/admin/") -> factory.addHandlerByOperationId(path.value.operationId, adminReadHandler)
}
}
更新函数。
private val adminLoginHandler = Handler<RoutingContext>
{ rc ->
adminAuth.authenticate(rc.bodyAsJson)
{ res ->
launch {
var retJson = json { obj() }
if (res.succeeded()) {
var user = res.result()
rc.session()?.regenerateId()
rc.setUser(user)
retJson = retJson.put("succeed", true)
if (user.isAuthorizedAwait("createadmin"))
retJson = retJson.put("createadmin", true)
} else {
retJson = retJson
.put("succeed", false)
.put("message", res.cause().message)
}
rc.response()
.putHeader("content-type", "application/json")
.end(retJson.encode())
}
}
}
private val adminReadHandler = Handler<RoutingContext>
{ rc ->
if (rc.user() == null)
rc.response().setStatusCode(403).end()
else
{
val user = rc.user()
val principal = user.principal()
}
}
事实证明,问题不在后端 (vertx) 端。 我在前端代码中使用浏览器获取 api,但我忘记包含凭据:"include" 选项。
Francesco:谢谢你的努力。你的要点帮助我一点一点地追溯问题。
P.s.: 虽然我的 authn/authz 现在工作了,但我仍然没有在 chrome 调试器的 network/cookies 面板上看到 cookies,使用 fetch api。如果我直接将 url 写入浏览器,我会看到所有内容。