【问题标题】:Is there something like sessions in dart?飞镖中有类似会话的东西吗?
【发布时间】:2014-10-06 22:36:40
【问题描述】:

这似乎是一个愚蠢的问题,但我刚刚开始使用 dart,需要验证用户会话,类似于使用 $_SESSION 数组对 PHP 执行的操作...

所以我正在编写一个基本的服务器后端和一个前端,并且需要对通过 XMLHttpRequest 进来的一些请求进行身份验证。后端根据给定的前端是否经过身份验证发回 JSON。在某些情况下,前端可以更新 DOM,但 为通过身份验证的用户。

不确定我是否解释得很好......

任何建议将不胜感激!

谢谢!

【问题讨论】:

  • 我搜索了可以实现会话的 dart 服务器端 Web 框架,结果发现:github.com/lvivski/start 可能还有其他的。

标签: session authentication dart


【解决方案1】:

会话是一种高级功能,不应成为任何语言的一部分。你可以通过实现类似的东西自己包含会话功能。

  Map<String, int> sessions = {'abcdef12345' : 42}; // exists in e.g. datastore and is managed by an authentication routine 

  String authenticationToken = 'abcdef12345'; // comes from the request
  if(sessions.containsKey(authenticationToken)) {
    print('User ${sessions[authenticationToken]} is at least authenticated but might not have the appropriate rights to perform this operation.');
  } else {
    print('Not authenticated.');
  }

【讨论】:

    【解决方案2】:

    没有内置会话,但您可以查看最近的 Google API 库,以便 Dart 使用 oAuth:

    Google API Dart Client

    【讨论】:

    • 有趣 - 将检查这一点。谢谢!
    【解决方案3】:

    shelf_authshelfJwtSessionHandler 实现提供了一个很好的全自动会话处理解决方案

    var authMiddleware = authenticate([new RandomAuthenticator()],
          new JwtSessionHandler('super app', 'shhh secret', testLookup));
    

    【讨论】:

    • 这已经过时了,现在还没有维护 - Dart 2 有什么可比的吗?
    猜你喜欢
    • 1970-01-01
    • 2014-09-05
    • 2015-07-18
    • 2020-04-10
    • 1970-01-01
    • 2021-08-04
    • 2012-05-31
    • 2012-11-27
    • 2017-11-21
    相关资源
    最近更新 更多