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

¶ 用户认证模块

更新时间: 2021-04-13 07:44:33

此模块包含注册登录、重置手机号邮箱、修改账号信息等方法,是以你的终端用户(End User)的身份进行请求,适合在需要验证用户身份的情况下使用。

¶ 使用邮箱注册

AuthenticationClient().registerByEmail(email, password, profile, options)

使用邮箱注册,此接口不要求用户对邮箱进行验证,用户注册之后 emailVerified 字段会为 false 。如果你希望邮箱未验证的用户不能进行登录,可以使用 pipeline 对此类请求进行拦截。

¶ 参数

  • email <String> 邮箱
  • password <String> 密码
  • profile <RegisterProfileInput> 用户资料
  • options <Object>
  • options.forceLogin <Boolean> 是否走一遍完整的登录的,会触发登录前后的 pipeline 函数以及登录事件 webhook ,同时该用户的累计登录次数会加 1 。默认为 false 。
  • options.clientIp <String> 客户端真实 IP,如果你在服务器端调用此接口,请务必将此参数设置为终端用户的真实 IP。

¶ 示例

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

¶ 使用用户名注册

AuthenticationClient().registerByUsername(username, password, profile, options)

使用用户名注册

¶ 参数

  • username <String> 用户名
  • password <String> 密码
  • profile <RegisterProfileInput> 用户资料
  • options <Object>
  • options.forceLogin <Boolean> 是否走一遍完整的登录的,会触发登录前后的 pipeline 函数以及登录事件 webhook ,同时该用户的累计登录次数会加 1 。默认为 false 。
  • options.generateToken <Boolean> 是否为该用户生成 token,不会触发登录后的完整流程,用户的累计登录次数不会加 1。默认为 false 。
  • options.clientIp <String> 客户端真实 IP,如果你在服务器端调用此接口,请务必将此参数设置为终端用户的真实 IP。

¶ 示例

String username = "test";
String password = "123456";
User user = authenticationClient.registerByUsername(new RegisterByUsernameInput(username, password)).execute();

¶ 使用手机号注册

AuthenticationClient().registerByPhoneCode(phone, code, password, profile, options)

使用手机号注册,你可以同时设置该账号的初始密码。发送短信的接口请见 sendSmsCode

¶ 参数

  • phone <String> 手机号
  • code <String> 短信验证码
  • password <String> 初始密码
  • profile <RegisterProfileInput> 用户资料
  • options <Object>
  • options.forceLogin <Boolean> 是否走一遍完整的登录的,会触发登录前后的 pipeline 函数以及登录事件 webhook ,同时该用户的累计登录次数会加 1 。默认为 false 。
  • options.generateToken <Boolean> 是否为该用户生成 token,不会触发登录后的完整流程,用户的累计登录次数不会加 1。默认为 false 。
  • options.clientIp <String> 客户端真实 IP,如果你在服务器端调用此接口,请务必将此参数设置为终端用户的真实 IP。

¶ 示例

String phone = "phone number";
String code = "1234";
String password = "123456";
User user = authenticationClient.registerByPhoneCode(new RegisterByPhoneCodeInput(phone, code).withPassword(password)).execute();

¶ 发送短信验证码

AuthenticationClient().sendSmsCode(phone)

发送短信验证码, 短信验证码的有效时间为 60 s。

¶ 参数

  • phone <String>

¶ 示例

String phone = "phone number";
authenticationClient.sendSmsCode(phone).execute();

¶ 使用邮箱登录

AuthenticationClient().loginByEmail(email, password, options)

使用邮箱登录,该接口默认不会限制未验证的邮箱进行登录,如果你希望邮箱未验证的用户不能进行登录,可以使用 pipeline 对此类请求进行拦截。

如果你的用户池配置了登录失败检测,当同一 IP 下登录多次失败的时候会要求用户输入图形验证码(code 为 2000)。

¶ 参数

  • email <String> 邮箱
  • password <String> 密码
  • options <Object>
  • options.autoRegister <Boolean> 是否自动注册。如果检测到用户不存在,会根据登录账密自动创建一个账号。
  • options.captchaCode <String> 图形验证码
  • options.clientIp <String> 客户端真实 IP,如果你在服务器端调用此接口,请务必将此参数设置为终端用户的真实 IP。

¶ 示例

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

¶ 使用用户名登录

