UCenter 是 Discuz! 旗下用戶中心,提供**統一註冊、登錄、登出、用戶資料同步**等 API。Java 應用作為「應用客戶端」與 UCenter Server (PHP) 通訊,實現單點登入 (SSO)。
<!-- Maven (從開源項目編譯安裝至本地倉庫) -->
<dependency>
<groupId>com.fivestars.interfaces</groupId>
<artifactId>discuz-ucenter-api</artifactId>
<version>1.0</version>
</dependency>
或下載 uc-client-java.jar 放入 WEB-INF/lib。
# 放在 src/main/resources/config.properties
UC_API=https://localhost/uc_server
UC_IP=127.0.0.1
UC_KEY=your_uc_key_here
UC_APPID=2
UC_CONNECT= # 空為 fsockopen, 'mysql' 為資料庫直連
UC_CHARSET=UTF-8
UC_DBTABLEPRE=`uc_`
UC_DBCHARSET=utf8
<!-- web.xml -->
<servlet>
<servlet-name>uc_api</servlet-name>
<servlet-class>com.fivestars.interfaces.bbs.api.UC</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>uc_api</servlet-name>
<url-pattern>/api/uc.php</url-pattern>
</servlet-mapping>
此 Servlet 處理 UCenter 發起的同步登出、刪除用戶等回調通知。
<%@ page language="java" pageEncoding="UTF-8" import="com.fivestars.interfaces.bbs.client.Client, com.fivestars.interfaces.bbs.util.XMLHelper, java.util.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String message = "";
if (username != null && password != null) {
Client uc = new Client();
// 1. 調用 UCenter 驗證用戶
String result = uc.uc_user_login(username, password);
LinkedList rs = XMLHelper.uc_unserialize(result);
if (rs.size() > 0) {
int uid = Integer.parseInt(rs.get(0));
String uname = rs.get(1);
String pwd = rs.get(2);
String email = rs.get(3);
if (uid > 0) {
// 2. 登錄成功:生成同步登錄 JS 代碼
String synloginJS = uc.uc_user_synlogin(uid);
// 3. 本地系統登錄邏輯 (建立 Session、寫 Cookie 等)
session.setAttribute("user", new User(uid, uname, email));
// 4. 寫入 UCenter 認證 Cookie (跨域同步用)
Cookie authCookie = new Cookie("auth", uc.uc_authcode(pwd + "\t" + uid, "ENCODE"));
authCookie.setMaxAge(31536000); // 1年
authCookie.setPath("/");
response.addCookie(authCookie);
message = "登錄成功!" + synloginJS; // synloginJS 包含 document.write 跨域 iframe
} else if (uid == -1) message = "用戶不存在";
else if (uid == -2) message = "密碼錯誤";
else message = "未知錯誤";
} else {
message = "通訊失敗: " + result;
}
}
%>
<!DOCTYPE html>
<html><head><meta charset="UTF-8"><title>統一登錄</title></head>
<body>
<h2>Discuz! UCenter 同步登錄</h2>
<p style="color:red;"><%=message%></p>
<form method="post">
用戶名: <input name="username" required><br>
密碼: <input type="password" name="password" required><br>
<button type="submit">登錄</button>
</form>
</body></html>
<%@ page import="com.fivestars.interfaces.bbs.client.Client" %>
<%
Client uc = new Client();
// 1. 本地銷毀 Session
session.invalidate();
// 2. 調用 UCenter 同步登出 (生成 JS 代碼)
String synlogoutJS = uc.uc_user_synlogout();
// 3. 清除 UCenter Cookie
Cookie c = new Cookie("auth", "");
c.setMaxAge(0); c.setPath("/");
response.addCookie(c);
%>
<html><body>
<h3>已登出</h3>
<%=synlogoutJS%> <!-- 此 JS 會觸發跨域請求登出 Discuz 論壇 --%>
<a href="login.jsp">重新登錄</a>
</body></html>
String result = uc.uc_user_register(username, password, email);
// 返回 uid > 0 成功,-1 用戶名非法,-2 含保留詞,-3 用戶名存在,-4 Email 非法,-5 Email 被佔用,-6 已被禁止註冊
// 核心:基於 Key 的對稱加密 (類似 Discuz authcode)
public String uc_authcode(String string, String operation, String key, int expiry) {
// ... 實現細節見 API 源碼
}
| 問題 | 原因 | 解決 |
|---|---|---|
| 通訊失敗/返回空 | UC_API URL 錯誤、防火牆阻擋、PHP 錯誤 | 檢查網路連通、PHP 錯誤日誌、UC_API 可直接瀏覽器訪問 |
| 簽名驗證失敗 | UC_KEY 不一致、字符集不一致 | 確認雙端 Key 完全一致、均用 UTF-8 |
| 同步登錄不生效 | Cookie 域/路徑不匹配、瀏覽器阻擋第三方 Cookie | 設定 Cookie Domain 為主域、SameSite=Lax、Secure |
| 中文用戶名亂碼 | 編碼不一致 | 配置 UC_CHARSET=UTF-8、資料庫 utf8mb4、Java 端 UTF-8 |
Java 整合 Discuz UCenter 核心流程:**配置連線參數 → 註冊回調 Servlet → 調用 Client API 登錄/註冊/登出 → 處理同步 JS 代碼 → 本地 Session 管理**。雖 UCenter 為舊技術,但在存量 Discuz 專案遷移、雙系統並行期仍具實用價值。新架構建議遷移至標準 OAuth2/OIDC 體系。