SyntaxError: Unexpected token t when using sattelizer and JAX-RS

SyntaxError: Unexpected token t when using sattelizer and JAX-RS

正在尝试从 JAX-RS 网络服务 return JWT 但出现以下错误:

SyntaxError: Unexpected token t
    at Object.parse (native)
    at fromJson (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:1250:14)
    at defaultHttpResponseTransform (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:9371:16)
    at http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:9462:12
    at forEach (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:336:20)
    at transformData (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:9461:3)
    at transformResponse (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:10241:23)
    at processQueue (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:14634:28)
    at http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:14650:27
    at Scope.$get.Scope.$eval (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:15916:28)

这是我的 LoginController 代码,我尝试从中执行登录:

app.controller('LoginController', function($scope, $log, $auth){
    $scope.login = function() {
          $auth.login($scope.user)
            .then(function() {
              $log.info('You have successfully signed in');
              $location.path('/');
            })
            .catch(function(response) {
                $log.info(response.data, response.status);
            });
        };

});

这是发布 JWT 令牌的 JAX-RS 网络服务代码,用于生成 JWT 我正在使用 jose.4.j 库:

@POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response mergeInfo(String json){
        Gson gson = new GsonBuilder()
        .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
        .setPrettyPrinting().create();

        System.out.println(json);

        String serializedJwe = null;
        try {
             Key key = new AesKey(ByteUtil.randomBytes(16));
             JsonWebEncryption jwe = new JsonWebEncryption();
             jwe.setPayload("Hello World!");
             jwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.A128KW);
             jwe.setEncryptionMethodHeaderParameter(ContentEncryptionAlgorithmIdentifiers.AES_128_CBC_HMAC_SHA_256);
             jwe.setKey(key);
             serializedJwe = jwe.getCompactSerialization();
             System.out.println("Serialized Encrypted JWE: " + serializedJwe);
             jwe = new JsonWebEncryption();
             jwe.setKey(key);
             jwe.setCompactSerialization(serializedJwe);
             System.out.println("Payload: " + jwe.getPayload());
        } catch (Exception e) {
            e.printStackTrace();
        }

        String token = "{token: " + serializedJwe + "}";

        return Response.ok(token).build();

    }

我认为我 return 在其中生成 JWT 的 format/way 是错误的,但不知道如何修复它。谢谢。

好的,我知道它不起作用的原因,我的 JSON 字符串格式错误。

这是将其解析为 json 的正确字符串:var test = '{"token":"test"}' 因此,如果您执行:JSON.parse(test),您将得到:Object {token: "test"}

但是! 如果你使用这样的字符串 var test = "{'token':'test'}" 解析为 javascript 对象,你将得到 Uncaught SyntaxError: Unexpected token '

所以我在我的 Java 应用程序中创建了以下 class:

public class Token {
    private String token;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}

然后将我代码的最后 3 行更改为以下内容:

        Token token = new Token();
        token.setToken(serializedJwe);

        return Response.ok(gson.toJson(token)).build();

现在一切正常。