AuthenticationClient().loginByUsername(username, password, options)

使用用户名登录。

如果你的用户池配置了登录失败检测,当同一 IP 下登录多次失败的时候会要求用户输入图形验证码(code 为 2000)。

¶ 参数

  • username <String> 用户名
  • password <String> 密码
  • options <Object>
  • options.autoRegister <Boolean> 是否自动注册。如果检测到用户不存在,会根据登录账密自动创建一个账号。
  • options.captchaCode <String> 图形验证码
  • options.clientIp <String> 客户端真实 IP,如果你在服务器端调用此接口,请务必将此参数设置为终端用户的真实 IP。

¶ 示例

String username = "username";
String password = "123456";
User user = authenticationClient.loginByUsername(new LoginByUsernameInput(username, password)).execute();

¶ 使用手机号验证码登录

AuthenticationClient().loginByPhoneCode(phone, code)

使用手机号验证码登录。

¶ 参数

  • phone <String> 手机号
  • code <String> 短信验证码
  • options.clientIp <String> 客户端真实 IP,如果你在服务器端调用此接口,请务必将此参数设置为终端用户的真实 IP。

¶ 示例

String phone = "phone number";
String code = "1234";
User user = authenticationClient.loginByPhoneCode(new LoginByPhoneCodeInput(phone, code)).execute();

¶ 使用手机号密码登录

AuthenticationClient().loginByPhonePassword(phone, password, options)

使用手机号密码登录。

¶ 参数

  • phone <String> 手机号
  • password <String> 密码
  • options <Object>
  • options.captchaCode <String> 图形验证码
  • options.clientIp <String> 客户端真实 IP,如果你在服务器端调用此接口,请务必将此参数设置为终端用户的真实 IP。

¶ 示例

String phone = "phone number";
String password = "123456";
User user = authenticationClient.loginByPhonePassword(new LoginByPhonePasswordInput(phone, password)).execute();

¶ 检测 Token 登录状态

AuthenticationClient().checkLoginStatus(token)

检测 Token 登录状态

¶ 参数

  • token <String> 用户的登录凭证 token

¶ 示例

JwtTokenStatus status = authenticationClient.checkLoginStatus().execute();

¶ 发送邮件

AuthenticationClient().sendEmail(email, scene)

发送邮件

¶ 参数

  • email <String> 邮箱
  • scene <EmailScene> 发送场景,可选值为 RESET_PASSWORD(发送重置密码邮件,邮件中包含验证码)、VerifyEmail(发送验证邮箱的邮件)、ChangeEmail(发送修改邮箱邮件,邮件中包含验证码)

¶ 示例

authenticationClient.sendEmail("test@example.com", EmailScene.RESET_PASSWORD).execute();

¶ 通过短信验证码重置密码

AuthenticationClient().resetPasswordByPhoneCode(phone, code, newPassword)

通过短信验证码重置密码,你需要先调用 sendSmsCode 接口发送重置密码邮件。

¶ 参数

  • phone <String> 手机号
  • code <String> 验证码
  • newPassword <String> 新的密码

¶ 示例

String phone = "phone number";
String code = "1234";
String password = "123456";
authenticationClient.resetPasswordByPhoneCode(phone, code, password).execute();

¶ 通过邮件验证码重置密码

AuthenticationClient().resetPasswordByEmailCode(phone, code, newPassword)

通过邮件验证码重置密码,你需要先调用 sendEmail 接口发送重置密码邮件。

¶ 参数

  • phone <String> 手机号
  • code <String> 验证码
  • newPassword <String> 新的密码

¶ 示例

String email = "test@example.com";
String code = "1234";
String password = "123456";
authenticationClient.resetPasswordByEmailCode(email, code, password).execute();

¶ 修改用户资料

AuthenticationClient().updateProfile(updates)

修改用户资料,此接口不能用于修改手机号、邮箱、密码,如果需要请调用 updatePhone、updateEmail、updatePassword 接口。

