本文整理汇总了Java中io.vertx.ext.auth.jwt.JWTAuth类的典型用法代码示例。如果您正苦于以下问题:Java JWTAuth类的具体用法?Java JWTAuth怎么用?Java JWTAuth使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JWTAuth类属于io.vertx.ext.auth.jwt包,在下文中一共展示了JWTAuth类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: auth
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的package包/类
private Future<JsonObject> auth(String token) {
Future<JsonObject> authFuture = Future.future();
JWTAuth provider = JWTAuth.create(vertx, jwtAuthOptions);
provider.authenticate(new JsonObject().put("jwt", token), ar -> {
if (ar.succeeded()) {
JsonObject principal = ar.result().principal();
if (principal.containsKey(userKey)) {
authFuture.complete(principal);
} else {
SystemException systemException = SystemException.create(DefaultErrorCode.INVALID_TOKEN)
.set("details", "no " + userKey);
authFuture.fail(systemException);
}
} else {
fail(authFuture, ar);
}
});
return authFuture;
}
示例2: MeshAuthProvider
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的package包/类
@Inject
public MeshAuthProvider(BCryptPasswordEncoder passwordEncoder, Database database, BootstrapInitializer boot) {
this.passwordEncoder = passwordEncoder;
this.db = database;
this.boot = boot;
// Use the mesh JWT options in order to setup the JWTAuth provider
AuthenticationOptions options = Mesh.mesh().getOptions().getAuthenticationOptions();
String keystorePassword = options.getKeystorePassword();
if (keystorePassword == null) {
throw new RuntimeException("The keystore password could not be found within the authentication options.");
}
String keyStorePath = options.getKeystorePath();
String type = "jceks";
JsonObject config = new JsonObject().put("keyStore",
new JsonObject().put("path", keyStorePath).put("type", type).put("password", keystorePassword));
jwtProvider = JWTAuth.create(Mesh.vertx(), config);
}
示例3: createJWTAuth
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的package包/类
private JWTAuth createJWTAuth() {
// Set default config
JsonObject defaultConfig = new JsonObject().put("keyStore",
new JsonObject()
.put("path", "keystore.jceks")
.put("type", "jceks")
.put("password", "secret")
);
// Merge with config jwtAuth attribute
JsonObject config = vertx.getOrCreateContext().config().getJsonObject("jwtAuth", defaultConfig);
// Prepare JWT Auth
return JWTAuth.create(vertx, config);
}
示例4: handle
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的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);
}
});
}
示例5: handle
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的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);
}
});
}
示例6: example50
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的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);
}
});
}
示例7: example51
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的package包/类
public void example51(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("/protected/*").handler(JWTAuthHandler.create(authProvider));
router.route("/protected/somepage").handler(ctx -> {
// some handle code...
});
}
示例8: setUp
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的package包/类
@Override
public void setUp() throws Exception {
super.setUp();
OKService service = new OKServiceImpl();
ServiceBinder serviceBinder = new ServiceBinder(vertx)
.setAddress(SERVICE_ADDRESS)
.addInterceptor(new ServiceJWTInterceptor().setJwtAuth(JWTAuth.create(vertx, new JWTAuthOptions()
.addSecret(new SecretOptions()
.setType("HS256")
.setSecret("notasecret")))));
consumer = serviceBinder.register(OKService.class, service);
serviceProxyBuilder = new ServiceProxyBuilder(vertx)
.setAddress(SERVICE_ADDRESS);
}
示例9: 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>
}
}
示例10: example10
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的package包/类
public void example10(JWTAuth jwtAuth) {
// This string is what you see after the string "Bearer" in the
// HTTP Authorization header
// In this case we are forcing the provider to ignore the `exp` field
jwtAuth.authenticate(new JsonObject()
.put("jwt", "BASE64-ENCODED-STRING")
.put("options", new JsonObject()
.put("ignoreExpiration", true)), res -> {
if (res.succeeded()) {
User theUser = res.result();
} else {
// Failed!
}
});
}
示例11: example11
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的package包/类
public void example11(JWTAuth jwtAuth) {
// This string is what you see after the string "Bearer" in the
// HTTP Authorization header
// In this case we are forcing the provider to ignore the `exp` field
jwtAuth.authenticate(new JsonObject()
.put("jwt", "BASE64-ENCODED-STRING")
.put("options", new JsonObject()
.put("audience", new JsonArray().add("[email protected]"))), res -> {
if (res.succeeded()) {
User theUser = res.result();
} else {
// Failed!
}
});
}
示例12: example12
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的package包/类
public void example12(JWTAuth jwtAuth) {
// This string is what you see after the string "Bearer" in the
// HTTP Authorization header
// In this case we are forcing the provider to ignore the `exp` field
jwtAuth.authenticate(new JsonObject()
.put("jwt", "BASE64-ENCODED-STRING")
.put("options", new JsonObject()
.put("issuer", "mycorp.com")), res -> {
if (res.succeeded()) {
User theUser = res.result();
} else {
// Failed!
}
});
}
示例13: testBadIssuer
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的package包/类
@Test
public void testBadIssuer() {
authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().setIssuer("https://vertx.io")));
JsonObject payload = new JsonObject().put("sub", "Paulo");
final String token = authProvider.generateToken(payload, new JWTOptions().setIssuer("https://auth0.io"));
assertNotNull(token);
JsonObject authInfo = new JsonObject()
.put("jwt", token);
authProvider.authenticate(authInfo, onFailure(thr -> {
assertNotNull(thr);
testComplete();
}));
await();
}
示例14: testBadAudience
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的package包/类
@Test
public void testBadAudience() {
authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(
new JWTOptions()
.addAudience("e")
.addAudience("d")));
JsonObject payload = new JsonObject()
.put("sub", "Paulo");
final String token = authProvider.generateToken(payload,
new JWTOptions().addAudience("a").addAudience("b").addAudience("c"));
assertNotNull(token);
JsonObject authInfo = new JsonObject()
.put("jwt", token);
authProvider.authenticate(authInfo, onFailure(thr -> {
assertNotNull(thr);
testComplete();
}));
await();
}
示例15: testGenerateNewTokenES256
import io.vertx.ext.auth.jwt.JWTAuth; //导入依赖的package包/类
@Test
public void testGenerateNewTokenES256() {
authProvider = JWTAuth.create(vertx, new JWTAuthOptions()
.setKeyStore(new KeyStoreOptions()
.setPath("es256-keystore.jceks")
.setPassword("secret")));
String token = authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions().setAlgorithm("ES256"));
assertNotNull(token);
JsonObject authInfo = new JsonObject()
.put("jwt", token);
authProvider.authenticate(authInfo, res -> {
if (res.failed()) {
res.cause().printStackTrace();
fail();
}
assertNotNull(res.result());
testComplete();
});
await();
}