跳到主要内容
给我们的新朋友:

Logto 是一个为现代应用和 SaaS 产品设计的 Auth0 替代方案。它提供 Cloud开源 服务,帮助你快速启动身份和管理 (IAM) 系统。享受认证 (Authentication)、授权 (Authorization) 和多租户管理 一体化

我们建议从 Logto Cloud 上的免费开发租户开始。这可以让你轻松探索所有功能。

在本文中,我们将介绍使用 Android (Kotlin / Java)Logto 快速构建 Microsoft Entra ID OIDC enterprise SSO 登录体验(用户认证 (Authentication))的步骤。

先决条件

  • 一个正在运行的 Logto 实例。查看 介绍页面 以开始。
  • Android (Kotlin / Java) 的基本知识。
  • 一个可用的 Microsoft Entra ID OIDC enterprise SSO 账户。

Create an application in Logto

Logto 基于 OpenID Connect (OIDC) 认证 (Authentication) 和 OAuth 2.0 授权 (Authorization)。它支持跨多个应用程序的联合身份管理,通常称为单点登录 (SSO)。

要创建你的 Native app 应用程序,只需按照以下步骤操作:

  1. 打开 Logto Console。在“开始使用”部分,点击“查看全部”链接以打开应用程序框架列表。或者,你可以导航到 Logto Console > Applications,然后点击“创建应用程序”按钮。 开始使用
  2. 在打开的模态窗口中,点击“Native app”部分,或使用左侧的快速过滤复选框过滤所有可用的“Native app”框架。点击 "Android (Kotlin)" / "Android (Java)" 框架卡片以开始创建你的应用程序。 框架
  3. 输入应用程序名称,例如“Bookstore”,然后点击“创建应用程序”。

🎉 太棒了!你刚刚在 Logto 中创建了你的第一个应用程序。你将看到一个祝贺页面,其中包含详细的集成指南。按照指南查看你的应用程序中的体验将会是什么样的。

Integrate Android (Kotlin) / Android (Java) SDK

提示:

安装

备注:

Logto Android SDK 支持的最低 Android API 级别是 24。

在安装 Logto Android SDK 之前,确保在 Gradle 项目的构建文件中将 mavenCentral() 添加到你的仓库配置中:

settings.gradle.kts
dependencyResolutionManagement {
repositories {
mavenCentral()
}
}

将 Logto Android SDK 添加到你的依赖项中:

build.gradle.kts
dependencies {
implementation("io.logto.sdk:android:1.1.3")
}

由于 SDK 需要访问互联网,你需要在 AndroidManifest.xml 文件中添加以下权限:

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<!-- 添加互联网权限 -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- 其他配置... -->
</manifest>

初始化 LogtoClient

创建一个 LogtoViewModel.kt 并在此视图模型中初始化 LogtoClient

LogtoViewModel.kt
//...with other imports
import io.logto.sdk.android.LogtoClient
import io.logto.sdk.android.type.LogtoConfig

class LogtoViewModel(application: Application) : AndroidViewModel(application) {
private val logtoConfig = LogtoConfig(
endpoint = "<your-logto-endpoint>",
appId = "<your-app-id>",
scopes = null,
resources = null,
usingPersistStorage = true,
)

private val logtoClient = LogtoClient(logtoConfig, application)

companion object {
val Factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras
): T {
// 从 extras 中获取 Application 对象
val application = checkNotNull(extras[APPLICATION_KEY])
return LogtoViewModel(application) as T
}
}
}
}

然后,为你的 MainActivity.kt 创建一个 LogtoViewModel

MainActivity.kt
//...with other imports
class MainActivity : AppCompatActivity() {
private val logtoViewModel: LogtoViewModel by viewModels { LogtoViewModel.Factory }
//...other codes
}

配置重定向 URI

让我们切换到 Logto Console 的应用详情页面。添加一个重定向 URI io.logto.android://io.logto.sample/callback 并点击“保存更改”。

Logto Console 中的重定向 URI

实现登录和登出

备注:

在调用 logtoClient.signIn 之前,请确保你已在管理控制台中正确配置了重定向 URI。