¶ 参数

  • updates <UpdateUserInput> 修改的用户资料
  • updates.username <String> 用户名
  • updates.nickname <String> 昵称
  • updates.photo <String> 头像
  • updates.company <String> 公司
  • updates.browser <String> 浏览器
  • updates.device <String> 设备
  • updates.lastIP <String> 最近登录的 IP
  • updates.name <String> Name
  • updates.givenName <String> Given Name
  • updates.familyName <String> Family Name
  • updates.middleName <String> Middle Name
  • updates.profile <String> Profile Url
  • updates.preferredUsername <String> Preferred Name
  • updates.website <String> 个人网站
  • updates.gender <String> 性别, M(Man) 表示男性、F(Female) 表示女性、未知表示 U(Unknown)
  • updates.birthdate <String> 生日
  • updates.zoneinfo <String> 时区
  • updates.locale <String> 语言
  • updates.address <String> 地址
  • updates.streetAddress <String> 街道地址
  • updates.locality <String>
  • updates.region <String> 地域
  • updates.postalCode <String> 邮编
  • updates.city <String> 城市
  • updates.province <String> 省份
  • updates.country <String> 国家

¶ 示例

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

¶ 更新用户密码

AuthenticationClient().updatePassword(newPassword, oldPassword)

更新用户密码

¶ 参数

  • newPassword <String> 新密码
  • oldPassword <String> 旧密码,如果用户没有设置密码,可以不填。

¶ 示例

String oldPassword = "111111";
String newPassword = "123456";
User user = authenticationClient.updatePassword(newPassword, oldPassword).execute();

¶ 更新用户手机号

AuthenticationClient().updatePhone(phone, phoneCode, oldPhone, oldPhoneCode)

更新用户手机号。和修改邮箱一样,默认情况下,如果用户当前已经绑定了手机号,需要同时验证原有手机号(目前账号绑定的手机号)和当前邮箱(将要绑定的手机号)。 也就是说,用户 A 当前绑定的手机号为 15888888888,想修改为 15899999999,那么就需要同时验证这两个手机号。 开发者也可以选择不开启 “验证原有手机号“ ,可以在 Authing 控制台 的 设置目录下的安全信息模块进行关闭。 用户首次绑定手机号请使用 bindPhone 接口。

¶ 参数

  • phone <String> 新手机号
  • phoneCode <String> 新手机号的验证码
  • oldPhone <String> 旧手机号
  • oldPhoneCode <String> 旧手机号的验证码

¶ 示例

User user = authenticationClient.updatePhone("phone number", "1234").execute();

¶ 更新用户邮箱

AuthenticationClient().updateEmail(email, emailCode, oldEmail, oldEmailCode)

如果用户已经绑定了邮箱,默认情况下,需要同时验证原有邮箱(目前账号绑定的邮箱)和当前邮箱(将要绑定的邮箱)。也就是说,用户 A 当前绑定的邮箱为 123456@qq.com,想修改为 1234567@qq.com,那么就需要同时验证这两个邮箱。 开发者也可以选择不开启 “验证原有邮箱“ ,可以在 Authing 控制台 的 设置目录下的安全信息模块进行关闭。 用户首次绑定手机号请使用 bindEmail 接口。

¶ 参数

  • email <String> 新邮箱
  • emailCode <String> 新邮箱的验证码
  • oldEmail <String> 旧邮箱
  • oldEmailCode <String> 旧邮箱的验证码

¶ 示例

String newEmail = "new@example.com";
String emailCode = "1234"
User user = authenticationClient.updateEmail(newEmail, emailCode).execute();

¶ 刷新当前用户的 token

AuthenticationClient().refreshToken()

刷新当前用户的 token,调用此接口要求先登录。

¶ 参数

¶ 示例

RefreshToken token = authenticationClient.refreshToken().execute();

¶ 绑定手机号

AuthenticationClient().bindPhone(phone, phoneCode)

用户初次绑定手机号,如果需要修改手机号请使用 updatePhone 接口。

¶ 参数

  • phone <String>
  • phoneCode <String>

¶ 示例

User user = authenticationClient.bindPhone("phone number", "1234").execute();

¶ 解绑手机号

AuthenticationClient().unbindPhone()

用户解绑手机号

¶ 示例

User user = authenticationClient.unbindPhone().execute();

¶ 解绑邮箱

AuthenticationClient().unbindEmail()

用户解绑邮箱

¶ 示例

User user = authenticationClient.unbindEmail().execute();

¶ 获取当前登录的用户信息

AuthenticationClient().getCurrentUser()

获取当前登录的用户信息

¶ 示例

User user = authenticationClient.getCurrentUser().execute();

¶ 退出登录

