【问题标题】:angular2 with Slim framework jwt authenticationangular2 与 Slim 框架 jwt 身份验证
【发布时间】:2017-05-18 06:45:36
【问题描述】:

我正在尝试创建一个服务来验证 angular2 中的用户名和密码。 这是 authentication.service.ts 的代码

从'@angular/core'导入{可注射}; 从 '@angular/http' 导入 { Http, Headers, Response }; 从'rxjs'导入{ Observable }; 从'ts-md5/dist/md5'导入{Md5}; 导出界面用户{ 用户名:字符串; 密码:字符串; } @Injectable() 导出类 AuthenticationService { 公共令牌:字符串; 构造函数(私有 http:Http){ // 如果保存在本地存储中,则设置令牌 var currentUser = JSON.parse(localStorage.getItem('currentUser')); this.token = currentUser && currentUser.token; } 登录(用户:用户):可观察 { return this.http.post('http://localhost/hj1/api/authenticate', JSON.stringify({ 'user': user.userName, 'password': Md5.hashStr(user.password) })) .map((响应:响应)=> { // 如果响应中有 jwt 令牌,则登录成功 控制台日志(响应); 让 token = response.json() && response.json().token; 如果(令牌){ // 设置令牌属性 this.token = 令牌; // 将用户名和 jwt 令牌存储在本地存储中,以在页面刷新之间保持用户登录 localStorage.setItem('currentUser', JSON.stringify({ user: user, token: token })); //返回true表示登录成功 返回真; } 别的 { // 返回 false 表示登录失败 返回假; } } ); } 登出() { localStorage.removeItem("currentUser"); this.token = null; } }

这是我使用 slim 框架的 index.php

获取容器(); $container["jwt"] = 函数 ($container) { 返回新的标准类; }; $app->add(新\Slim\Middleware\JwtAuthentication([ "路径" => "/", "passthrough" => "/authenticate", "秘密" => getenv("HJ_ENV"), “错误” => 函数 ($request, $response, $arguments) { $data["状态"] = "错误"; $data["message"] = $arguments["message"]; 返回$响应 ->withHeader("内容类型", "应用程序/json") ->写(json_encode($数据,JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); }, "回调" => 函数 ($request, $response, $arguments) 使用 ($container) { $body = $response->getBody(); $body->write($arguments["decoded"]); $container["jwt"] = $arguments["decoded"]; } ])); $app->post('/authenticate', 'authenticate'); $app->运行(); 功能验证(请求 $request,响应 $response) { $params = json_decode($request->getBody()); $sql = "select * from users where userName = :userName"; $result = json_decode(runQuery($sql, [ ':userName', $params->user ]) ); $body = $response->getBody(); if ( $result && $result[0]->password == $params->password ) { $decoded = $request->getAttribute("jwt"); $body->write(json_encode([ 'token' => $decoded ]) ); } 别的 { $body->write(json_encode(['token' => null])); } } 函数 runQuery($sql, ...$params) { 尝试 { $db = getConnection(); $stmt = $db->prepare($sql); foreach ( $params 作为 $param ) { $stmt->bindParam($param[0], $param[1]); } $stmt->执行(); $行 = []; 而($row=$stmt->fetch(PDO::FETCH_OBJ)) { /*它在一行中获取数据。它是一个对象*/ array_push($rows, $row); } $db = 空; 返回 json_encode($rows); } 捕获(PDOException $e) { $db = 空; 返回 $e->getMessage() ; } } ?>

我的问题是 我无法从容器 ['jwt'] 获取令牌。 如果我提供不正确的用户名和密码,则令牌保持为空。 但如果我给出正确的用户名和密码。 $result 变量为我提供数据库中的数据。我可以验证密码。但是 $request->getAttribute("jwt") 这个方法给了我null。 我也检查了 $decoded = $container["jwt"] 但这也给了我空值。 所以我不知道如何获取 jwt 创建的令牌。 谢谢。

【问题讨论】:

    标签: php angular authentication jwt slim


    【解决方案1】:
    添加(新 \Slim\Middleware\JwtAuthentication([ "路径" => "/", "passthrough" => "/authenticate", “错误” => 函数 ($request, $response, $arguments) { $data["状态"] = "错误"; $data["message"] = $arguments["message"] ; 返回$响应 ->withHeader("内容类型", "应用程序/json") ->写(json_encode($数据,JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); } ])); $app->post('/authenticate', function (Request $request, Response $response) { $params = json_decode($request->getBody()); /* $params 将包含 angular 发布的用户和密码 在数据库中验证 */ /* 这里你从数据库中检索用户名和密码 */ if ( /* 在这里检查用户名和密码 */ ) { $now = 新日期时间(); $future = new DateTime("现在 +2 小时"); $有效载荷 = [ "iat" => $now->getTimeStamp(), "exp" => $future->getTimeStamp() ]; $secret = getenv("HJ_ENV"); /* 把你的密钥放在这里 */ $token = JWT::encode($payload, $secret, "HS256"); $数据[“状态”] =“确定”; $数据[“令牌”] = $令牌; 返回 $response->withStatus(201) ->withHeader("内容类型", "应用程序/json") ->写(json_encode($数据,JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); } 别的 { $data["状态"] = "错误"; $data["message"] = "无效令牌" ; 返回$响应 ->withHeader("内容类型", "应用程序/json") ->写(json_encode($数据,JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); } });

    【讨论】:

      猜你喜欢
      • 2018-07-03
      • 2016-09-05
      • 2019-03-01
      • 2021-12-21
      • 2015-11-16
      • 1970-01-01
      • 2017-05-29
      • 2019-07-11
      • 1970-01-01
      相关资源
      最近更新 更多