gpt4 book ai didi

dart - 如何将数据从channel.dart传递到中间件句柄方法?

转载 作者:行者123 更新时间:2023-12-03 03:41:55 27 4
gpt4 key购买 nike

我创建了一个中间件 Controller ,该 Controller 负责身份验证。 Controller 将链接在资源 Controller 的前面。我认为这是正确的方法?

在handle方法中,我正在执行数据库查询,该查询需要连接信息。但是,当我尝试通过构造函数传递数据时,它告诉我不允许使用setter。

将带有连接信息的Map从channel.dart传递到中间件 Controller 的最佳方法是什么(请参阅dbConnectionDetails)?

channel.dart

@override
Future prepare() async {
logger.onRecord.listen((rec) => print("$rec ${rec.error ?? ""} ${rec.stackTrace ?? ""}"));

dbConnectionDetails = ConnectionSettings (
user: "mydbuser",
password: "mydbpass",
host: "localhost",
port: 3306
);
}

@override
Controller get entryPoint {
final router = Router();

router
.route("/api/user/query/complex")
.link(() => AuthMiddleware())
.link(() => UserComplexQueryController(dbConnectionDetails));
}

在上面的示例中,我可以将dbConnectionDetails传递到UserComplexQueryController的构造函数中,以便可以将其用于数据库连接。

但是,如果将dbConnectionDetails传递到AuthMiddleWare的构造函数中,则会出现以下错误:

ArgumentError (Invalid argument(s): Invalid controller 'AuthMiddleware'. Controllers must not have setters and all fields must be marked as final, or it must implement 'Recyclable'.)



在我的案例中,AuthMiddleWare正在URL中解析一个 token ,并将检查该 token 在数据库中是否有效。如您所见,除了直接在AuthMiddleware isValid函数内添加dbConnectionDetails之外,我没有发现其他方法。但是我希望,如果我可以将其从channel.dart传递下来。
class AuthMiddleware extends Controller {
// This does not work, as setters not allowed
//AuthMiddleware(this.dbConnectionDetails);
//
//ConnectionSettings dbConnectionDetails

@override
Future<RequestOrResponse> handle(Request request) async {
if (await isValid(request)) {
return request;
}
return Response.unauthorized();
}

Future<bool> isValid(Request request) async {
final String token = request.raw.requestedUri.queryParameters["token"];
final int timestamp = (DateTime.now().toUtc().millisecondsSinceEpoch / 1000).round();

ConnectionSettings dbConnectionDetails = ConnectionSettings (
user: "myuser",
password: "mypass",
host: "localhost",
port: 3306
);

AuthTokenDao authTokenDao = new AuthTokenDao(dbConnectionDetails);

if(await authTokenDao.validateToken(token, timestamp)) {
return true;
}

return false;
}
}

是的,您已经看对了。我正在连接到MySQL数据库:)

最佳答案

由CA编辑:解决方案(由Joe Conway提供):

class AuthMiddleware extends Controller {
// Works by adding "final"
final ConnectionSettings dbConnectionDetails;

// Now dbConnectionDetails can be passed within the constructor.
AuthMiddleware(this.dbConnectionDetails);

@override
Future<RequestOrResponse> handle(Request request) async {
if (await isValid(request)) {
return request;
}
return Response.unauthorized();
}

Future<bool> isValid(Request request) async {
final String token = request.raw.requestedUri.queryParameters["token"];
final int timestamp = (DateTime.now().toUtc().millisecondsSinceEpoch / 1000).round();

/* Removed this
ConnectionSettings dbConnectionDetails = ConnectionSettings (
user: "myuser",
password: "mypass",
host: "localhost",
port: 3306
);
*/
AuthTokenDao authTokenDao = new AuthTokenDao(dbConnectionDetails);

if(await authTokenDao.validateToken(token, timestamp)) {
return true;
}

return false;
}
}

关于dart - 如何将数据从channel.dart传递到中间件句柄方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54062590/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com