AuthenticationClient().logout()

退出登录,会使用当前用户的 token 失效。

¶ 示例

authenticationClient.logout().execute();

¶ 绑定社交账号

AuthenticationClient().linkAccount(primaryUserToken, secondaryUserToken)

将一个社交账号绑定到一个主账号(手机号、邮箱账号)。

¶ 参数

  • primaryUserToken <String> 主账号 Token
  • secondaryUserToken <String> 社交账号 Token

¶ 示例

String primaryUserToken = "test";
String secondaryUserToken = "test";
CommonMessage message = authenticationClient.linkAccount(primaryUserToken, secondaryUserToken).execute();

¶ 使用 LDAP 用户名登录

AuthenticationClient().loginByLdap(loginByLdapParam)

使用 LDAP 用户名登录

¶ 参数

  • username <String> 用户名
  • password <String> 密码
  • options <LoginByLdapParamOptions>
  • options.autoRegister <Boolean> 是否自动注册。如果检测到用户不存在,会根据登录账密自动创建一个账号。
  • options.captchaCode <String> 图形验证码
  • options.clientIp <String> 客户端真实 IP,如果你在服务器端调用此接口,请务必将此参数设置为终端用户的真实 IP。

¶ 示例

String username = "test";
String password = "test";
LoginByLdapParam loginByLdapParam = new LoginByLdapParam(username, password);
User user = authenticationClient.loginByLdap(loginByLdapParam).execute();

¶ 使用 AD 用户名登录

AuthenticationClient().loginByAd(username, password)

使用 AD 用户名登录

¶ 参数

  • username <String> 用户名
  • password <String> 密码

¶ 示例

String username = "test";
String password = "test";
User user = authenticationClient.loginByAd(username, password).execute();

¶ 检查密码强度

AuthenticationClient().checkPasswordStrength(password)

检查密码强度,点此查看详情。

¶ 参数

  • password <String> 密码

¶ 示例

String password = "test";
CheckPasswordStrengthResult result = authenticationClient.checkPasswordStrength(password).execute();

¶ 获取用户所在组织机构

AuthenticationClient().listOrgs()

获取用户所在的组织机构,以及他所属的节点在此组织机构内的完整路径。

¶ 示例

List<List<Org>> orgs = this.authenticationClient.listOrgs().execute();

¶ 获取用户自定义数据

AuthenticationClient().listUdv()

获取用户的所有自定义数据。

¶ 示例

authenticationClient.setAppId("AUTHING_APP_ID");
authenticationClient.setToken("ACCESS_TOKEN");

List<UserDefinedData> list = authenticationClient.listUdv().execute();

¶ 返回值

  • Promise<UserDefinedData>

¶ 示例数据

[
  {
    "key": "school",
    "dataType": "STRING",
    "value": "华中科技大学",
    "label": "学校"
  }
]

¶ 设置自定义数据

AuthenticationClient().setUdv(key, value)

设置用户的自定义字段,需要用户池配置了该字段之后才能设置,且传入值的类型必须和定义的类型匹配。

¶ 参数

  • key <String> 自定义字段的 key 。
  • value <String> 所设置的值,传入值的类型必须和定义的类型匹配。

¶ 示例

authenticationClient.setAppId("AUTHING_APP_ID");
authenticationClient.setToken("ACCESS_TOKEN");

List<UserDefinedData> list = authenticationClient.setUdv('school', '华中科技大学').execute();

¶ 删除自定义数据

AuthenticationClient().removeUdv(key)

删除自定义数据。

¶ 参数

  • key <String> 自定义字段的 key 。

¶ 示例

authenticationClient.setAppId("AUTHING_APP_ID");
authenticationClient.setToken("ACCESS_TOKEN");

List<UserDefinedData> list = authenticationClient.removeUdv('school').execute();

¶ 获取用户账号安全等级

AuthenticationClient().getSecurityLevel()

获取用户账号安全等级。

¶ 示例

SecurityLevel result = this.authenticationClient.getSecurityLevel().execute();
Assert.assertNotNull(result !=null);

¶ 获取用户被授权的所有资源列表

AuthenticationClient().listAuthorizedResources(namespace)

获取一个用户被授权的所有资源,用户被授权的所有资源里面包括从角色、分组、组织机构继承的资源。