你可以使用 logtoClient.signIn 来让用户登录,并使用 logtoClient.signOut 来让用户登出。

例如,在 Android 应用中:

LogtoModelView.kt
//...with other imports
class LogtoViewModel(application: Application) : AndroidViewModel(application) {
// ...other codes

// 添加一个 live data 来观察认证状态
private val _authenticated = MutableLiveData(logtoClient.isAuthenticated)
val authenticated: LiveData<Boolean>
get() = _authenticated

fun signIn(context: Activity) {
logtoClient.signIn(context, "io.logto.android://io.logto.sample/callback") { logtoException ->
logtoException?.let { println(it) }
// 更新 live data
_authenticated.postValue(logtoClient.isAuthenticated)
}
}

fun signOut() {
logtoClient.signOut { logtoException ->
logtoException?.let { println(it) }
// 更新 live data
_authenticated.postValue(logtoClient.isAuthenticated)
}
}
}

然后在你的 activity 中调用 signInsignOut 方法:

MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
//...other codes

// 假设你的布局中有一个 id 为 "sign_in_button" 的按钮
val signInButton = findViewById<Button>(R.id.sign_in_button)
signInButton.setOnClickListener {
logtoViewModel.signIn(this)
}

// 假设你的布局中有一个 id 为 "sign_out_button" 的按钮
val signOutButton = findViewById<Button>(R.id.sign_out_button)
signOutButton.setOnClickListener {
if (logtoViewModel.authenticated) { // 检查用户是否已认证
logtoViewModel.signOut()
}
}

// 观察认证状态以更新 UI
logtoViewModel.authenticated.observe(this) { authenticated ->
if (authenticated) {
// 用户已认证
signInButton.visibility = View.GONE
signOutButton.visibility = View.VISIBLE
} else {
// 用户未认证
signInButton.visibility = View.VISIBLE
signOutButton.visibility = View.GONE
}
}

}
}

检查点:测试你的应用程序

现在,你可以测试你的应用程序:

  1. 运行你的应用程序,你将看到登录按钮。
  2. 点击登录按钮,SDK 将初始化登录过程并将你重定向到 Logto 登录页面。
  3. 登录后,你将被重定向回你的应用程序,并看到登出按钮。
  4. 点击登出按钮以清除令牌存储并登出。

Add Microsoft Entra ID OIDC enterprise SSO connector

To simplify access management and gain enterprise-level safeguards for your big clients, connect with Android (Kotlin) / Android (Java) as a federated identity provider. The Logto enterprise SSO connector helps you establish this connection in minutes by allowing several parameter inputs.

To add an enterprise SSO connector, simply follow these steps:

  1. Navigate to Logto console > Enterprise SSO.

SSO page

  1. Click "Add enterprise connector" button and choose your SSO provider type. Choose from prebuilt connectors for Microsoft Entra ID (Azure AD), Google Workspace, and Okta, or create a custom SSO connection using the standard OpenID Connect (OIDC) or SAML protocol.
  2. Provide a unique name (e.g., SSO sign-in for Acme Company).

Select your SSO provider

  1. Configure the connection with your IdP in the "Connection" tab. Check the guides above for each connector types.

SSO connection

  1. Customize the SSO experience and enterprise’s email domain in the "Experience" tab. Users sign in with the SSO-enabled email domain will be redirected to SSO authentication.

SSO experience

  1. Save changes.

Set up Azure AD SSO 应用程序

步骤 1:创建 Microsoft EntraID OIDC 应用程序

  1. 前往 Microsoft Entra 管理中心 并以管理员身份登录。

  2. 浏览到 Identity > Applications > App registrations。

Create Application
  1. 选择 New registration

  2. 输入应用程序名称,并为你的应用程序选择适当的账户类型。

  3. 选择 Web 作为应用程序平台。

  4. 从 Logto 的 SSO 设置页面复制并粘贴 redirect URIredirect URI 是用户在通过 Microsoft Entra ID 认证后被重定向的 URL。

Configure Application
  1. 点击 Register 创建应用程序。

步骤 2:在 Logto 配置 Microsoft Entra ID OIDC 单点登录 (SSO)

