尝试从 JAX-RS 网络服务返回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 Web 服务代码,用于生成 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();
}
我认为我返回生成的 JWT 的格式/方式是错误的,但无法意识到如何修复它。谢谢你。