Xác thực bằng SAML trong các ứng dụng web
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Nếu đã nâng cấp lên Firebase Authentication with Identity Platform, bạn có thể xác thực người dùng bằng Firebase
thông qua nhà cung cấp danh tính SAML mà bạn chọn. Nhờ vậy, bạn có thể sử dụng
giải pháp đăng nhập một lần (SSO) dựa trên SAML để giúp người dùng đăng nhập vào ứng dụng Firebase của bạn.
Firebase Authentication chỉ hỗ trợ quy trình SAML do nhà cung cấp dịch vụ khởi tạo.
Trước khi bắt đầu
Để người dùng đăng nhập bằng nhà cung cấp danh tính SAML, trước tiên, bạn phải thu thập
của nhà cung cấp:
- Mã nhận dạng thực thể của nhà cung cấp: URI xác định nhà cung cấp danh tính.
- URL đăng nhập một lần dựa trên SAML của nhà cung cấp: URL của trang đăng nhập của nhà cung cấp danh tính
.
- Chứng chỉ khoá công khai của nhà cung cấp: Chứng chỉ dùng để xác thực
mã thông báo do nhà cung cấp danh tính ký.
- Mã nhận dạng thực thể của ứng dụng: URI xác định ứng dụng của bạn, ví dụ:
".
Sau khi có thông tin nêu trên, hãy bật SAML làm nhà cung cấp dịch vụ đăng nhập cho
Dự án Firebase:
Thêm Firebase vào dự án JavaScript của bạn.
Nếu bạn chưa nâng cấp lên Firebase Authentication with Identity Platform, hãy nâng cấp lên. Chỉ xác thực SAML
có sẵn trong các dự án đã nâng cấp.
Trên trang Nhà cung cấp dịch vụ đăng nhập
của bảng điều khiển Firebase, nhấp vào Thêm nhà cung cấp mới, sau đó nhấp vào
SAML.
Đặt tên cho nhà cung cấp này. Lưu ý mã nhà cung cấp được tạo:
chẳng hạn như saml.example-provider
. Bạn sẽ cần mã này khi thêm
vào ứng dụng của mình.
Chỉ định mã nhận dạng thực thể, URL SSO và khoá công khai của nhà cung cấp danh tính
chứng chỉ. Đồng thời, hãy chỉ định mã nhận dạng thực thể của ứng dụng (nhà cung cấp dịch vụ).
Các giá trị này phải khớp chính xác với giá trị mà nhà cung cấp đã chỉ định cho bạn.
Lưu thay đổi.
Nếu bạn chưa uỷ quyền miền của ứng dụng, hãy thêm miền đó vào danh sách cho phép
trên danh sách Xác thực > Cài đặt
của bảng điều khiển Firebase.
Xử lý quy trình đăng nhập bằng Firebase SDK
Để xử lý quy trình đăng nhập bằng SDK JavaScript của Firebase, hãy làm theo các bước sau
các bước:
Tạo một thực thể của SAMLAuthProvider
bằng mã nhà cung cấp mà bạn có trong đó
bảng điều khiển của Firebase.
Web
import { SAMLAuthProvider } from "firebase/auth";
const provider = new SAMLAuthProvider('saml.example-provider');
Web
var provider = new firebase.auth.SAMLAuthProvider('saml.example-provider');
``
Xác thực bằng Firebase bằng đối tượng nhà cung cấp SAML.
Bạn có thể chuyển hướng người dùng đến trang đăng nhập của nhà cung cấp hoặc mở
trong cửa sổ trình duyệt bật lên.
Quy trình chuyển hướng
Chuyển hướng đến trang đăng nhập của nhà cung cấp bằng cách gọi signInWithRedirect()
:
Web
import { getAuth, signInWithRedirect } from "firebase/auth";
const auth = getAuth();
signInWithRedirect(auth, provider);
Web
firebase.auth().signInWithRedirect(provider);
Sau khi người dùng hoàn tất đăng nhập và quay lại ứng dụng của bạn, bạn có thể nhận được
bằng cách gọi getRedirectResult()
.
Web
import { getAuth, getRedirectResult, SAMLAuthProvider } from "firebase/auth";
const auth = getAuth();
getRedirectResult(auth)
.then((result) => {
// User is signed in.
// Provider data available using getAdditionalUserInfo()
})
.catch((error) => {
// Handle error.
});
Web
firebase.auth().getRedirectResult()
.then((result) => {
// User is signed in.
// Provider data available in result.additionalUserInfo.profile,
// or from the user's ID token obtained from result.user.getIdToken()
// as an object in the firebase.sign_in_attributes custom claim.
})
.catch((error) => {
// Handle error.
});
Quy trình bật lên
Web
import { getAuth, signInWithPopup, OAuthProvider } from "firebase/auth";
const auth = getAuth();
signInWithPopup(auth, provider)
.then((result) => {
// User is signed in.
// Provider data available in result.additionalUserInfo.profile,
// or from the user's ID token obtained from result.user.getIdToken()
// as an object in the firebase.sign_in_attributes custom claim.
})
.catch((error) => {
// Handle error.
});
Web
firebase.auth().signInWithPopup(provider)
.then((result) => {
// User is signed in.
// Provider data available in result.additionalUserInfo.profile,
// or from the user's ID token obtained from result.user.getIdToken()
// as an object in the firebase.sign_in_attributes custom claim.
})
.catch((error) => {
// Handle error.
});
Mã thông báo giá trị nhận dạng và UserInfo
đối tượng chỉ chứa địa chỉ email của người dùng nếu địa chỉ đó được cung cấp trong
Thuộc tính NameID
của nội dung xác nhận SAML từ nhà cung cấp danh tính:
<Subject>
<NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">test@email.com</NameID>
</Subject>
Mặc dù các ví dụ ở trên tập trung vào quy trình đăng nhập, bạn có thể sử dụng cùng một
để liên kết nhà cung cấp SAML với người dùng hiện có bằng
linkWithRedirect()
và linkWithPopup()
, đồng thời xác thực lại người dùng bằng
reauthenticateWithRedirect()
và reauthenticateWithPopup()
, có thể là
dùng để truy xuất thông tin đăng nhập mới cho những hoạt động nhạy cảm cần đến
lần đăng nhập gần đây.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-08 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-08 UTC."],[],[],null,["# Authenticate Using SAML in web apps\n\nIf you've upgraded to Firebase Authentication with Identity Platform, you can authenticate your users with Firebase\nusing the SAML identity provider of your choice. This makes it possible to use\nyour SAML-based SSO solution to sign users in to your Firebase app.\n\nFirebase Authentication supports only the service-provider initiated SAML flow.\n\nBefore you begin\n----------------\n\nTo sign in users using a SAML identity provider, you must first collect some\ninformation from the provider:\n\n- **The provider's Entity ID**: A URI that identifies the identity provider.\n- **The provider's SAML SSO URL**: The URL of the identity provider's sign-in page.\n- **The provider's public key certificate**: The certificate used to validate tokens signed by the identity provider.\n- **Your app's Entity ID**: A URI that identifies your app, the \"service provider\".\n\nAfter you have the above information, enable SAML as a sign-in provider for your\nFirebase project:\n\n1. [Add Firebase to your JavaScript project](/docs/web/setup).\n\n2. If you haven't upgraded to Firebase Authentication with Identity Platform, do so. SAML authentication is only\n available in upgraded projects.\n\n3. On the [**Sign-in providers**](//console.firebase.google.com/project/_/authentication/providers)\n page of the Firebase console, click **Add new provider** , and then click\n **SAML**.\n\n4. Give a name to this provider. Note the provider ID that's generated:\n something like `saml.example-provider`. You'll need this ID when you add\n sign-in code to your app.\n\n5. Specify your identity provider's entity ID, SSO URL, and public key\n certificate. Also specify the entity ID of your app (the service provider).\n These values must exactly match the values your provider assigned to you.\n\n6. Save your changes.\n\n7. If you haven't already authorized your app's domain, add it to the allow\n list on the [**Authentication \\\u003e Settings**](//console.firebase.google.com/project/_/authentication/settings)\n page of the Firebase console.\n\nHandle the sign-in flow with the Firebase SDK\n---------------------------------------------\n\nTo handle the sign-in flow with the Firebase JavaScript SDK, follow these\nsteps:\n\n1. Create an instance of an `SAMLAuthProvider` using the provider ID you got in\n the Firebase console.\n\n ### Web\n\n import { SAMLAuthProvider } from \"firebase/auth\";\n\n const provider = new SAMLAuthProvider('saml.example-provider');\n\n ### Web\n\n var provider = new firebase.auth.SAMLAuthProvider('saml.example-provider');\n ``\n\n\u003c!-- --\u003e\n\n1. Authenticate with Firebase using the SAML provider object.\n\n You can either redirect the user to the provider's sign-in page or open the\n sign-in page in a pop-up browser window.\n\n **Redirect flow**\n\n Redirect to the provider sign-in page by calling `signInWithRedirect()`: \n\n ### Web\n\n import { getAuth, signInWithRedirect } from \"firebase/auth\";\n\n const auth = getAuth();\n signInWithRedirect(auth, provider);\n\n ### Web\n\n firebase.auth().signInWithRedirect(provider);\n\n After the user completes sign-in and returns to your app, you can obtain the\n sign-in result by calling `getRedirectResult()`. \n\n ### Web\n\n import { getAuth, getRedirectResult, SAMLAuthProvider } from \"firebase/auth\";\n\n const auth = getAuth();\n getRedirectResult(auth)\n .then((result) =\u003e {\n // User is signed in.\n\n // Provider data available using getAdditionalUserInfo()\n })\n .catch((error) =\u003e {\n // Handle error.\n });\n\n ### Web\n\n firebase.auth().getRedirectResult()\n .then((result) =\u003e {\n // User is signed in.\n\n // Provider data available in result.additionalUserInfo.profile,\n // or from the user's ID token obtained from result.user.getIdToken()\n // as an object in the firebase.sign_in_attributes custom claim.\n })\n .catch((error) =\u003e {\n // Handle error.\n });\n\n **Pop-up flow**\n\n ### Web\n\n import { getAuth, signInWithPopup, OAuthProvider } from \"firebase/auth\";\n\n const auth = getAuth();\n signInWithPopup(auth, provider)\n .then((result) =\u003e {\n // User is signed in.\n\n // Provider data available in result.additionalUserInfo.profile,\n // or from the user's ID token obtained from result.user.getIdToken()\n // as an object in the firebase.sign_in_attributes custom claim.\n })\n .catch((error) =\u003e {\n // Handle error.\n });\n\n ### Web\n\n firebase.auth().signInWithPopup(provider)\n .then((result) =\u003e {\n // User is signed in.\n\n // Provider data available in result.additionalUserInfo.profile,\n // or from the user's ID token obtained from result.user.getIdToken()\n // as an object in the firebase.sign_in_attributes custom claim.\n })\n .catch((error) =\u003e {\n // Handle error.\n });\n\n The ID token and [UserInfo](/docs/reference/js/auth.userinfo#userinfoemail)\n object contains the user's email address only if it is provided in the\n `NameID` attribute of the SAML assertion from the identity provider: \n\n \u003cSubject\u003e\n \u003cNameID Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress\"\u003etest@email.com\u003c/NameID\u003e\n \u003c/Subject\u003e\n\n2. While the above examples focus on sign-in flows, you can use the same\n pattern to link a SAML provider to an existing user using\n `linkWithRedirect()` and `linkWithPopup()`, and re-authenticate a user with\n `reauthenticateWithRedirect()` and `reauthenticateWithPopup()`, which can be\n used to retrieve fresh credentials for sensitive operations that require\n recent login."]]