如何使用 Principal 或 Account 服务获取当前登录用户
How to get the current logged in user using Principal or Account service
我正在尝试加载特定于登录用户的 $scope.projects。 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
那样的RESTurl我们可以直接在工厂中使用/projects
以及javarest controller。
这最终将有助于获取当前登录用户的项目列表。
希望对某人有所帮助。
我认为当前用户不能每次都从服务器返回...
当前用户必须在 Principal 服务中注册,因为在应用程序中,我们可能会出于不同目的多次调用此函数。如果在其他服务中,您不想收到验证当前用户的承诺,则可以在 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;
当然,当用户注销时,您必须重置它或将其设置为 {} 或其他任何内容。
我正在尝试加载特定于登录用户的 $scope.projects。 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
那样的RESTurl我们可以直接在工厂中使用/projects
以及javarest controller。
这最终将有助于获取当前登录用户的项目列表。
希望对某人有所帮助。
我认为当前用户不能每次都从服务器返回... 当前用户必须在 Principal 服务中注册,因为在应用程序中,我们可能会出于不同目的多次调用此函数。如果在其他服务中,您不想收到验证当前用户的承诺,则可以在 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;
当然,当用户注销时,您必须重置它或将其设置为 {} 或其他任何内容。