如何使用Principal或Account服务获取当前登录的用户



我正在尝试加载$scope。特定于登录用户的项目。REST api端是

@RequestMapping(value = "/users/{id}/projects", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<List<Project>> getAllProjects(@PathVariable("id") String id,
        HttpServletResponse response)
        throws URISyntaxException {
    log.debug("REST request to get User : {}", id);
    User user = userRepository.findOneByLogin(id);
    if (user == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
    List<Project> page = projectRepository.findByUserIsCurrentUser();
    return new ResponseEntity<List<Project>>(page,
            HttpStatus.OK);
}

使用swagger客户端检查。

    angular.module('tfmappApp')
.factory('UserProject', function ($resource) {
    return $resource('api/users/:id/projects', {}, {
            'query': {method: 'GET', isArray: true, params: { id: '@login'}},
            'get': {
                method: 'GET',
                transformResponse: function (data) {
                    data = angular.fromJson(data);
                    return data;
                }
            }
        });
    });
angular.module('tfmappApp')
.controller('ProjectController', function ($scope, Principal, Project, User, UserProject, ParseLinks) {
    $scope.projects = [];
    $scope.page = 1;
    Principal.identity().then(function(account) {
        $scope.account = account;
        $scope.isAuthenticated = Principal.isAuthenticated;
    });
    $scope.loadAll = function() {
        $scope.projects = UserProject.query({id: $scope.account.login});
    };
    $scope.loadPage = function(page) {
        $scope.page = page;
        $scope.loadAll();
    };
    $scope.loadAll();});

主体服务是Jhipster生成的。下面的代码不工作,或者我错过了一些东西。

Principal.identity().then(function(account) {
        $scope.account = account;
        $scope.isAuthenticated = Principal.isAuthenticated;
    });

如何获取当前登录的用户?

获取当前登录的帐户或主体或用户的正确方法是什么?

我正在使用Jhipster生成的HTTP会话认证

我有一些解决这个问题的方法。不是我想要的。

@Query("select project from Project project where project.user.login = ?#{principal.username}")
List<Project> findByUserIsCurrentUser();

是jhipster生成的代码,用于加载当前用户的项目列表。从项目到用户是多对一的。

所以不需要像/users/{id}/projects那样的REST url,我们可以直接在工厂中使用/projects以及java REST控制器。

这将最终帮助获得当前登录用户的项目列表。

我认为当前用户不能每次我们想要它时都从服务器返回…当前用户必须在Principal服务中注册,因为在应用程序中,我们可能会出于不同的目的多次调用此函数。如果在其他服务中,您不想接收验证当前用户的承诺,则可以在Principal服务中使用当前用户变量。因此,为此,我只是创建了一个Principal字段,每次用户登录时我都会更新它…(每次在主体服务中调用身份函数):

var principalService = this;
// retrieve the identity data from the server, update the identity object, and then resolve.
Account.get().$promise
            .then(function (account) {
                _identity = account.data;
                principalService.currentIdentity = angular.copy(_identity);
                console.log('current identity :' + JSON.stringify(principalService.currentIdentity));
                _authenticated = true;
                deferred.resolve(_identity);
                Tracker.connect();
            })
            .catch(function () {
                _identity = null;
                _authenticated = false;
                deferred.resolve(_identity);
            });
        return deferred.promise; 

当然,当用户注销时,你必须重置它或将其设置为{}或其他。

最新更新