Java源码示例:org.keycloak.admin.client.KeycloakBuilder
示例1
@Override
public Map<String, String> start() {
RealmRepresentation realm = createRealm(KEYCLOAK_REALM);
realm.getClients().add(createClient("quarkus-app"));
realm.getUsers().add(createUser("alice", "user"));
realm.getUsers().add(createUser("admin", "user", "admin"));
realm.getUsers().add(createUser("jdoe", "user", "confidential"));
keycloak = KeycloakBuilder.builder()
.serverUrl(KEYCLOAK_SERVER_URL)
.realm("master")
.clientId("admin-cli")
.username("admin")
.password("admin")
.build();
keycloak.realms().create(realm);
return Collections.emptyMap();
}
示例2
@Override
public Map<String, String> start() {
RealmRepresentation realm = createRealm(KEYCLOAK_REALM);
realm.getClients().add(createClient("quarkus-app"));
realm.getUsers().add(createUser("alice", "user"));
realm.getUsers().add(createUser("admin", "user", "admin"));
realm.getUsers().add(createUser("jdoe", "user", "confidential"));
keycloak = KeycloakBuilder.builder()
.serverUrl(KEYCLOAK_SERVER_URL)
.realm("master")
.clientId("admin-cli")
.username("admin")
.password("admin")
.build();
keycloak.realms().create(realm);
return Collections.emptyMap();
}
示例3
private static Keycloak createKeycloakClient() {
return KeycloakBuilder.builder()
.serverUrl("http://localhost:11080/auth")
.realm("master")
.username("admin")
.password("password")
.clientId("admin-cli")
.build();
}
示例4
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
LOG.info("Hello invoked");
Keycloak keycloak = KeycloakBuilder.builder().serverUrl(url)
.realm("master")
.clientId("admin-cli")
.username("admin")
.password("admin").build();
return keycloak.realm("quarkus").toRepresentation().getRealm();
}
示例5
/**
* Method to initializate the Keycloak connection
*
* @return Keycloak connection
*/
public static Keycloak initialiseConnection() throws Exception {
ProjectLogger.log("key cloak instance is creation started.");
keycloak = initialiseEnvConnection();
if (keycloak != null) {
return keycloak;
}
KeycloakBuilder keycloakBuilder =
KeycloakBuilder.builder()
.serverUrl(cache.getProperty(JsonKey.SSO_URL))
.realm(cache.getProperty(JsonKey.SSO_REALM))
.username(cache.getProperty(JsonKey.SSO_USERNAME))
.password(cache.getProperty(JsonKey.SSO_PASSWORD))
.clientId(cache.getProperty(JsonKey.SSO_CLIENT_ID))
.resteasyClient(
new ResteasyClientBuilder()
.connectionPoolSize(Integer.parseInt(cache.getProperty(JsonKey.SSO_POOL_SIZE)))
.build());
if (cache.getProperty(JsonKey.SSO_CLIENT_SECRET) != null
&& !(cache.getProperty(JsonKey.SSO_CLIENT_SECRET).equals(JsonKey.SSO_CLIENT_SECRET))) {
keycloakBuilder.clientSecret(cache.getProperty(JsonKey.SSO_CLIENT_SECRET));
}
SSO_URL = cache.getProperty(JsonKey.SSO_URL);
SSO_REALM = cache.getProperty(JsonKey.SSO_REALM);
CLIENT_ID = cache.getProperty(JsonKey.SSO_CLIENT_ID);
keycloak = keycloakBuilder.build();
ProjectLogger.log("key cloak instance is created successfully.");
return keycloak;
}
示例6
public static Keycloak createAdminClient(boolean ignoreUnknownProperties, String authServerContextRoot, String realmName, String username, String password, String clientId, String clientSecret) throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException {
ResteasyClientBuilder resteasyClientBuilder = new ResteasyClientBuilder();
if ("true".equals(System.getProperty("auth.server.ssl.required"))) {
File trustore = new File(PROJECT_BUILD_DIRECTORY, "dependency/keystore/keycloak.truststore");
resteasyClientBuilder.sslContext(getSSLContextWithTrustore(trustore, "secret"));
System.setProperty("javax.net.ssl.trustStore", trustore.getAbsolutePath());
}
// We need to ignore unknown JSON properties e.g. in the adapter configuration representation
// during adapter backward compatibility testing
if (ignoreUnknownProperties) {
// We need to use anonymous class to avoid the following error from RESTEasy:
// Provider class org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider is already registered. 2nd registration is being ignored.
ResteasyJackson2Provider jacksonProvider = new ResteasyJackson2Provider() {};
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jacksonProvider.setMapper(objectMapper);
resteasyClientBuilder.register(jacksonProvider, 100);
}
resteasyClientBuilder
.hostnameVerification(ResteasyClientBuilder.HostnameVerificationPolicy.WILDCARD)
.connectionPoolSize(10)
.httpEngine(getCustomClientHttpEngine(resteasyClientBuilder, 1));
return KeycloakBuilder.builder()
.serverUrl(authServerContextRoot + "/auth")
.realm(realmName)
.username(username)
.password(password)
.clientId(clientId)
.clientSecret(clientSecret)
.resteasyClient(resteasyClientBuilder.build()).build();
}
示例7
Keycloak createAdminClient(String realm, String clientId, String username, String password, ResteasyClient resteasyClient) {
if (password == null) {
password = username.equals("admin") ? "admin" : "password";
}
return KeycloakBuilder.builder().serverUrl(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth")
.realm(realm)
.username(username)
.password(password)
.clientId(clientId)
.resteasyClient(resteasyClient)
.build();
}
示例8
/**
* This method will provide the keycloak connection from environment variable. if environment
* variable is not set then it will return null.
*
* @return Keycloak
*/
private static Keycloak initialiseEnvConnection() throws Exception {
String url = System.getenv(JsonKey.SUNBIRD_SSO_URL);
String username = System.getenv(JsonKey.SUNBIRD_SSO_USERNAME);
String password = System.getenv(JsonKey.SUNBIRD_SSO_PASSWORD);
String cleintId = System.getenv(JsonKey.SUNBIRD_SSO_CLIENT_ID);
String clientSecret = System.getenv(JsonKey.SUNBIRD_SSO_CLIENT_SECRET);
String relam = System.getenv(JsonKey.SUNBIRD_SSO_RELAM);
if (StringUtils.isBlank(url)
|| StringUtils.isBlank(username)
|| StringUtils.isBlank(password)
|| StringUtils.isBlank(cleintId)
|| StringUtils.isBlank(relam)) {
ProjectLogger.log(
"key cloak connection is not provided by Environment variable.", LoggerEnum.INFO.name());
return null;
}
SSO_URL = url;
ProjectLogger.log("SSO url is==" + SSO_URL, LoggerEnum.INFO.name());
SSO_REALM = relam;
CLIENT_ID = cleintId;
KeycloakBuilder keycloakBuilder =
KeycloakBuilder.builder()
.serverUrl(url)
.realm(relam)
.username(username)
.password(password)
.clientId(cleintId)
.resteasyClient(
new ResteasyClientBuilder()
.connectionPoolSize(Integer.parseInt(cache.getProperty(JsonKey.SSO_POOL_SIZE)))
.build());
if (StringUtils.isNotBlank(clientSecret)) {
keycloakBuilder.clientSecret(clientSecret);
ProjectLogger.log(
"KeyCloakConnectionProvider:initialiseEnvConnection client sceret is provided.",
LoggerEnum.INFO.name());
}
keycloakBuilder.grantType("client_credentials");
keycloak = keycloakBuilder.build();
ProjectLogger.log(
"key cloak instance is created from Environment variable settings .",
LoggerEnum.INFO.name());
return keycloak;
}