Authing 文档
文档
概念
使用指南
开发集成
应用集成
旧版
概念
使用指南
开发集成
应用集成
旧版
开发集成
  • 登录组件
  • 单点登录(SSO)
  • JavaScript/Node.js
  • Java / Kotlin
  • Python
  • C#
  • PHP
  • Go
  • Ruby
  • Swift
  • Android
  • React Native
  • 微信小程序
  • 微信网页授权
  • 框架集成
  • 错误代码
  1. 开发集成
  2. /
  3. Java / Kotlin

¶ Authing - Java / Kotlin

Authing Java SDK 由两部分组成:ManagementClient 和 AuthenticationClient。ManagementClient 中进行的所有操作均以管理员的身份进行,包含管理用户、管理角色、管理权限策略、管理用户池配置等模块。AuthenticationClient 中的所有操作以当前终端用户的身份进行,包含登录、注册、修改用户资料、退出登录等方法。

你应该将初始化过后的 ManagementClient 实例设置为一个全局变量(只初始化一次),而 AuthenticationClient 应该每次请求初始化一个。

¶ 安装

¶ gradle 项目

在 build.gradle 内的 dependencies 中添加:

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

你可以在 https://search.maven.org/artifact/cn.authing/java-core (opens new window) 查看最新的版本。

¶ maven 项目

在 pom.xml 内的 dependencies 中添加:

如果你需要在 spring 中使用此 SDK,由于 spring 依赖的 OkHttp 版本过低,所以你需要手动指定一下 OkHttp 的版本。

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

¶ 使用管理模块

初始化 ManagementClient 需要 userPoolId(用户池 ID) 和 secret(用户池密钥):

你可以在此了解如何获取 UserPoolId 和 Secret .

import cn.authing.core.mgmt.ManagementClient;

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

      // 获取管理员权限
      managementClient.requestToken().execute();
    }
}

现在 managementClient 实例就可以使用了。例如可以获取用户池中的用户列表:

import cn.authing.core.mgmt.ManagementClient;

public class ManagementClientTest {
    public static void main(String[] args){
        ManagementClient managementClient = new ManagementClient("AUTHING_USERPOOL_ID", "AUTHING_USERPOOL_SECRET");
        // 获取管理员权限
        managementClient.requestToken().execute();

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

¶ 使用认证模块

初始化 AuthenticationClient 需要 userPoolId(用户池 ID)和 secret(用户池密钥):

你可以在此了解如何获取 UserPoolId, 在控制台的应用中查看自己的应用列表。

import cn.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");
    }
}

接下来可以进行注册登录等操作:

import cn.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();
    }
}

完成登录之后,update_profile 等要求用户登录的方法就可用了:

import cn.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();
    }
}

你也可以通过用户的 token 初始化 SDK, 不需要每次都调用 LoginByXXX 方法:

import cn.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.setToken("ID_TOKEN");
    }
}

再次执行 UpdateProfile 方法,发现也成功了:

import cn.authing.core.auth.AuthenticationClient;

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

¶ 错误处理

import cn.authing.core.auth.AuthenticationClient;
import cn.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.setToken("ID_TOKEN");

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

¶ 私有化部署

私有化部署场景需要指定你私有化的 Authing 服务的 GraphQL 端点(不带协议头和 Path)以及密码加密公钥,如果你不清楚可以联系 Authing IDaaS 服务管理员。

如:

ManagementClient managementClient = new ManagementClient("AUTHING_USERPOOL_ID", "AUTHING_USERPOOL_SECRET");
// 配置自定义域名
managementClient.setHost("https://core.you-authing-service.com");
// 配置自定义公钥
managementClient.setPublicKey("public key");

¶ 使用管理模块

初始化 ManagementClient 需要 userPoolId(用户池 ID) 和 secret(用户池密钥):

import cn.authing.core.mgmt.ManagementClient;

public class ManagementClientTest {
    public static void main(String[] args){
      ManagementClient managementClient = new ManagementClient("AUTHING_USERPOOL_ID", "AUTHING_USERPOOL_SECRET");
      // 获取管理员权限
      managementClient.requestToken().execute();
    }
}

¶ 使用认证模块

初始化 ManagementClient 需要 userPoolId(用户池 ID):

import cn.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("AUTHING_USERPOOL_ID");
        // 配置自定义域名
        authenticationClient.setAppId("https://core.you-authing-service.com");
        // 配置自定义公钥
        authenticationClient.setPublicKey("public key");
    }
}

¶ 接口索引

可用的 Authentication 方法

  • 获取当前用户的用户资料: getCurrentUser
  • 使用邮箱注册: registerByEmail
  • 使用用户名注册: registerByUsername
  • 使用手机号验证码注册: registerByPhoneCode
  • 使用邮箱登录: loginByEmail
  • 使用用户名登录: loginByUsername
  • 使用手机号验证码登录 loginByPhoneCode
  • 使用手机号密码登录: loginByPhonePassword
  • 发送邮件: sendEmail
  • 发送短信验证码: sendSmsCode
  • 检查 token 的有效状态: checkLoginStatus
  • 使用手机号验证码重置密码: resetPasswordByPhoneCode
  • 使用邮件验证码重置密码: resetPasswordByEmailCode
  • 更新用户资料: updateProfile
  • 更新密码: updatePassword
  • 更新手机号: updatePhone
  • 更新邮箱: updateEmail
  • 刷新 token: refreshToken
  • 绑定手机号: bindPhone
  • 解绑手机号: unbindPhone

详情请见:

用户认证模块

管理模块包含以下子模块:

管理用户管理角色管理策略管理资源、权限、应用访问控制管理用户自定义字段管理分组管理组织机构管理用户池配置管理注册白名单管理应用

¶ 获取帮助

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

本文是否有解决您的问题?

如果遇到其他问题,你可以在 authing-chat/community 联系我们。

  • 安装
  • 使用管理模块
  • 使用认证模块
  • 错误处理
  • 私有化部署
  • 接口索引
  • 获取帮助

用户身份管理

集成第三方登录
手机号闪验 (opens new window)
通用登录表单组件
自定义认证流程

企业内部管理

单点登录
多因素认证
权限管理

开发者

开发文档
框架集成
博客 (opens new window)
Github (opens new window)
社区用户中心 (opens new window)

公司

服务状态
176-0250-2507
xuziqiang@authing.cn
北京市海淀区中关村东路威盛大厦 6 层

京ICP备19051205号

© 北京蒸汽记忆科技有限公司