Java Object.parse处的JSON意外标记

Java Object.parse处的JSON意外标记,java,javascript,json,angularjs,spring-security,Java,Javascript,Json,Angularjs,Spring Security,我正在使用SpringSecurity,并试图显示登录用户的角色 Spring Sec正在运行-我有工作的register和login JS函数(我可以在数据库中看到新用户,并且我可以进行有效登录),但是当我尝试获取当前用户的角色信息并通过前端简单html文件中的简单{role}显示时,我得到以下错误: 但我仍然从JSON得到了一个有效的响应: 这是my user.html,其中数据ng init和{{role}}是: <div class="masthead"> &l

我正在使用SpringSecurity,并试图显示登录用户的角色

Spring Sec正在运行-我有工作的register和login JS函数(我可以在数据库中看到新用户,并且我可以进行有效登录),但是当我尝试获取当前用户的角色信息并通过前端简单html文件中的简单{role}显示时,我得到以下错误:

但我仍然从JSON得到了一个有效的响应:

这是my user.html,其中数据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>
My 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);
        }

    };
}); 
My UserController.java(本文仅显示感兴趣的函数):

哦,还有一件奇怪的事: 当我放置断点(如下图所示)并在user.html上点击f5时,调试的行为就像它从未到达断点一样,并在控制台中报告提到的SyntaxError。如果你问我,那是相当超自然的…:)


您的响应无效<如果将包含单个字符串元素“SOME_ROLE”的数组转换为JSON,将生成code>[“SOME_ROLE”]。如果您正在使用Google的Gson库,您可以执行以下操作:

Gson gson = new Gson();
gson.toJson(new String[]{SecurityContextHolder.getContext().getAuthentication().getAuthorities().toString‌​()});

[ROLE\u ANONYMOUS]
不是有效的JSON<代码>[“匿名角色”]应该是。我该怎么做?如何修复它?那么服务器代码是如何发回格式错误的响应的?好的,我知道问题出在这里
返回SecurityContextHolder.getContext().getAuthentication().getAuthority().toString()
但是你知道如何发送[“SOME_ROLE”]而不是[SOME_ROLE]吗?使用库生成JSON:
@RequestMapping(value = "/getAuthorization", method = RequestMethod.GET)
public String getAuthorization() {
return SecurityContextHolder.getContext().getAuthentication().getAuthorities().toString();
}
Gson gson = new Gson();
gson.toJson(new String[]{SecurityContextHolder.getContext().getAuthentication().getAuthorities().toString‌​()});