Authing DocsDocuments
Concept
Guides
Development Integration
Application integration
Concept
Guides
Development Integration
Application integration
Old Version
Development Integration
  • Single Sign-On (SSO)
  • Login component

  • JavaScript/Node.js

  • Java / Kotlin

  • Python

  • C#

  • PHP

  • Go

  • Ruby
  • Android

  • iOS

  • Flutter

  • React Native
  • WeChat Mini Program
  • WeChat webpage authorization
  • Framework Integration
  • Error code
  1. Development Integration
  2. /
  3. Java / Kotlin

¶ Authing - Java / Kotlin

Update Time: 2022-07-04 13:53:25
Edit

Authing Java SDK is comprised of two parts:ManagementClient and AuthenticationClient. All operations in ManagementClient are performed as an administrator, including managing users, managing roles, managing authority policies, and managing user pool configuration. All operations in AuthenticationClient are performed as the current terminal user, including login, registration, modification of user information, and logout.

You should set the initialized ManagementClient instance to a global variable (initialize only once), and the AuthenticationClient should be initialized for each request.

¶ Installation

¶ gradle project

Add to dependencies in build.gradle:

implementation "com.authing:java-core:<LATEST_VERSION>"

You can check the latest version at https://search.maven.org/artifact/cn.authing/java-core (opens new window) .

¶ maven project

Add to dependencies in pom.xml:

If you need to use this SDK in spring, since the spring relies on old version of OkHttp, you need to manually specify the version of OkHttp.

<dependency>
    <groupId>com.authing</groupId>
    <artifactId>java-core</artifactId>
    <version><LATEST_VERSION></version>
</dependency>
<properties>
    <okhttp3.version>4.8.0</okhttp3.version>
</properties>

¶ Use ManagementClient

Initializing ManagementClient requires userPoolId and secret:

You can learn how to get UserPoolId and Secret here .

import com.authing.core.mgmt.ManagementClient;

public class ManagementClientTest {
    public static void main(String[] args){
      ManagementClient managementClient = new ManagementClient("authing_USERPOOL_ID", "authing_USERPOOL_SECRET");

      // get admin privileges
      managementClient.requestToken().execute();
    }
}

Now the ManagementClient instance is ready to use. For example, you can get the list of users in the user pool.

import com.authing.core.mgmt.ManagementClient;

public class ManagementClientTest {
    public static void main(String[] args){
        ManagementClient managementClient = new ManagementClient("authing_USERPOOL_ID", "authing_USERPOOL_SECRET");
        // get admin privileges
        managementClient.requestToken().execute();

        PaginatedUsers users = managementClient.users().list().execute();
    }
}

¶ Use AuthenticationClient

Initializing ManagementClient requires userPoolId and appId:

You can learn how to getUserPoolId here, and view your own application list in the console.

import com.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("authing_USERPOOL_ID");
        authenticationClient.setAppId("authing_APP_ID");
    }
}

Next, you can perform operations such as registration and login:

import com.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("authing_USERPOOL_ID");
        authenticationClient.setAppId("authing_APP_ID");

        String email = "test@example.com";
        String password = "123456";
        User user = authenticationClient.registerByEmail(new RegisterByEmailInput(email, password)).execute();
    }
}

After login, update_profile and the other methods that require users to log in are available:

import com.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("authing_USERPOOL_ID");
        authenticationClient.setAppId("authing_APP_ID");

        String email = "test@example.com";
        String password = "123456";
        authenticationClient.loginByEmail(new LoginByEmailInput(email, password)).execute();

        User user = authenticationClient.updateProfile(new UpdateUserInput().withNickname("nickname")).execute();
    }
}

You can also set the AccessToken parameter after initialization, so that it is unnecessary to call the LoginByXXX method:

import com.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("authing_USERPOOL_ID");
        authenticationClient.setAppId("authing_APP_ID");
        authenticationClient.setAccessToken("ACCESS_TOKEN");
    }
}

Executing the UpdateProfile method can also succeed:

