本文整理汇总了Java中io.vertx.ext.auth.jwt.JWTAuth.generateToken方法的典型用法代码示例。如果您正苦于以下问题:Java JWTAuth.generateToken方法的具体用法?Java JWTAuth.generateToken怎么用?Java JWTAuth.generateToken使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.vertx.ext.auth.jwt.JWTAuth
的用法示例。
在下文中一共展示了JWTAuth.generateToken方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: example7
import io.vertx.ext.auth.jwt.JWTAuth; //导入方法依赖的package包/类
public void example7(Vertx vertx, String username, String password) {
JWTAuthOptions config = new JWTAuthOptions()
.setKeyStore(new KeyStoreOptions()
.setPath("keystore.jceks")
.setPassword("secret"));
JWTAuth provider = JWTAuth.create(vertx, config);
// on the verify endpoint once you verify the identity of the user by its username/password
if ("paulo".equals(username) && "super_secret".equals(password)) {
String token = provider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions());
// now for any request to protected resources you should pass this string in the HTTP header Authorization as:
// Authorization: Bearer <token>
}
}
示例2: testAlgNone
import io.vertx.ext.auth.jwt.JWTAuth; //导入方法依赖的package包/类
@Test
public void testAlgNone() {
JWTAuth authProvider = JWTAuth.create(vertx, new JWTAuthOptions());
JsonObject payload = new JsonObject()
.put("sub", "UserUnderTest")
.put("aud", "OrganizationUnderTest")
.put("iat", 1431695313)
.put("exp", 1747055313)
.put("roles", new JsonArray().add("admin").add("developer").add("user"))
.put("permissions", new JsonArray().add("read").add("write").add("execute"));
final String token = authProvider.generateToken(payload, new JWTOptions().setSubject("UserUnderTest").setAlgorithm("none"));
assertNotNull(token);
JsonObject authInfo = new JsonObject().put("jwt", token);
authProvider.authenticate(authInfo, onSuccess(res -> {
assertNotNull(res);
testComplete();
}));
await();
}
示例3: doFilter
import io.vertx.ext.auth.jwt.JWTAuth; //导入方法依赖的package包/类
@Override
public void doFilter(ApiContext apiContext, Future<ApiContext> completeFuture) {
JWTAuth provider = JWTAuth.create(vertx, jwtAuthOptions);
Result result = apiContext.result();
JsonObject body = result.responseObject();
String jti = UUID.randomUUID().toString();
Object userId = body.getValue(userKey);
if (userId == null) {
Log.create(Filter.LOGGER)
.setEvent("jwt.build.ignore")
.setMessage("Miss userId")
.info();
completeFuture.complete(apiContext);
return;
}
JsonObject claims = new JsonObject()
.put("jti", jti)
.put(userKey, userId);
claimKey.forEach(k -> {
if (body.getValue(k) != null) {
claims.put(k, body.getValue(k));
}
});
String token = provider.generateToken(claims, jwtOptions);
if (emptyingField) {
body.clear().put("token", token);
} else {
body.put("token", token);
}
//保存JTI,后面使用
apiContext.addVariable("jti", jti);
apiContext.setResult(Result.createJsonObject(result.statusCode(), body,
result.headers()));
completeFuture.complete(apiContext);
}
示例4: example52
import io.vertx.ext.auth.jwt.JWTAuth; //导入方法依赖的package包/类
public void example52(Vertx vertx) {
JsonObject authConfig = new JsonObject().put("keyStore", new JsonObject()
.put("type", "jceks")
.put("path", "keystore.jceks")
.put("password", "secret"));
JWTAuth authProvider = JWTAuth.create(vertx, authConfig);
authProvider.generateToken(new JsonObject().put("sub", "paulo").put("someKey", "some value"), new JWTOptions());
}