使用 Oracle Identity Cloud Service 開發 Java 應用程式

瞭解認證流程,並瞭解 Customer Quotes 應用程式範例如何使用 Java Servlet 導入與 Oracle Identity Cloud Service 的整合。

瞭解認證流程

下列處理流程描述認證流程中的步驟,以及範例 Customer Quotes 應用程式與 Oracle Identity Cloud Service 之間的通訊。

  1. 使用者會存取「客戶報價」應用程式 (https://localhost:8181/cquotes),然後按一下以 Identity Cloud Service 登入

  2. 「客戶報價」應用程式會以下列格式準備授權代碼要求:
    • 網址:https://example.identity.oraclecloud.com/oauth2/v1/authorize?client_id=clientid&response_type=code&redirect_uri=https://localhost:8181/cquotes/return&scope=openid

    • 參數:
      • client_id:客戶引用在 Oracle Identity Cloud Service 中註冊的唯一應用程式 ID。

      • response_type:來自 Oracle Identity Cloud Service 的預期回應。在此步驟中,它是授權碼。

      • redirect_uri:使用者以 Oracle Identity Cloud Service 完成認證和授權之後,傳送授權碼的 URL。

      • scope:控制「客戶報價」應用程式可代表使用者存取與處理的資料。由於使用 OpenID Connect,因此範圍為 openid

  3. 「客戶報價」應用程式會將使用者重新導向至步驟 2 中產生的 Oracle Identity Cloud Service 授權碼 URL。

  4. Oracle Identity Cloud Service 會從「客戶報價」應用程式收到授權碼要求 (由其 client_id 識別)。

  5. Oracle Identity Cloud Service 會驗證使用者是否已經過認證。如果是,Oracle Identity Cloud Service 會略過登入程序。否則,Oracle Identity Cloud Service 會啟動登入程序並顯示登入頁面。

  6. 使用者將登入證明資料提交至 Oracle Identity Cloud Service 進行驗證。Oracle Identity Cloud Service 登入處理作業會套用密碼制定原則,直到順利驗證登入證明資料為止。

  7. 如果登入程序成功,Oracle Identity Cloud Service 會使用下列重新導向 URL 將使用者重新導向回「客戶報價」應用程式:
    • URL:

      • https://localhost:8181/cquotes/return?code=code

    • 參數:

      • code:由 Oracle Identity Cloud Service 建立的授權碼。

  8. 「客戶報價」應用程式會從要求擷取授權代碼。

  9. Customer Quotes 應用程式會直接與 Oracle Identity Cloud Service 通訊,使用下列 URL 和標頭來交換使用者存取權杖的授權代碼:
    • 網址:https://example.identity.oraclecloud.com/oauth2/v1/token?grant_type=authorization_code&code=code

    • 要求標頭:
      • Authorization=Basic ( client_id:client_secret ,編碼為 64 位元)

      • Accept=*/*

    • 參數:
      • grant_type:由於您使用 authorization_codeOracle Identity Cloud Service 要求存取權杖,因此授權類型必須是 authorization_code

      • code:使用者順利登入後,從 Oracle Identity Cloud Service 收到的授權碼。

    • 檔頭清單:
      • 授權:受信任的應用程式 client_idclient_secret (以 64 位元編碼),格式為:client_id:client_secret

      • 接受:客戶報價單應用模組預期的回應型態

        .
  10. Oracle Identity Cloud Service 會驗證要求,並將下列 JSON Web Token (JWT) 傳回「客戶報價」應用程式:
    • JWT 內容:
      • access_token:包含使用者的相關資訊。「客戶報價」應用程式可以在代表使用者進行 Oracle Identity Cloud Service API 呼叫時使用此記號。access_token 內容取決於認證處理作業期間所要求的範圍。

      • id_token:OpenID Connect 中的主要記號,用於以 scope=openid 授權端點。id_token 包含使用者的識別資訊 (例如,名稱和電子郵件)。用戶端應用程式可以將此資訊用於數種用途,包括驗證和顯示內容。合法的 (由從屬端根據 OpenID Connect 提供者簽章驗證) 和作用中的 id_token 會告知應用程式使用者已認證且具有有效的記號。

  11. 「客戶報價」應用程式會處理 JWT 權杖 (id_token),然後擷取 Oracle Identity Cloud Service 傳回的使用者資訊,例如名稱和電子郵件。

  12. 「客戶報價」應用程式會顯示包含使用者相關資訊的首頁,例如名稱和電子郵件。

瞭解 Java 應用程式程式碼

Customer Quotes 應用程式使用 Servlet 技術的範例。

應用程式由下列主要 Servlet 所組成:
  • com.example.servlet.AccessResourceServlet:將使用者重新導向至 Oracle Identity Cloud Service 以要求授權碼,以起始認證流程。

  • com.example.servlet.ReturnServlet:處理來自 Oracle Identity Cloud Service 的重新導向 URL、接收授權碼,以及使用 com.example.utils.OICOAuthClient 類別來交換識別記號和存取權杖的授權碼。

  • com.example.servlet.LogoutServlet:終止應用程式的使用者階段作業,但不會從 Oracle Identity Cloud Service 登出使用者。

Servlet 使用下列公用程式類別:
  • com.example.utils.OICOAuthClient:建構 Oracle Identity Cloud Service REST API 的 URL 端點、處理要求、剖析 Oracle Identity Cloud Service 回應,以及將使用者資訊新增至應用程式的 HTTP 階段作業。

  • com.example.utils.HttpUtil:處理與 Oracle Identity Cloud Service REST API 端點的 HTTP 通訊。從 Customer Quotes 應用程式到 Oracle Identity Cloud Service 的所有直接通訊都透過 java.net.HttpURLConnection 類別進行。