Build a simple Java OAuth client to access Qlik Sense SaaS

1 Introduction

This tutorial shows you how to use the Java OAuth Spring client with Qlik Sense. SaaS.

2 Prerequisites

For this tutorial you will need:

  1. A client Id and Client Secret created from the management console.
  2. A development environment prepared with JDK 11+ and maven 3.6+.

3 Setup

3.1 Spring Dependencies

In your project's pom.xml file, include the following dependencies


3.2 Spring Configuration

In your, define the following configurations. Change the configuration in bold. Make sure the is in your identity provider allowed list of redirect URLs. Change CLIENT_ID, CLIENT_SECRET, your.tenant.domain, and you.project.domain to your own values.

# the name of the OAuth2 provider
# the scopes to use when requesting the authorization
# OAuth2 client Id
# OAuth2 client secret
# the used authentication method
# the used authorization grant type
# the redirect url after successfully logging
# user's information uri
# the field name of the name attribute in the user's information response
# the authorization endpoint uri
# the token endpoint uri

3.3 Spring Web Filter Configuration

Enable the Spring web security component by using @EnableWebFluxSecurity in your configuration class. In your SecurityWebFilterChain configuration bean, make sure to enable oauth2Login(). This requires users to authenticate using OAuth. White list any endpoints that do not require authentication in pathMatchers("/").permitAll(). All other endpoints will be protected by using anyExchange().authenticated(). It is also recommended to provide a custom AuthorizationRequestResolver that uses Proof Key for Code Exchange (PKCE) flow. See the example below:

SecurityWebFilterChain Example
public SecurityWebFilterChain configure(ServerHttpSecurity http) throws Exception {
    return http.authorizeExchange()
        .pathMatchers("/").permitAll() // allows any request to "/"
        .anyExchange().authenticated() // all other requests require authentication
        // it is recommended to add the PKCEAuthorizationRequestResolver to enable PKCE flow
        .authorizationRequestResolver(new PKCEAuthorizationRequestResolver(reactiveClientRegistrationRepository))

3.4 Obtaining the Access Token

Once a user tries to access an authenticated page, Spring will redirect the user to the authorization endpoint configured in step 3.2. Spring will complete the Authorization Code flow and obtain the access token and finally assign the user a new session associated with the token.

3.5 Calling Qlik Sense SaaS APIs using the Access Token

You can create an OAuth web client that can automatically use the authenticated user's access token in making requests to Qlik Sense SaaS.

public WebClient oauth2WebClient(ReactiveClientRegistrationRepository clientRegistrations, ServerOAuth2AuthorizedClientRepository authorizedClients) {

    try {
        HttpClient httpClient = HttpClient.create().followRedirect(true);
        ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients);
        return WebClient.builder().filter(oauth).clientConnector(new ReactorClientHttpConnector(httpClient)).build();
    } catch (SSLException e) {
        throw new RuntimeException("Failed to create oauth2WebClient: " + e.getMessage());

You can now use the created oauth2WebClient to access Qlik Sense SaaS APIs, for example:

String TENANT_ENDPOINT_URL = "https://your.tenant.domain/api/v1/tenants/me"
Mono<Tenant> tenant = oauth2WebClient.get().uri(TENANT_ENDPOINT_URL).retrieve().bodyToMono(Tenant.class);
Was this page helpful?