我使用vert.x设置了JDBCAuth,并且身份验证有效。我在JDBCAuth.authenticate(adminLoginHandler)之后得到User对象,但是在下一个HTTP GET查询(adminReadHandler)的RoutingContext中它为null。
我检查了一下,RoutingContext上有会话和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()
}
}
答案 0 :(得分:1)
原来,问题不在后端(vertx)一侧。 我在前端代码中使用了浏览器访存api,却忘记了包含凭据:“ include”选项。
弗朗切斯科:谢谢您的努力。您的要旨帮助我一步一步地找到问题所在。
P.s .:尽管我的authn / authz现在可以正常工作,但使用提取API仍无法在chrome调试器的网络/ cookie面板上看到cookie。如果我直接将网址写到浏览器中,那么我将看到所有内容。