¶ 参数

  • namespace <String> 权限分组的 code,详情请见使用权限分组管理权限资源。

¶ 示例

String namespace = "default";
PaginatedAuthorizedResources result = this.authenticationClient.listAuthorizedResources(namespace).execute();
Assert.assertNotNull(result.getList());

¶ 示例数据

  • type 为资源类型,一共有以下几种资源类型
    • DATA: 数据类型;
    • API: API 类型数据;
    • MENU: 菜单类型数据;
    • BUTTON: 按钮类型数据;
  • code: 资源描述符,如果是 DATA 类型资源,格式为 resourceType:resourceId,如 books:* 表示所有书籍,books:1 表示 ID 为 1 的书籍。
  • actions: 用户被授权对该资源的操作。
{
  "totalCount": 12,
  "list": [
    {
      "code": "menu_a",
      "type": "MENU"
    },
    {
      "code": "menu_b",
      "type": "MENU"
    },
    {
      "code": "books:1",
      "type": "DATA",
      "actions": ["books:delete", "books:update"]
    }
  ]
}

¶ Code 换 Token

AuthenticationClient().getAccessTokenByCode(code)

使用授权码 Code 获取用户的 Token 信息。

¶ 参数

  • code <String> 授权码 Code,用户在认证成功后,Authing 会将授权码 Code 发送到回调地址,详情请见使用 OIDC 授权码模式。

初始化 AuthenticationClient 时的参数:

  • appId <String> 应用 ID,必填。
  • secret <String> 应用密钥,必填。
  • appHost <String> Authing 应用 Host 地址(必填),如 https://my-awesome-app.authing.cn。
  • redirectUri <String> 业务回调 URL,必填。
  • protocol <String> 协议类型,可选值为 oidc、oauth。
  • tokenEndPointAuthMethod <String> 获取 token 端点验证方式,可选值为 client_secret_post、client_secret_basic、none,默认为 client_secret_post。
  • introspectionEndPointAuthMethod <String> 检验 token 端点验证方式,可选值为 client_secret_post、client_secret_basic、none,默认为 client_secret_post。
  • revocationEndPointAuthMethod <String> 撤回 token 端点验证方式,可选值为 client_secret_post、client_secret_basic、none,默认为 client_secret_post。

¶ 示例

AuthenticationClient authenticationClient = new AuthenticationClient("AUTHING_USERPOOL_ID");
authenticationClient.setAppId("AUTHING_APP_ID");
authenticationClient.setSecret("AUTHING_APP_SECRET");
authenticationClient.setHost("https://YOUR_AUTHING_DOMAIN.authing.cn");
// 你在 Authing 控制台配置的回调链接
authenticationClient.setRedirectUri("https://baidu.com");

Object result = authenticationClient.getAccessTokenByCode("CODE").execute();
Assert.assertNotNull(result !=null);

¶ 示例数据

