AngularJS 1.6 升级代码以登录不再使用 REST?

AngularJS 1.6 Upgrade code to login no longer working with REST?

我有一个使用 AngularJS 1.5.8 的项目,登录方法如下:

$scope.login = function() {
    // creating base64 encoded String from user name and password
    var base64Credential = btoa($scope.username + ':' + $scope.password);
    // calling GET request for getting the user details
    $http.get('user', {
        headers : {
            // setting the Authorisation Header
            'Authorization' : 'Basic ' + base64Credential
        }
    }).success(function(res) {
        $scope.password = null;
        if (res.authenticated) {
            $scope.message = '';
            // setting the same header value for all request calling from
            // this application
            $http.defaults.headers.common['Authorization'] = 'Basic ' + base64Credential;
            AuthService.user = res;
            $rootScope.$broadcast('LoginSuccessful');
            $state.go('dashboard');
        } else {
            $scope.message = 'Login Failed!';
        }
    }).error(function(error) {
        $scope.message = 'Login Failed!';
    });
};

这是使用 spring 通过此请求启动从数据库中获取信息

@RequestMapping("/user")
    public Principal user(Principal principal) {
        return principal;
    }

代码必须在 AngularJS 1.6.8 上更新到 运行 所以我一直在关注我在网上找到的教程等,现在有了这个:

$scope.login = function() {
    // creating base64 encoded String from user name and password
    var base64Credential = btoa($scope.username + ':' + $scope.password);
    // calling GET request for getting the user details
     $http({
          url: 'user',
          method: 'GET',
          headers : {
                // setting the Authorisation Header
                'Authorization' : 'Basic ' + base64Credential
            }
        })
        .then(function onSuccess(res) {
        $scope.password = null;
        if (res.authenticated) {
            $scope.message = '';
            // setting the same header value for all request calling from
            // this application
            $http.defaults.headers.common['Authorization'] = 'Basic ' + base64Credential;
            AuthService.user = res;
            $rootScope.$broadcast('LoginSuccessful');
            $state.go('dashboard');
        } else {
            $scope.message = 'Login Failed!';
        }
    }, function onError(res) {
        $scope.message = 'Login Failed!';
    });
};

问题是我一直登录失败,但用户在数据库中,并且 .success 已被弃用,所以不知道我做错了什么帮助非常感谢?

看看这个:

success 和 error 方法在幕后做一些工作来打开 response.data。所以你可能需要做

if (res.data.authenticated) ....