【问题标题】:How to keep the session with angular-http-auth?如何保持与 angular-http-auth 的会话?
【发布时间】:2013-10-05 06:24:37
【问题描述】:

我在 angular-js 应用程序中使用 angular-http-auth 进行身份验证。

这是登录控制器内部的登录功能:

$scope.login = function() {
    var credentials = Base64.encode($scope.username + ':' + $scope.password);
    var config = { headers: { 'Authorization': 'Basic ' + credentials } };          
    $http.get('url/to/json/user', config)
        .success(function() {
            $http.defaults.headers.common['Authorization'] = 'Basic ' + credentials;
            authService.loginConfirmed();
            console.log('login success');
        })
        .error(function() {
            console.log('login failed');
    });
}

(base64是来自here的加密服务)

问题:如果用户已经登录并且他打开了一个新选项卡,或者如果他重新加载页面,他必须再次登录。

如果用户重新加载页面或来自外部链接,如何避免这种情况并保持会话打开?

【问题讨论】:

  • 请注意:base64 是编码,不是加密。它很容易可逆,无需密钥,因此会混淆身份验证信息,并使其免受其他编码问题(如 HTTP 编码)的影响,但不会增强安全性。

标签: session angularjs http-authentication


【解决方案1】:

您可以使用 cookie 或 html5 数据存储来保存凭据或带有凭据的 base64 字符串。然后,您可以从那里加载它们并将它们解析为 $http.defaults.headers.common['Authorization'] = 'Basic ' + credentials;

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    相关资源
    最近更新 更多