【发布时间】:2023-03-30 00:57:02
【问题描述】:
试图从 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 网络服务代码,用于生成 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 的格式/方式是错误的,但不知道如何修复它。谢谢。
【问题讨论】:
-
请注意,jose4j 代码生成的是 JWE 而不是 JWT,因为有效负载只是字符串
"Hello World!"。看看 bitbucket.org/b_c/jose4j/wiki/JWT%20Examples 生产和使用 JWT。 -
嗨,从安全的角度来看,如果我使用 JWE 或 JWT 有什么区别吗?例如,如果我只需要在有效负载中存储用户的电子邮件,我可以使用 JWE 吗?
-
有很多不同之处,这里就不多说了。 JWT 只是一个 JWS 或 JWE(或 JWE 中的嵌套 JWS),其中 JSON 声明作为有效负载。像 aud、iss 和 exp 这样的声明确实提供了额外的安全上下文,但可能并不总是必要的。 JWS 和 JWE 都可以与对称或非对称密钥/算法一起使用,并且安全特性不同。在发行您还将使用的令牌时(也许您在做什么?),基于 JWE 的 JWT 具有对称的 alg 和电子邮件的子声明以及 exp 声明,因此它不会永远有效,这可能是一个极简主义的解决方案。跨度>
标签: angularjs jax-rs jwt satellizer