【问题标题】:getting token as cookie from spring boot not accessible in angularjs从spring boot获取令牌作为cookie在angularjs中无法访问
【发布时间】:2017-03-23 16:03:26
【问题描述】:

cookie 确实显示在浏览器中,但无法使用 $cookie.get('io') 访问。我错过了什么。我还尝试了 $timeout 延迟 5 秒。我试图在 headers() 中查看,但该标记没有显示在那里。

代码:

$http({
              url: 'http://localhost:8081/api/v1/login',
              method: 'POST',
              data: $httpParamSerializerJQLike({
                    username: username,
                    password: password,
                    REQUEST_TYPE: requestType
                }), // Make sure to inject the service you choose to the controller
              headers: {

                'Content-Type': 'application/x-www-form-urlencoded' // Note the appropriate header
              }
            }).success(function (data, status, headers, config) {

//10 seconds delay
        $timeout( function(){
           var favoriteCookie = $cookies.get('io');
            alert(favoriteCookie);
        }, 5000 );

        //time
        var time = 0;

        //timer callback
        var timer = function() {
            if( time < 5000 ) {
                time += 1000;
                $timeout(timer, 1000);
            }
        }

        //run!!
        $timeout(timer, 1000);       




                //console.log(response.headers('set-cookie'));

                callback(response = { success: true });
            }).error(function (data, status, headers, config) {
                callback(response = { success: false });
            });

【问题讨论】:

    标签: angularjs cookies


    【解决方案1】:

    请检查您尝试获取的 cookie 是否未标记为 httpOnly。

    当您使用 HttpOnly 标志标记 cookie 时,它​​会告诉浏览器 这个特定的 cookie 只能由服务器访问。任何 严格禁止尝试从客户端脚本访问 cookie。 当然,这假设您拥有: 现代网络浏览器。浏览器 实际上正确实现了HttpOnly。

    【讨论】:

      【解决方案2】:

      它是自动生成的 cookie。我试图访问的 cookie 没有出现在浏览器中。我必须在 angularjs 中进行一些代码修改才能将 cookie 放入浏览器。我必须在 http 请求中包含一个参数“withCredentials:true”。一旦我这样做了,我的 cookie 就会出现在浏览器中。现在我的 http 请求看起来像这样。

      $http({
                        url: 'http:localhost/login',
                        method: 'POST',
                        data: $httpParamSerializerJQLike({
                              username: username,
                              password: password,
                              REQUEST_TYPE: requestType
                          }), // Make sure to inject the service you choose to the controller
                        withCredentials:true,
                        headers: {
                          'Content-Type': 'application/x-www-form-urlencoded; 
      
                        }
                      }).success(function (data, status, headers, config) {
      
                          callback(response = { success: true });
                      }).error(function (data, status, headers, config) {
                          callback(response = { success: false });
                      });
      
      
      
                  }
      

      【讨论】:

        猜你喜欢
        • 2014-05-04
        • 2018-02-05
        • 1970-01-01
        • 2018-10-10
        • 2023-02-14
        • 2019-08-16
        • 2022-01-01
        相关资源
        最近更新 更多