{
  "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlRmTE90M0xibjhfYThwUk11ZXNzYW1xai1vM0RCQ3MxLW93SExRLVZNcVEifQ.eyJqdGkiOiJsdzg0NW5zdGcwS3EtMTlodVpQOHYiLCJzdWIiOiI1ZmY3MDFkODQ2YjkyMDNlMmY2YWM2ZjMiLCJpYXQiOjE2MTU4ODM1ODYsImV4cCI6MTYxNTg4NzE4Niwic2NvcGUiOiJlbWFpbCBvcGVuaWQgcHJvZmlsZSBwaG9uZSIsImlzcyI6Imh0dHBzOi8vb2lkYzEuYXV0aGluZy5jbi9vaWRjIiwiYXVkIjoiNWYxN2E1MjlmNjRmYjAwOWI3OTRhMmZmIn0.VvYKBcWcr8iIi1b37ugWQ9hsvog4_7EqDQyFqwhIuvM0NHlHH3Bhw83EQIKSNfbWV4nv3ihfeNGPLMzslbQr-wwjnWZTLMYl1bcn7IdVtD_kTN3Zz10MwF5td-VQ7UndU28wJ0HE1mo6E8QH93kYGckS5FSZXmCBa0M5H59Jec_a1MHI1MZrr_V9cZ9EfeF97V-PcqU8JVAwDZclCJ3mWY_Mb65RnMR9yEVqUZzJStmaXGMuRIzjkm2pklqt0CtQQJfzECXq_4USpwRXDiYLWILYPUCcO6hGxDjhMEd8IcxdG51TQP-w1UM6LyIRn61uSJvDsz8zg5dStDKyocypiA",
  "expires_in": 3600,
  "id_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRlc3QzQDEyMy5jb20iLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsInN1YiI6IjVmZjcwMWQ4NDZiOTIwM2UyZjZhYzZmMyIsImJpcnRoZGF0ZSI6bnVsbCwiZmFtaWx5X25hbWUiOm51bGwsImdlbmRlciI6IlUiLCJnaXZlbl9uYW1lIjpudWxsLCJsb2NhbGUiOm51bGwsIm1pZGRsZV9uYW1lIjpudWxsLCJuYW1lIjpudWxsLCJuaWNrbmFtZSI6bnVsbCwicGljdHVyZSI6Imh0dHBzOi8vZmlsZXMuYXV0aGluZy5jby9hdXRoaW5nLWNvbnNvbGUvZGVmYXVsdC11c2VyLWF2YXRhci5wbmciLCJwcmVmZXJyZWRfdXNlcm5hbWUiOm51bGwsInByb2ZpbGUiOm51bGwsInVwZGF0ZWRfYXQiOiIyMDIxLTAzLTE1VDA1OjU0OjU0LjY4NVoiLCJ3ZWJzaXRlIjpudWxsLCJ6b25laW5mbyI6bnVsbCwicGhvbmVfbnVtYmVyIjpudWxsLCJwaG9uZV9udW1iZXJfdmVyaWZpZWQiOmZhbHNlLCJub25jZSI6IjcwVEU3eW9NVFEiLCJhdF9oYXNoIjoiUFNnOGw5eDRldGxmLXA4UDdjYnVoQSIsImlzcyI6Imh0dHBzOi8vb2lkYzEuYXV0aGluZy5jbi9vaWRjIiwiaXNzMiI6Imh0dHBzOi8vYmFpZHUuY29tIiwiYXVkIjoiNWYxN2E1MjlmNjRmYjAwOWI3OTRhMmZmIiwiZXhwIjoxNjE1ODg3MTg3LCJpYXQiOjE2MTU4ODM1ODh9.OlX-FP7znIEqx0YpnOQ8kxadMe1toHDj1KPVm0dbEVc",
  "scope": "email openid profile phone",
  "token_type": "Bearer"
}

¶ Token 换用户信息

AuthenticationClient().getUserInfoByAccessToken('access_token')

使用 Access token 获取用户信息。

¶ 参数

  • access_token <String> Access token,使用授权码 Code 换取的 Access token 的内容。详情请见使用 OIDC 授权码模式。

初始化 AuthenticationClient 时的参数:

  • appId <String> 应用 ID,必填。
  • secret <String> 应用密钥,必填。
  • appHost <String> Authing 应用 Host 地址(必填),如 https://my-awesome-app.authing.cn。
  • redirectUri <String> 业务回调 URL,必填。
  • protocol <String> 协议类型,可选值为 oidc、oauth。
  • tokenEndPointAuthMethod <String> 获取 token 端点验证方式,可选值为 client_secret_post、client_secret_basic、none,默认为 client_secret_post。
  • introspectionEndPointAuthMethod <String> 检验 token 端点验证方式,可选值为 client_secret_post、client_secret_basic、none,默认为 client_secret_post。
  • revocationEndPointAuthMethod <String> 撤回 token 端点验证方式,可选值为 client_secret_post、client_secret_basic、none,默认为 client_secret_post。

¶ 示例

AuthenticationClient authenticationClient = new AuthenticationClient("AUTHING_USERPOOL_ID");
authenticationClient.setAppId("AUTHING_APP_ID");
authenticationClient.setSecret("AUTHING_APP_SECRET");
authenticationClient.setHost("https://YOUR_AUTHING_DOMAIN.authing.cn");
// 你在 Authing 控制台配置的回调链接
authenticationClient.setRedirectUri("https://baidu.com");


Object result = testAC.getUserInfoByAccessToken("ACCESS_TOKEN").execute();
Assert.assertNotNull(result !=null);

¶ 示例数据