成功创建 Microsoft Entra OIDC 应用程序后,你需要将 IdP 配置提供给 Logto。在 Logto 控制台中导航到 Connection 选项卡,并填写以下配置:

  1. Client ID:由 Microsoft Entra 分配给你的 OIDC 应用程序的唯一标识符。Logto 使用此标识符在 OIDC 流程中识别和认证 (Authentication) 应用程序。你可以在应用程序概览页面找到它,标识为 Application (client) ID
Application Details
  1. Client Secret:创建一个新的客户端密钥,并将其值复制到 Logto。此密钥用于认证 (Authentication) OIDC 应用程序并保护 Logto 与 IdP 之间的通信。
Create Secret
  1. 发行者 (Issuer):发行者 URL,是 IdP 的唯一标识符,指定可以找到 OIDC 身份提供商 (IdP) 的位置。它是 OIDC 配置的重要组成部分,因为它帮助 Logto 发现必要的端点。

    Logto 不需要手动提供所有这些 OIDC 端点,而是自动获取所有必需的配置和 IdP 端点。这是通过利用你提供的发行者 URL 并调用 IdP 的发现端点来完成的。

    要获取发行者 URL,你可以在应用程序概览页面的 Endpoints 部分找到它。

    找到 OpenID Connect metadata document 端点并复制 URL 不包括末尾路径 .well-known/openid-configuration。这是因为 Logto 在获取 OIDC 配置时会自动将 .well-known/openid-configuration 附加到发行者 URL。

Endpoints
  1. 权限 (Scope):一个以空格分隔的字符串列表,定义 Logto 在 OIDC 认证 (Authentication) 过程中请求的所需权限或访问级别。scope 参数允许你指定 Logto 从 IdP 请求哪些信息和访问权限。

scope 参数是可选的。无论自定义 scope 设置如何,Logto 始终会向 IdP 发送 openidprofileemail 权限 (Scopes)。

点击 Save 完成配置过程。

步骤 3:设置电子邮件域并启用 SSO 连接器

在连接器 体验 (Experience) 选项卡中提供你组织的电子邮件 。这将启用 SSO 连接器作为这些用户的认证 (Authentication) 方法。

在指定域中的电子邮件地址的用户将被专门限制为仅使用你的 SSO 连接器作为他们的唯一认证 (Authentication) 方法。

Save your configuration

仔细检查你是否在 Logto 连接器配置区域填写了必要的值。点击“保存并完成”(或“保存更改”),Microsoft Entra ID OIDC enterprise SSO 连接器现在应该可用了。

Enable Microsoft Entra ID OIDC enterprise SSO connector in Sign-in Experience

You don’t need to configure enterprise connectors individually, Logto simplifies SSO integration into your applications with just one click.

  1. Navigate to: Console > Sign-in experience > Sign-up and sign-in.
  2. Enable the "Enterprise SSO" toggle.
  3. Save changes.

Once enabled, a "Single Sign-On" button will appear on your sign-in page. Enterprise users with SSO-enabled email domains can access your services using their enterprise identity providers (IdPs).

Auto detect SSO sign-in via email domain Navigate to SSO sign-in via manually click link button

To learn more about the SSO user experience, including SP-initiated SSO and IdP-initiated SSO, refer to User flows: Enterprise SSO.

Testing and Validation

返回到你的 Android (Kotlin / Java) 应用。你现在应该可以使用 Microsoft Entra ID OIDC enterprise SSO 登录了。享受吧!

Further readings

终端用户流程:Logto 提供开箱即用的认证 (Authentication) 流程,包括多因素认证 (MFA) 和企业单点登录 (SSO),以及强大的 API,用于灵活实现账户设置、安全验证和多租户体验。

授权 (Authorization):授权 (Authorization) 定义了用户在被认证 (Authentication) 后可以执行的操作或访问的资源。探索如何保护你的 API 以用于原生和单页应用程序,并实现基于角色的访问控制 (RBAC)。

组织 (Organizations):在多租户 SaaS 和 B2B 应用中特别有效,组织功能支持租户创建、成员管理、组织级 RBAC 和即时供应。

客户 IAM 系列:我们关于客户(或消费者)身份和访问管理的系列博客文章,从 101 到高级主题及更深入的内容。