2016-07-26 11 views
0

私はできる限り問題を解決しましたが、それでも解決できません。エクスプレスセッションのCookieをデコードする方法は?

Iループバックで空のサーバーを作成したが、次にexpress-sessionbody-parser ミドルウェア

"session": { 
    "express-session": { 
     "params": { 
     "name": "id", 
     "secret": "potato with eyes", 
     "httpOnly": "false" 
     } 
    } 
    }, 
    "auth": {}, 
    "parse": { 
    "body-parser": {}, 
    "body-parser#json": {}, 
    "body-parser#urlencoded": {"params": { "extended": true }} 
    } 

を追加し、Iはroot.js.にメソッドのペアを追加しました。最初の変数は変数にsession.idを設定します。 2番目のユーザーは次のリクエストでログに記録します。

module.exports = function (app) 
{ 
    var cookieParser = require("cookie-parser") 
    var router = app.loopback.Router() 
    router.post("/login", function (req, res) 
    { 
     req.session.id = req.body.id 
     console.log("Set id to " + req.body.id) 
     res.end() 
    }) 
    router.post("/addLead", function (req, res) 
    { 
     console.log("session", req.session) 
     console.log("got id: ", req.session.id) 
     console.log("decrypting single id:", cookieParser.signedCookie(req.session.id, "potato with eyes")) 
     console.log("cookie! ", req.cookie) 
     console.log("cookie parsed:", cookieParser.JSONCookie(req.cookie, "potato with eyes")) 
     console.log("cookie parsed:", cookieParser.signedCookie(req.cookie, "potato with eyes")) 
     res.status(403).send("You must login first") 
    }) 
    app.use(router) 
} 

Cookieの復号化に失敗しました。 idを解読してもそれは変わりません。ここで

Web server listening at: http://localhost:3000 
Browse your REST API at http://localhost:3000/explorer 
Set id to 15 
session Session { 
    cookie: 
    { path: '/', 
    _expires: null, 
    originalMaxAge: null, 
    httpOnly: true } } 
got id: XifrUZXLhKcDHYqQwxESQlLQQ6N1j49d 
decrypting single id: XifrUZXLhKcDHYqQwxESQlLQQ6N1j49d 
cookie! undefined 
cookie parsed: undefined 
cookie parsed: undefined 

は、私がテストのために使用されるHTMLです:

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta charset="utf-8" /> 
    <title></title> 
    <script src="https://code.jquery.com/jquery-3.1.0.min.js" 
      integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" 
      crossorigin="anonymous"></script> 

    <script> 
     function log(data) 
     { 
      console.log(JSON.stringify(data)) 
     } 
     $.post("http://localhost:3000/login", { name: "John Smit", id: 15 }, function (data) 
     { 
      log(data) 
      $.post("http://localhost:3000/addLead", { 
       name: "Anton Berezin", phone: "337225", model: "1234", 
       mark: "Toyota", yearFrom: "2014", yearTo: "2017" 
      }, function (data) 
      { 
       log(data) 
       alert(JSON.stringify(data)) 
      }) 
      .fail(function (error) 
      { 
       log(error) 
      }) 

     }) 
     .fail(function(error) 
     { 
      alert(JSON.stringify(error)) 
     }) 
    </script> 
</head> 
<body> 

</body> 
</html> 

とプロジェクトとのアーカイブ: http://www.filedropper.com/cookietest

私は任意のヘルプ


編集を感謝します:でテストされた req.session.cookieの代わりに0。出力を変更しませんでした。

+0

このようにあなたのコードを変更しますが、なぜあなたは 'req.session.cookie'を読んでみてくださいないとき'クッキーparser'解析それは 'req.cookie'ですか? –

+0

ありがとう、それは助けにならなかった)だからボディパーサーとcookieParserはenougする必要がありますか? – user2136963

+0

'' var router = ... 'の前に' app.use(require( 'cookie-parser')());を追加してみてください。 –

答えて

0

cookie-parserexpress-sessionanymoreを使用しないでください。あなたは特急・セッションを登録しているので

は、私はたぶん私はなめらかを逃した

module.exports = function (app) 
{ 
    var router = app.loopback.Router() 

    // serialize the session 
    router.post("/login", function (req, res) 
    { 
     req.session = { 
      userId: 123, 
      accessToken: 'eadeadaew' 
      // or whatever data you want to store in the session 
     }; 
     console.log("Set session"); 
     res.end(); 
    }); 

    // deserialize the session 
    router.post("/addLead", function (req, res) 
    { 
     if(req.session) { 
      console.log("session", req.session); 
      console.log("user id: ", req.session.userId); 
      console.log("token ", req.session.accessToken); 
      res.end(); 
     } else { 
      res.status(403).send("You must login first"); 
     } 
    }) 
    app.use(router); 
} 
関連する問題