Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Set-up yuuvis® API system (see minikube setup, for example Installation Guide)
  • A user with at least read permissions on a document type in the system (see tutorial for permissions)
  • Simple Maven project
  • Preconfigured yuuvis® API system with Keycloak as the identity provider
    • Keycloak contains a realm default
    • In the realm default, the Authentication service is registered as a client under the name enaio
    • The realm default contains a user clouduser with the password secret

...

Code Block
titleOkHttpClient with Session Handling and SSL
linenumberstrue
private OkHttpClient client = null;

// necessary to obtain access tokens via SSL
X509TrustManager trustManager = new X509TrustManager()
{
   public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {}
   public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {}
   public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
};


CookieJar cookieJar = new JavaNetCookieJar(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[]{this.trustManager}, new SecureRandom());

// create HTTP Client
this.client = new OkHttpClient.Builder()
    .cookieJar(cookieJar)
    .hostnameVerifier((s, sslSession) -> true)
    .sslSocketFactory(sslContext.getSocketFactory(), this.trustManager)
    .build();

Required System Data

...

Code Block
languagejava
titleLogin with an OAuth 2.0 Access Token
linenumberstrue
String payload = "client_id=enaio&" +
                 "client_secret=4c5254363c1d&" +
                 "username=clouduser&" +
                 "password=secret&" +
                 "grant_type=password";

//retrieve access token from identity provider (Keycloak)
Request.Builder request = new Request.Builder()
	.url(keycloakBaseUrl+"/auth/realms/default/protocol/openid-connect/token")
	.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), payload))
	.build();

String responseJson = this.client.newCall(request).execute().body().string();

DocumentContext context = JsonPath.parse(responseJson);
String tokenType = context.read("token_type");
String accessToken = context.read("access_token");

...