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



i使用了一个使用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 Boot从数据库中获取信息

@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!';
    });
};

问题是我继续登录失败,但用户在数据库中,并且核心已被弃用,因此不知道不知道我做错了什么,这对您有很大的赞赏吗?

有一个查看:为什么要弃用Angular $ HTTP成功/错误方法?从v1.6?

中删除

成功和错误方法正在幕后进行一些工作以解开响应。因此,您可能需要做

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

最新更新