首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 實現 OAuth 2.0 安全授權完整指南:從協議原理到代碼落地

     編輯:Java 相關知識

OAuth 2.0 授權框架 Java 實戰全解析

OAuth 2.0 是業界標準的授權框架,允許第三方應用在用戶授權下訪問受保護資源,而無需獲取用戶憑證。本文從協議流程、核心術語、Java 手動實現三個維度深度講解。

核心角色與術語

角色說明
Resource Owner (用戶)擁有受保護資源的實體
Client (客戶端/第三方應用)請求訪問資源的應用
Authorization Server (授權服務器)驗證用戶並頒發 Access Token
Resource Server (資源服務器)存儲受保護資源,驗證 Token

關鍵參數:client_id、client_secret、redirect_uri、scope、state(防 CSRF)、code(授權碼)、access_token、refresh_token。

授權碼流程(最經典、最安全)

+----------+                               +---------------+
|   User   |                               |  Auth Server  |
+----------+                               +---------------+
     |                                           |
     |  1. 點擊「授權登錄」                       |
     |------------------------------------------>|
     |                                           |
     |  2. 重定向到授權頁面                       |
     |<------------------------------------------|
     |                                           |
     |  3. 用戶登錄並授權                         |
     |------------------------------------------>|
     |                                           |
     |  4. 重定向回 Client 帶 code               |
     |<------------------------------------------|
     |                                           |
     |  5. Client 用 code 換 access_token        |
     |------------------------------------------>|
     |                                           |
     |  6. 返回 access_token (+ refresh_token)   |
     |<------------------------------------------|
     |                                           |
     |  7. 攜帶 token 訪問資源                    |
     |------------------------------------------>|

Java 手動實現關鍵步驟(教學用,生產建議用成熟庫)

1. 獲取 Request Token(OAuth 1.0 概念,2.0 直接獲取授權碼)

構建基礎字符串,HMAC-SHA1 簽名:

String baseString = "GET&" + urlEncode(authUrl) + "&" + urlEncode(params);
// 簽名密鑰 = consumer_secret + "&" + token_secret (若有)
String signature = hmacSha1(baseString, signingKey);

2. 用戶授權重定向

String authUrl = "https://provider.com/oauth/authorize?" +
    "client_id=" + clientId +
    "&redirect_uri=" + urlEncode(redirectUri) +
    "&response_type=code" +
    "&scope=read_write" +
    "&state=" + randomState; // 必須驗證回調時的 state

3. 授權碼換 Access Token(後端調用,保護 client_secret)

HttpRequest.post(tokenUrl)
    .form("grant_type=authorization_code")
    .form("code=" + authCode)
    .form("redirect_uri=" + redirectUri)
    .form("client_id=" + clientId)
    .form("client_secret=" + clientSecret)
    .execute()
    .body(); // 解析 JSON 獲取 access_token, expires_in, refresh_token

4. 攜帶 Token 訪問資源

HttpRequest.get(resourceUrl)
    .header("Authorization", "Bearer " + accessToken)
    .execute();

Token 刷新與撤銷

// 刷新
POST /oauth/token
grant_type=refresh_token
&refresh_token=xxx
&client_id=xxx
&client_secret=xxx

// 撤銷 (RFC 7009)
POST /oauth/revoke
token=xxx
&token_type_hint=access_token
&client_id=xxx
&client_secret=xxx

安全最佳實踐

  • PKCE (RFC 7636):公共客戶端(SPA、移動端)必須使用,防授權碼攔截
  • State 參數:綁定用戶會話,防 CSRF
  • Token 存儲:Access Token 內存短期持有,Refresh Token 加密持久化
  • HTTPS 強制:全鏈路加密傳輸
  • Scope 最小化:按需申請權限

推薦成熟庫

  • Spring Security OAuth2:Spring 生態標準選擇
  • Apache Oltu:輕量級 OAuth 客戶端/服務端實現
  • OkHttp + JSON:手動實現時的網絡層基礎
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved