【发布时间】:2014-10-13 23:26:36
【问题描述】:
我正在使用 Spring Security 并尝试显示登录用户的角色。
Spring Sec 正在运行 - 我有有效的注册和登录 JS 功能(我可以在数据库中看到新用户,并且我可以进行有效登录)但是当我尝试获取当前用户的角色信息并通过简单的方式显示它时{{role}} 在前端的简单 html 文件中出现此错误:
但我仍然收到来自 JSON 的有效响应:
这是我的 user.html,其中 data-ng-init 和 {{role}} 是:
<div class="masthead">
<h4 class="text-center" data-ng-init="init()">
<strong>Users Home Screen</strong>
</h4>
<p>{{role}}</p>
<br>
...
</div>
这是我的 userController.js(它通过 app.js 连接到 user.html):
collectionsApp.controller('userController', function($scope, userService,
$state) {
$scope.init = function() {
userService.getAuthorization(displayRole);
}
displayRole = function(response) {
$scope.role = response.data;
}
});
我的 userService.js:
collectionsApp.service('userService', function($http) {
return {
login : function(user, callback) {
$http.post('/user/login', user).then(callback);
},
register : function(user, callback) {
$http.post('/user/add', user).then(callback);
},
getAuthorization : function(callback) {
$http.get('/user/getAuthorization').then(callback);
}
};
});
我的 UserController.java(本文只展示感兴趣的功能):
@RequestMapping(value = "/getAuthorization", method = RequestMethod.GET)
public String getAuthorization() {
return SecurityContextHolder.getContext().getAuthentication().getAuthorities().toString();
}
哦,还有一件奇怪的事情: 当我设置断点(如下图所示)并在 user.html 上按 f5 时,调试的行为就像它永远不会到达断点并在控制台中报告提到的 SyntaxError。如果你问我,那是相当超自然的...... :)
【问题讨论】:
-
[ROLE_ANONYMOUS]是不是有效的 JSON。["ROLE_ANONYMOUS"]会。 -
我该怎么做?如何解决?
-
那么您的服务器代码如何发回格式错误的响应?那将是您修复它的地方。
-
好的,我知道这是问题
return SecurityContextHolder.getContext().getAuthentication().getAuthorities().toString();,但您知道如何发送 ["SOME_ROLE"] 而不是 [SOME_ROLE]?
标签: java javascript json angularjs spring-security