在角应用程序中使用$ http发送mojolicious会话cookie

时间:2014-10-24 19:45:31

标签: angularjs perl session-cookies mojolicious

我希望由角度($http)提出的GET请求包含会话cookie。我虽然包括$httpProvider.defaults.withCredentials = true;就足够了。但是,没有请求包含cookie。

我有一个mojolicious应用程序服务角度应用程序。 Mojolicious公开登录API:

  • POST验证
  • GET指示会话是否仍然有效(如果正常则返回带有值的名称,否则返回null)。

会话cookie需要包含在GET请求中。 如何以角度实现?

» curl localhost:3000/login -d '{"user": "test", "password": "pswd"}' -c cookie.txt
{"login":1,"name":"test"}

» curl localhost:3000/login -b cookie.txt
{"name":"test"}

» curl localhost:3000/login 
{"name":null}

» cat cookie.txt
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_localhost FALSE   /   FALSE   1414178938  lncddbv3    eyJleHBpcmVzIjoxNDE0MTc4OTM4LCJ1c2VyIjoidGVzdCJ9--7fa22efde0d87e7d79f2d29d7adb97ab92f632c4

客户端:

<html lang="en">
<head>
  <title>login test</title>
  <script type="text/javascript" src="lib/jquery-1.11.0.min.js"></script>
  <script type="text/javascript" src="lib/angular.js"></script>
  <script type="text/javascript" src="bower_components/angular-cookies/angular-cookies.js"></script>
  <script>
    var URL='http://localhost:3000/';

    angular.module('loginTest',['ngCookies'])
        .config(function($httpProvider) {
          $httpProvider.defaults.withCredentials = true;
        })

    angular.module('loginTest')
      .controller('loginCtrl',function loginCtrl($scope,$http){

         // is our session valid?
         $scope.isvalid = function() {
              $http({
                 url: URL+'login/',
                 method:"GET",
                 headers: {'Content-Type': 'application/json'},
                })
               .success(
                 function(data){
                   console.log('isvalid',data);
                   $scope.validname = data.name;
                });
         };

         // authenticate session
         // call isvalid on success
         $scope.auth = function(){
              $http({
                 url: URL+'login/',
                 method:"POST",
                 headers: {'Content-Type': 'application/json'},
                 dataType: 'json',
                 data: {user: "test", password: "pswd" }
              })
              .success(
                 function(data){
                   console.log('login', data);
                   $scope.authname = data.name;
                   $scope.isvalid()
                 }
              );
            };

       // try both. auth calls valid
       $scope.auth()
      });

  </script>
</head>
<body ng-app="loginTest">
 <div ng-controller="loginCtrl">
   <button ng-click="auth()">auth</button> 
   <button ng-click="isvalid()">valid?</button> <br>
   auth as: "{{authname}}";<br>
   valid as: "{{validname}}"
 </div>
</body>
</html>

如浏览器+ firebug所示:

Firebug Output

Auth API:

  ## use angular files
  push @{$self->static->paths}, $self->home->rel_dir('../angular/');

  ## routes
  my $r=$self->routes;
  $r->get ('/login')->to('login#isLogin');
  $r->post('/login')->to('login#login');

  #### Login.pm
  sub login {
   my $self=shift;
   my $json =decode_json($self->req->body || '{}');
   $self->session(user => $json->{user}) 
          if($json->{user} eq "test" && $json->{password} eq "pswd");

  }

  sub isLogin {
   my $self = shift;
   $self->render(json => {name=>$self->session('user') } );
  }

  ####

1 个答案:

答案 0 :(得分:2)

使用localhost.localdomain127.0.0.1代替localhost

Cookie会针对有效域的所有请求发送。有效的域名至少需要一个点。

Cookies on localhost with explicit domain