本文整理汇总了Java中io.vertx.ext.auth.jwt.JWTOptions类的典型用法代码示例。如果您正苦于以下问题:Java JWTOptions类的具体用法?Java JWTOptions怎么用?Java JWTOptions使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JWTOptions类属于io.vertx.ext.auth.jwt包,在下文中一共展示了JWTOptions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: jwtExpiredShouldThrowExpiredToken
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
@Test
public void jwtExpiredShouldThrowExpiredToken(TestContext testContext) {
JsonObject claims = new JsonObject()
.put("userId", userId);
String token =
provider.generateToken(claims, new JWTOptions().setExpiresInSeconds(-1000l * 30));
ApiContext apiContext = createApiContext(ImmutableMultimap.of("Authorization",
"Bearer " + token),
ArrayListMultimap.create());
Filter filter = Filter.create(AuthenticationFilter.class.getSimpleName(),
vertx, new JsonObject());
filters.add(filter);
Task<ApiContext> task = Task.create();
task.complete(apiContext);
Async async = testContext.async();
Filters.doFilter(task, filters)
.andThen(context -> testContext.fail())
.onFailure(throwable -> {
testContext.assertTrue(throwable instanceof SystemException);
SystemException ex = (SystemException) throwable;
testContext.assertEquals(DefaultErrorCode.EXPIRE_TOKEN, ex.getErrorCode());
async.complete();
});
}
示例2: generateToken
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
/**
* Authenticates the user and returns a JWToken if successful.
*
* @param username
* Username
* @param password
* Password
* @param resultHandler
* Handler to be invoked with the created JWToken
*/
public void generateToken(String username, String password, Handler<AsyncResult<String>> resultHandler) {
authenticate(username, password, rh -> {
if (rh.failed()) {
resultHandler.handle(Future.failedFuture(rh.cause()));
} else {
User user = rh.result().getUser();
String uuid = null;
if (user instanceof MeshAuthUser) {
uuid = ((MeshAuthUser) user).getUuid();
} else {
uuid = user.principal().getString("uuid");
}
JsonObject tokenData = new JsonObject().put(USERID_FIELD_NAME, uuid);
resultHandler.handle(Future.succeededFuture(jwtProvider.generateToken(tokenData,
new JWTOptions().setExpiresInSeconds(Mesh.mesh().getOptions().getAuthenticationOptions().getTokenExpirationTime()))));
}
});
}
示例3: handle
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
public void handle(RoutingContext context) {
JsonObject authInfo = new JsonObject()
.put("username", context.request().getParam("username"))
.put("password", context.request().getParam("password"));
authProvider.authenticate(authInfo, res -> {
if (res.succeeded()) {
JWTAuth jwt = JWTAuth.create(context.vertx(), new JsonObject().put("keyStore", new JsonObject()
.put("type", "jceks")
.put("path", "keystore.jceks")
.put("password", "secret")));
String token = jwt.generateToken(
new JsonObject().put("sub", context.request().getParam("username")),
new JWTOptions().setExpiresInMinutes(60)
);
context.setUser(res.result());
context.session().put("token", token);
context.response()
.putHeader("location", (String) context.session().remove("return_url"))
.setStatusCode(302)
.end();
} else {
context.fail(401);
}
});
}
示例4: handle
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
public void handle(RoutingContext context) {
JsonObject authInfo = new JsonObject()
.put("username", context.request().getParam("username"))
.put("password", context.request().getParam("password"));
authProvider.authenticate(authInfo, res -> {
if (res.succeeded()) {
JWTAuth jwt = JWTAuth.create(context.vertx(), new JsonObject().put("keyStore", new JsonObject()
.put("type", "jceks")
.put("path", "keystore.jceks")
.put("password", "secret")));
String token = jwt.generateToken(
new JsonObject().put("sub", context.request().getParam("username")),
new JWTOptions().setExpiresInMinutes(60L)
);
context.setUser(res.result());
context.session().put("token", token);
context.response()
.putHeader("location", (String) context.session().remove("return_url"))
.setStatusCode(302)
.end();
} else {
context.fail(401);
}
});
}
示例5: example50
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
public void example50(Vertx vertx) {
Router router = Router.router(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);
router.route("/login").handler(ctx -> {
// this is an example, authentication should be done with another provider...
if ("paulo".equals(ctx.request().getParam("username")) && "secret".equals(ctx.request().getParam("password"))) {
ctx.response().end(authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions()));
} else {
ctx.fail(401);
}
});
}
示例6: testLogin
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
@Test
public void testLogin() throws Exception {
Handler<RoutingContext> handler = rc -> {
assertNotNull(rc.user());
assertEquals("paulo", rc.user().principal().getString("sub"));
rc.response().end("Welcome to the protected resource!");
};
router.route("/protected/*").handler(JWTAuthHandler.create(authProvider));
router.route("/protected/somepage").handler(handler);
testRequest(HttpMethod.GET, "/protected/somepage", null, resp -> {
}, 401, "Unauthorized", null);
// Now try again with credentials
testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Bearer " + authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions())), 200, "OK", "Welcome to the protected resource!");
}
示例7: example7
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的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>
}
}
示例8: JwtAuth
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
private JwtAuth(final Vertx vertx,
final String keystorePath,
final String keystorePass,
final List<String> audience) {
this.jwtAuth = JWTAuth.create(vertx, new JsonObject().put("keyStore", new JsonObject()
.put("path", keystorePath)
.put("type", "jceks")
.put("password", keystorePass)));
this.jwtOptions = new JWTOptions()
.setAudience(audience)
.setAlgorithm("RS256")
.setExpiresInSeconds(Duration.of(365, DAYS).getSeconds());
this.audience = audience;
}
示例9: handleAuthentication
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
private void handleAuthentication(AsyncResult<JsonObject> resultHandler) {
if (resultHandler.failed()) {
unauthorized(resultHandler.cause());
return;
}
JsonObject jwtOptions = vertx.getOrCreateContext().config().getJsonObject("jwtOptions", new JsonObject().put("expiresInSeconds", 60));
JWTOptions options = new JWTOptions().setExpiresInSeconds(jwtOptions.getLong("expiresInSeconds"));
JsonObject user = resultHandler.result();
String jwt = jwtAuth.getJWTAuth().generateToken(user, options);
success(new JsonObject().put("jwt", jwt));
}
示例10: JwtBuildFilter
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
/**
* @param vertx Vertx
* @param config 配置
*/
JwtBuildFilter(Vertx vertx, JsonObject config) {
this.vertx = vertx;
if (config.getValue("jwt.builder") instanceof JsonObject) {
JsonObject jwtBuilderConfig = config.getJsonObject("jwt.builder");
this.jwtOptions = new JWTOptions(jwtBuilderConfig);
if (jwtBuilderConfig.getValue("claimKey") instanceof JsonArray) {
jwtBuilderConfig.getJsonArray("claimKey").forEach(item -> {
if (item instanceof String) { claimKey.add((String) item); }
});
}
if (jwtBuilderConfig.getValue("emptyingField") instanceof Boolean) {
emptyingField = jwtBuilderConfig.getBoolean("emptyingField");
}
} else {
this.jwtOptions = new JWTOptions();
}
//jwt
this.jwtAuthOptions = new JWTAuthOptions();
if (config.getValue("keyStore") instanceof JsonObject) {
this.jwtAuthOptions
.setKeyStore(new KeyStoreOptions(config.getJsonObject("keyStore")));
} else {
KeyStoreOptions keyStoreOptions = new KeyStoreOptions()
.setPath("keystore.jceks")
.setType("jceks")
.setPassword("INIHPMOZPO");
this.jwtAuthOptions.setKeyStore(keyStoreOptions);
}
}
示例11: ignoreExpShouldSuccess
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
@Test
public void ignoreExpShouldSuccess(TestContext testContext) {
JsonObject claims = new JsonObject()
.put("userId", userId)
.put("jti", jti);
String token =
provider.generateToken(claims, new JWTOptions().setExpiresInSeconds(-1000l * 30));
ApiContext apiContext = createApiContext(ImmutableMultimap.of("Authorization",
"Bearer " + token),
ArrayListMultimap.create());
JsonObject jwtConfig = new JsonObject()
.put("ignoreExpiration", true);
Filter filter = Filter.create(AuthenticationFilter.class.getSimpleName(),
vertx, new JsonObject()
.put("jwt.auth", jwtConfig));
filters.add(filter);
Task<ApiContext> task = Task.create();
task.complete(apiContext);
Async async = testContext.async();
Filters.doFilter(task, filters)
.andThen(context -> {
JsonObject principal = context.principal();
System.out.println(principal);
testContext.assertEquals(userId, principal.getInteger("userId"));
testContext.assertEquals(jti, principal.getString("jti"));
async.complete();
})
.onFailure(throwable -> {
testContext.fail();
});
}
示例12: leewayShouldSuccess
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
@Test
public void leewayShouldSuccess(TestContext testContext) {
JsonObject claims = new JsonObject()
.put("userId", userId)
.put("jti", jti);
String token =
provider.generateToken(claims, new JWTOptions().setExpiresInSeconds(-30l));
ApiContext apiContext = createApiContext(ImmutableMultimap.of("Authorization",
"Bearer " + token),
ArrayListMultimap.create());
JsonObject jwtConfig = new JsonObject()
.put("leeway", 50);
Filter filter = Filter.create(AuthenticationFilter.class.getSimpleName(),
vertx, new JsonObject()
.put("jwt.auth", jwtConfig));
filters.add(filter);
Task<ApiContext> task = Task.create();
task.complete(apiContext);
Async async = testContext.async();
Filters.doFilter(task, filters)
.andThen(context -> {
JsonObject principal = context.principal();
System.out.println(principal);
testContext.assertEquals(userId, principal.getInteger("userId"));
testContext.assertEquals(jti, principal.getString("jti"));
async.complete();
})
.onFailure(throwable -> {
testContext.fail();
});
}
示例13: leewayThrowExpiredToken
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
@Test
public void leewayThrowExpiredToken(TestContext testContext) {
JsonObject claims = new JsonObject()
.put("userId", userId);
String token =
provider.generateToken(claims, new JWTOptions().setExpiresInSeconds(-30l));
ApiContext apiContext = createApiContext(ImmutableMultimap.of("Authorization",
"Bearer " + token),
ArrayListMultimap.create());
JsonObject jwtConfig = new JsonObject()
.put("leeway", 10);
Filter filter = Filter.create(AuthenticationFilter.class.getSimpleName(),
vertx, new JsonObject()
.put("jwt.auth",jwtConfig));
filters.add(filter);
Task<ApiContext> task = Task.create();
task.complete(apiContext);
Async async = testContext.async();
Filters.doFilter(task, filters)
.andThen(context -> testContext.fail())
.onFailure(throwable -> {
testContext.assertTrue(throwable instanceof SystemException);
SystemException ex = (SystemException) throwable;
testContext.assertEquals(DefaultErrorCode.EXPIRE_TOKEN, ex.getErrorCode());
async.complete();
});
}
示例14: testAuthSuccess
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
@Test
public void testAuthSuccess(TestContext testContext) {
JsonObject claims = new JsonObject()
.put("userId", userId)
.put("jti", jti);
// .put("exp", System.currentTimeMillis() / 1000 + 1000 * 30);
String token =
provider.generateToken(claims, new JWTOptions());
ApiContext apiContext = createApiContext(ImmutableMultimap.of("Authorization",
"Bearer " + token),
ArrayListMultimap.create());
Filter filter = Filter.create(AuthenticationFilter.class.getSimpleName(),
vertx, new JsonObject()
.put("jwt",
new JsonObject().put("expiresInSeconds", 60 * 30))
.put("user", new JsonObject())
.put("namespace", namespace));
filters.add(filter);
Task<ApiContext> task = Task.create();
task.complete(apiContext);
Async async = testContext.async();
Filters.doFilter(task, filters)
.andThen(context -> {
JsonObject principal = context.principal();
System.out.println(principal);
testContext.assertEquals(userId, principal.getInteger("userId"));
testContext.assertEquals(jti, principal.getString("jti"));
testContext.assertFalse(principal.containsKey("username"));
async.complete();
})
.onFailure(throwable -> {
testContext.fail();
});
}
示例15: missUserIdShouldThrownInvalidToken
import io.vertx.ext.auth.jwt.JWTOptions; //导入依赖的package包/类
@Test
public void missUserIdShouldThrownInvalidToken(TestContext testContext) {
JsonObject claims = new JsonObject()
.put(UUID.randomUUID().toString(), userId)
.put("jti", jti);
// .put("exp", System.currentTimeMillis() / 1000 + 1000 * 30);
String token =
provider.generateToken(claims, new JWTOptions());
ApiContext apiContext = createApiContext(ImmutableMultimap.of("Authorization",
"Bearer " + token),
ArrayListMultimap.create());
Filter filter = Filter.create(AuthenticationFilter.class.getSimpleName(),
vertx, new JsonObject());
filters.add(filter);
Task<ApiContext> task = Task.create();
task.complete(apiContext);
Async async = testContext.async();
Filters.doFilter(task, filters)
.andThen(context -> testContext.fail())
.onFailure(throwable -> {
throwable.printStackTrace();
testContext.assertTrue(throwable instanceof SystemException);
SystemException ex = (SystemException) throwable;
testContext.assertEquals(DefaultErrorCode.INVALID_TOKEN, ex.getErrorCode());
async.complete();
});
}