{
  "address": {
    "country": null,
    "postal_code": null,
    "region": null,
    "formatted": null
  },
  "birthdate": null,
  "family_name": null,
  "gender": "U",
  "given_name": null,
  "locale": null,
  "middle_name": null,
  "name": null,
  "nickname": null,
  "picture": "https://files.authing.co/authing-console/default-user-avatar.png",
  "preferred_username": null,
  "profile": null,
  "updated_at": "2021-03-03T06:17:14.485Z",
  "website": null,
  "zoneinfo": null,
  "email": "test1@authing.cn",
  "email_verified": false,
  "sub": "603f184cec4505e2868431fc",
  "phone_number": null,
  "phone_number_verified": false
}

¶ Client Credentials 模式获取 Access Token

AuthenticationClient().getAccessTokenByClientCredentials(scope, options)

使用编程访问账号获取具备权限的 Access Token。

¶ 参数

  • scope <String> 权限项目,空格分隔的字符串,每一项代表一个权限。详情请见机器间(M2M)授权。
  • options,编程访问账号的 AK 与 SK 信息。
  • options.accessKey,编程访问账号 AccessKey。
  • options.secretKey,编程访问账号 SecretKey。

初始化 AuthenticationClient 时的参数:

  • appId <String> 应用 ID,必填。
  • secret <String> 应用密钥,必填。
  • appHost <String> Authing 应用 Host 地址(必填),如 https://my-awesome-app.authing.cn。
  • redirectUri <String> 业务回调 URL,必填。
  • protocol <String> 协议类型,可选值为 oidc、oauth。
  • tokenEndPointAuthMethod <String> 获取 token 端点验证方式,可选值为 client_secret_post、client_secret_basic、none,默认为 client_secret_post。
  • introspectionEndPointAuthMethod <String> 检验 token 端点验证方式,可选值为 client_secret_post、client_secret_basic、none,默认为 client_secret_post。
  • revocationEndPointAuthMethod <String> 撤回 token 端点验证方式,可选值为 client_secret_post、client_secret_basic、none,默认为 client_secret_post。

¶ 示例

AuthenticationClient authenticationClient = new AuthenticationClient("AUTHING_USERPOOL_ID");
authenticationClient.setAppId("AUTHING_APP_ID");
authenticationClient.setSecret("AUTHING_APP_SECRET");
authenticationClient.setHost("https://YOUR_AUTHING_DOMAIN.authing.cn");
// 你在 Authing 控制台配置的回调链接
authenticationClient.setRedirectUri("https://baidu.com");

ClientCredentialInput clientCredentialInput = new ClientCredentialInput("AUTHING_APP_ID", "AUTHING_APP_SECRET");
Object result = testAC.getAccessTokenByClientCredentials("testr2",clientCredentialInput).execute();
Assert.assertNotNull(result !=null);

¶ 示例数据

{
  "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlRmTE90M0xibjhfYThwUk11ZXNzYW1xai1vM0RCQ3MxLW93SExRLVZNcVEifQ.eyJqdGkiOiJsdzg0NW5zdGcwS3EtMTlodVpQOHYiLCJzdWIiOiI1ZmY3MDFkODQ2YjkyMDNlMmY2YWM2ZjMiLCJpYXQiOjE2MTU4ODM1ODYsImV4cCI6MTYxNTg4NzE4Niwic2NvcGUiOiJlbWFpbCBvcGVuaWQgcHJvZmlsZSBwaG9uZSIsImlzcyI6Imh0dHBzOi8vb2lkYzEuYXV0aGluZy5jbi9vaWRjIiwiYXVkIjoiNWYxN2E1MjlmNjRmYjAwOWI3OTRhMmZmIn0.VvYKBcWcr8iIi1b37ugWQ9hsvog4_7EqDQyFqwhIuvM0NHlHH3Bhw83EQIKSNfbWV4nv3ihfeNGPLMzslbQr-wwjnWZTLMYl1bcn7IdVtD_kTN3Zz10MwF5td-VQ7UndU28wJ0HE1mo6E8QH93kYGckS5FSZXmCBa0M5H59Jec_a1MHI1MZrr_V9cZ9EfeF97V-PcqU8JVAwDZclCJ3mWY_Mb65RnMR9yEVqUZzJStmaXGMuRIzjkm2pklqt0CtQQJfzECXq_4USpwRXDiYLWILYPUCcO6hGxDjhMEd8IcxdG51TQP-w1UM6LyIRn61uSJvDsz8zg5dStDKyocypiA",
  "expires_in": 3600,
  "scope": "email openid profile phone",
  "token_type": "Bearer"
}