import com.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("authing_USERPOOL_ID");
        authenticationClient.setAccessToken("ACCESS_TOKEN");
        User user = authenticationClient.updateProfile(new UpdateUserInput().withNickname("nickname")).execute();
    }
}

¶ Error handling

import com.authing.core.auth.AuthenticationClient;
import com.authing.core.graphql.GraphQLException;
import java.io.IOException;


public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("authing_USERPOOL_ID");
        authenticationClient.setAccessToken("ACCESS_TOKEN");

        try {
            User user = authenticationClient.updateProfile(new UpdateUserInput().withNickname("nickname")).execute();
        } catch (GraphQLException | IOException e) {
            e.printStackTrace();
        }
    }
}

¶ Privatization deployment

The privatization deployment scenario needs to specify the GraphQL endpoint of your privatized Authing service (without protocol header and Path) and the password encryption public key. If you are not sure, you can contact the Authing IDaaS service administrator.

¶ Use ManagementClient

Initializing ManagementClient requires userPoolId and secret:

import com.authing.core.mgmt.ManagementClient;

public class ManagementClientTest {
    public static void main(String[] args){
      ManagementClient managementClient = new ManagementClient("authing_USERPOOL_ID", "authing_USERPOOL_SECRET");
      // configure customized doimain name
      managementClient.setHost("https://core.you-authing-service.com");
      // configure customized public key
      managementClient.setPublicKey("public key");
      // get admin privileges
      managementClient.requestToken().execute();
    }
}

¶ Use AuthenticationClient

Initializing ManagementClient requires userPoolId and appId:

import com.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("authing_USERPOOL_ID");
        // configure customized doimain name
        authenticationClient.setHost("https://core.you-authing-service.com");
        // configure customized public key
        authenticationClient.setPublicKey("public key");
    }
}

¶ Interface index

Available Authentication methods

  • Get the user profile of the current user: getCurrentUser
  • Register with email: registerByEmail
  • Register with username: registerByUsername
  • Register with SMS verification code: registerByPhoneCode
  • Login with email: loginByEmail
  • Login with username: loginByUsername
  • Login with SMS verification code: loginByPhoneCode
  • Login with phone number password: loginByPhonePassword
  • Send mail: sendEmail
  • Send SMS verification code: sendSmsCode
  • Check the valid status of the token: checkLoginStatus
  • Use the SMS verification code to reset the password: resetPasswordByPhoneCode
  • Use email verification code to reset password: resetPasswordByEmailCode
  • Update user profile: updateProfile
  • Update password: updatePassword
  • Update phone number: updatePhone
  • Update email: updateEmail
  • Refresh token:refreshToken
  • Bind mobile phone number: bindPhone
  • Unbind phone number: unbindPhone

Learn more::

AuthenticationClient

ManagementClient contains the following sub-clients:

UsersManagementClientRolesManagementClientPoliciesManagementClientAclManagementClientUdfManagementClienGroupsManagementClientOrgManagementClientUserPoolManagementClientWhitelistManagementClientApplicationManagementClient

¶ Get help

Join us on forum: #authing-chat (opens new window)

Prev: Management subject certification Next: User Authentication Module
  • Installation
  • Use ManagementClient
  • Use AuthenticationClient
  • Error handling
  • Privatization deployment
  • Interface index
  • Get help

User identity management

Integrated third-party login
Mobile phone number flash check (opens new window)
Universal login form component
Custom authentication process

Enterprise internal management

Single Sign On
Multi-factor Authentication
Authority Management

Developers

Development Document
Framework Integration
Blog (opens new window)
GitHub (opens new window)
Community User Center (opens new window)

Company

400 888 2106
sales@authing.cn
16 / F, Block B, NORTH STAR CENTURY CENTER, Beijing(Total)
room 406, 4th floor, zone B, building 1, No. 200, Tianfu Fifth Street, Chengdu(branch)

Beijing ICP No.19051205-1

© Beijing Steamory Technology Co.