本文整理汇总了Java中io.vertx.ext.auth.oauth2.providers.KeycloakAuth.create方法的典型用法代码示例。如果您正苦于以下问题:Java KeycloakAuth.create方法的具体用法?Java KeycloakAuth.create怎么用?Java KeycloakAuth.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.vertx.ext.auth.oauth2.providers.KeycloakAuth
的用法示例。
在下文中一共展示了KeycloakAuth.create方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testLoadJWK2
import io.vertx.ext.auth.oauth2.providers.KeycloakAuth; //导入方法依赖的package包/类
@Test
@Ignore
public void testLoadJWK2() {
JsonObject config = new JsonObject("{\n" +
" \"realm\": \"master\",\n" +
" \"auth-server-url\": \"http://localhost:8080/auth\",\n" +
" \"ssl-required\": \"external\",\n" +
" \"resource\": \"test\",\n" +
" \"credentials\": {\n" +
" \"secret\": \"b0568625-a482-45d8-af8b-27beba502ed3\"\n" +
" }\n" +
"}");
OAuth2Auth oauth2 = KeycloakAuth.create(vertx, config);
oauth2.loadJWK(load -> {
assertFalse(load.failed());
testComplete();
});
await();
}
示例2: example13
import io.vertx.ext.auth.oauth2.providers.KeycloakAuth; //导入方法依赖的package包/类
public void example13(Vertx vertx) {
// you would get this config from the keycloak admin console
JsonObject keycloakJson = new JsonObject()
.put("realm", "master")
.put("realm-public-key", "MIIBIjANBgkqhk...wIDAQAB")
.put("auth-server-url", "http://localhost:9000/auth")
.put("ssl-required", "external")
.put("resource", "frontend")
.put("credentials", new JsonObject()
.put("secret", "2fbf5e18-b923-4a83-9657-b4ebd5317f60"));
// Initialize the OAuth2 Library
OAuth2Auth oauth2 = KeycloakAuth.create(vertx, OAuth2FlowType.PASSWORD, keycloakJson);
// first get a token (authenticate)
oauth2.authenticate(new JsonObject().put("username", "user").put("password", "secret"), res -> {
if (res.failed()) {
// error handling...
} else {
AccessToken token = (AccessToken) res.result();
// now check for permissions
token.isAuthorised("account:manage-account", r -> {
if (r.result()) {
// this user is authorized to manage its account
}
});
}
});
}
示例3: loadUser
import io.vertx.ext.auth.oauth2.providers.KeycloakAuth; //导入方法依赖的package包/类
@Test
public void loadUser() {
OAuth2Auth provider = KeycloakAuth.create(Vertx.vertx(), OAuth2FlowType.AUTH_CODE, keycloakConfig);
OAuth2TokenImpl user = new OAuth2TokenImpl();
System.out.println(keycloakToken.length());
user.readFromBuffer(0, Buffer.buffer().appendInt(0).appendInt(keycloakToken.length()).appendString(keycloakToken));
user.setAuthProvider(provider);
}
示例4: keycloakTest
import io.vertx.ext.auth.oauth2.providers.KeycloakAuth; //导入方法依赖的package包/类
@Test
public void keycloakTest() throws Exception {
super.setUp();
oauth2 = KeycloakAuth.create(vertx, OAuth2FlowType.AUTH_CODE, keycloakConfig);
OAuth2TokenImpl token = new OAuth2TokenImpl((OAuth2AuthProviderImpl) oauth2, keycloakToken);
assertNotNull(token.opaqueAccessToken());
assertNotNull(token.opaqueRefreshToken());
assertNull(token.accessToken());
// trust it
token.setTrustJWT(true);
assertNotNull(token.accessToken());
}
示例5: authenticate
import io.vertx.ext.auth.oauth2.providers.KeycloakAuth; //导入方法依赖的package包/类
@Override
public Authenticator authenticate(Vertx vertx, Map<String, String> config, MultiMap headerMap, Handler<AsyncResult<Void>> resultHandler) {
OAuth2FlowType flowType = getFlowType(config.get("flowType"));
JsonObject params = new JsonObject();
if (config.get("username") != null) {
params.put("username", config.get("username"));
}
if (config.get("password") != null) {
params.put("password", config.get("password"));
}
OAuth2Auth oauth2 = KeycloakAuth.create(vertx, flowType, mapToJson(config));
oauth2.getToken(params, tokenResult -> {
if (tokenResult.succeeded()) {
log.debug("OAuth2 Keycloak exchange succeeded.");
AccessToken token = tokenResult.result();
headerMap.set("Authorization", "Bearer " + token.principal().getString("access_token"));
resultHandler.handle(Future.succeededFuture());
} else {
log.error("Access Token Error: {0}.", tokenResult.cause().getMessage());
resultHandler.handle(Future.failedFuture(tokenResult.cause()));
}
});
return this;
}
示例6: start
import io.vertx.ext.auth.oauth2.providers.KeycloakAuth; //导入方法依赖的package包/类
@Override
public void start(Future<Void> future) throws Exception {
super.start();
// get HTTP host and port from configuration, or use default value
String host = config().getString("api.gateway.http.address", "localhost");
int port = config().getInteger("api.gateway.http.port", DEFAULT_PORT);
Router router = Router.router(vertx);
// cookie and session handler
enableLocalSession(router);
// body handler
router.route().handler(BodyHandler.create());
// version handler
router.get("/api/v").handler(this::apiVersion);
// create OAuth 2 instance for Keycloak
oauth2 = KeycloakAuth.create(vertx, OAuth2FlowType.AUTH_CODE, config());
router.route().handler(UserSessionHandler.create(oauth2));
String hostURI = buildHostURI();
// set auth callback handler
router.route("/callback").handler(context -> authCallback(oauth2, hostURI, context));
router.get("/uaa").handler(this::authUaaHandler);
router.get("/login").handler(this::loginEntryHandler);
router.post("/logout").handler(this::logoutHandler);
// api dispatcher
router.route("/api/*").handler(this::dispatchRequests);
// static content
router.route("/*").handler(StaticHandler.create());
// enable HTTPS
HttpServerOptions httpServerOptions = new HttpServerOptions()
.setSsl(true)
.setKeyStoreOptions(new JksOptions().setPath("server.jks").setPassword("123456"));
// create http server
vertx.createHttpServer(httpServerOptions)
.requestHandler(router::accept)
.listen(port, host, ar -> {
if (ar.succeeded()) {
publishApiGateway(host, port);
future.complete();
logger.info("API Gateway is running on port " + port);
// publish log
publishGatewayLog("api_gateway_init_success:" + port);
} else {
future.fail(ar.cause());
}
});
}