¶ 获取当前用户能够访问的应用

AuthenticationClient().listApplications(options)

获取当前用户能够访问的应用。

¶ 参数

  • options<object>,选填
    • options.page <number> 分页序号, 默认为 1。
    • options.limit <number> 每页返回的个数, 默认为 10。

¶ 示例

// 拼接前端万能登出链接
AuthenticationClient authenticationClient = new AuthenticationClient({
  appId: '应用 ID',
})

// 登录用户
// 可以使用其他方式登录
authenticationClient.loginByUsername(username, password, ops)

Pagination<ApplicationPublicDetail> resData = authenticationClient.listApplications({
  page: 1,
  limit: 10,
})

¶ 示例数据

正常时返回:

{
  "code": 200,
  "message": "获取可访问的应用列表成功",
  "data": {
    "list": {
      "0": {
        "id": "5f97fb40d352ecf69ffe6d98",
        "name": "oo",
        "logo": "https://files.authing.co/authing-console/default-app-logo.png",
        "domain": "okokiohutuyfrtd",
        "description": null,
        "createdAt": "2020-10-27T10:49:36.817Z",
        "updatedAt": "2021-03-17T10:39:53.650Z",
        "protocol": "oidc"
      }
    },
    "totalCount": 1
  }
}

¶ 获取用户被授权的所有资源列表

AuthenticationClient().listAuthorizedResources(namespace)

获取一个用户被授权的所有资源,用户被授权的所有资源里面包括从角色、分组、组织机构继承的资源。

¶ 参数

  • namespace <String> 权限分组的 code,详情请见使用权限分组管理权限资源。

¶ 示例

PaginatedAuthorizedResources res = authenticationClient.listAuthorizedResources(namespace).execute();

¶ 示例数据

  • type 为资源类型,一共有以下几种资源类型
    • DATA: 数据类型;
    • API: API 类型数据;
    • MENU: 菜单类型数据;
    • BUTTON: 按钮类型数据;
  • code: 资源描述符,如果是 DATA 类型资源,格式为 resourceType:resourceId,如 books:* 表示所有书籍,books:1 表示 ID 为 1 的书籍。
  • actions: 用户被授权对该资源的操作。
{
  "totalCount": 12,
  "list": [
    {
      "code": "menu_a",
      "type": "MENU"
    },
    {
      "code": "menu_b",
      "type": "MENU"
    },
    {
      "code": "books:1",
      "type": "DATA",
      "actions": ["books:delete", "books:update"]
    }
  ]
}

¶ 获取自定义数据

AuthenticationClient().getUdfValue()

获取用户的所有自定义数据。你需要先在用户池定义用户自定义数据元信息。

¶ 示例


Map resu = authenticationClient.getUdfValue().execute();

¶ 示例数据

{
  "school": "华中科技大学",
  "age": 20
}

上一篇: 管理 MFA 下一篇: 管理用户

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

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

  • 使用邮箱注册
  • 使用用户名注册
  • 使用手机号注册
  • 发送短信验证码
  • 使用邮箱登录
  • 使用用户名登录
  • 使用手机号验证码登录
  • 使用手机号密码登录
  • 检测 Token 登录状态
  • 发送邮件
  • 通过短信验证码重置密码
  • 通过邮件验证码重置密码
  • 修改用户资料
  • 更新用户密码
  • 更新用户手机号
  • 更新用户邮箱
  • 刷新当前用户的 token
  • 绑定手机号
  • 解绑手机号
  • 解绑邮箱
  • 获取当前登录的用户信息
  • 退出登录
  • 绑定社交账号
  • 使用 LDAP 用户名登录
  • 使用 AD 用户名登录
  • 检查密码强度
  • 获取用户所在组织机构
  • 获取用户自定义数据
  • 设置自定义数据
  • 删除自定义数据
  • 获取用户账号安全等级
  • 获取用户被授权的所有资源列表
  • Code 换 Token
  • Token 换用户信息
  • Client Credentials 模式获取 Access Token
  • 获取当前用户能够访问的应用
  • 获取用户被授权的所有资源列表
  • 获取自定义数据

用户身份管理

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

企业内部管理

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

开发者

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

公司

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

京ICP备19051205号

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