{"id":23251346,"url":"https://github.com/renfei/discuz-ucenter-api-for-java","last_synced_at":"2025-08-20T09:31:15.789Z","repository":{"id":41237129,"uuid":"321956565","full_name":"renfei/discuz-ucenter-api-for-java","owner":"renfei","description":"最完美的Discuz UCenter的JAVA API接口，简单易用，完全免费！","archived":false,"fork":false,"pushed_at":"2023-05-08T07:03:31.000Z","size":153,"stargazers_count":17,"open_issues_count":7,"forks_count":8,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-17T09:07:47.470Z","etag":null,"topics":["api","bbs","comsenz","discuz","java","sso","ucenter","uhome"],"latest_commit_sha":null,"homepage":"https://www.renfei.net/posts/1003429","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/renfei.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-16T11:08:24.000Z","updated_at":"2024-09-10T06:16:01.000Z","dependencies_parsed_at":"2022-09-01T10:50:25.535Z","dependency_job_id":null,"html_url":"https://github.com/renfei/discuz-ucenter-api-for-java","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renfei%2Fdiscuz-ucenter-api-for-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renfei%2Fdiscuz-ucenter-api-for-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renfei%2Fdiscuz-ucenter-api-for-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renfei%2Fdiscuz-ucenter-api-for-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/renfei","download_url":"https://codeload.github.com/renfei/discuz-ucenter-api-for-java/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230408135,"owners_count":18220977,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","bbs","comsenz","discuz","java","sso","ucenter","uhome"],"created_at":"2024-12-19T09:17:56.026Z","updated_at":"2024-12-19T09:17:56.863Z","avatar_url":"https://github.com/renfei.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# discuz-ucenter-api-for-java\n\n\u003ca href=\"https://search.maven.org/search?q=g:%22net.renfei%22%20AND%20a:%22discuz-ucenter-api-for-java%22\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/maven-central/v/net.renfei/discuz-ucenter-api-for-java.svg?label=Maven%20Central\" alt=\"Latest Stable Version\"/\u003e\u003c/a\u003e[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Frenfei%2Fdiscuz-ucenter-api-for-java.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Frenfei%2Fdiscuz-ucenter-api-for-java?ref=badge_shield)\n\n最完美的Discuz UCenter的JAVA API接口，简单易用，完全免费！\n\n相关博文：[https://www.renfei.net/posts/1003429](https://www.renfei.net/posts/1003429)\n\n社区论坛：[https://bbs.renfei.net/forum-44-1.html](https://bbs.renfei.net/forum-44-1.html)\n\n## 说明\n\n此项目移植自[https://code.google.com/archive/p/discuz-ucenter-api-for-java](https://code.google.com/archive/p/discuz-ucenter-api-for-java) / [https://github.com/liangping/dzclient4j](https://github.com/liangping/dzclient4j)，感谢原作者[梁平 (no_ten@163.com)](https://github.com/liangping)将他的代码开源。\n\n由于原有项目已经非常古老了，看时间应该是2009年的产物，但现在的项目都使用```Maven```和```SpringBoot```进行构建，里面直接配置```servlet```的方式不太适合现代项目集成，我就拿来进行了一些修改，让使用```Maven```和```SpringBoot```的程序更容易集成进去。\n\n#### 关于 GPL-2.0 License\n\nGPLv2具有很强的传染性，选用这个开源协议并不是我定的，而是原项目作者在开源时就选用了 GPLv2，我也只能跟随原开源协议继续传染下去。\n\n当然，如果您并不注重什么版权协议，那可以无视。\n\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Frenfei%2Fdiscuz-ucenter-api-for-java.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Frenfei%2Fdiscuz-ucenter-api-for-java?ref=badge_large)\n\n#### 关于程序包名修改\n\n也许您注意到源程序的程序包名由```com.fivestars```被我改为了```net.renfei.discuz.ucenter```，这是因为要发布到Maven中央仓库，在注册Maven中央仓库的时候会要求验证域名所有权，并且数字签名，所以我只能发布```net.renfei```的库，为了在项目中不产生混淆，我就直接将源码中的包名也一起统一修改了。\n\n#### 必要条件\n\n您的项目环境需要是```JDK1.8```以上。其中```Base64```使用了```Java8```自带的库实现，移除了原作者自己写的```Base64```算法。同时我的编译环境也是在```Java8```中编译发布的。\n\n## 安装\n\n我使用的是```Maven```和```SpringBoot```进行演示。在```pom.xml```文件中添加依赖：\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003enet.renfei\u003c/groupId\u003e\n  \u003cartifactId\u003ediscuz-ucenter-api-for-java\u003c/artifactId\u003e\n  \u003cversion\u003e1.0.7\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n如果你使用的是传统方式，需要下载```Jar```包，放入```lib```文件夹，下载地址：[discuz-ucenter-api-for-java-1.0.7.jar\n](https://github.com/renfei/discuz-ucenter-api-for-java/releases/download/1.0.7/discuz-ucenter-api-for-java-1.0.7.jar)\n\n## 使用\n\n为了跟```SpringBoot```更好的集成，我对原项目进行了修改，原项目是修改配置文件，我修改为了实例化时传递参数的方式\n\n### Https(SSL) 站点\n如果你的站点是```https```的，必须使用版本号```1.0.6```及以上，旧版本的作者没有考虑```https```，我修改了代码兼容了```https```。\n\n### 实例化\n\n实例化一个客户端```net.renfei.discuz.ucenter.client.Client```，参数依次是：UCenter接口地址、IP地址、通讯Key、APPID、Connect。\n\n```java\nClient client = new Client(\"http://localhost/uc_server\", null, \"key\", \"2\",\"\");\n```\n\n### 客户端\n\n旧的源代码中是通过配置文件注册一个```servlet```，我改造的是用于```SpringBoot```项目的，所以通过配置文件注册```servlet```不是很方便，而且为了尽量降低代码入侵性，我就改为自己使用```Controller```处理```HttpServletRequest```和```HttpServletResponse```的方式。\n\n先创建一个```Controller```，然后创建一个处理```HttpServletRequest```和```HttpServletResponse```的方法，给一个```UCenter```请求的地址```@RequestMapping(\"/api/uc.php\")```，实例化一个客户端```net.renfei.discuz.ucenter.api.UCClient```和```net.renfei.discuz.ucenter.client.Client```，然后把```HttpServletRequest```交给```net.renfei.discuz.ucenter.api.UCClient.doAnswer()```去处理，最后将结果写入```HttpServletResponse```，如果```UCenter```配置正确，就应该可以在```UCenter```看到通讯正常了。具体使用如下案例：\n\n```Java\n@Controller\npublic class UCenterController {\n\n    @ResponseBody\n    @RequestMapping(\"/api/uc.php\")\n    public void uc(HttpServletRequest request, HttpServletResponse response) throws IOException {\n        UCClient ucClient = new UCClient();\n        Client client = new Client(\"http://localhost/uc_server\", null, \"key\", \"2\",\"\");\n        String result = ucClient.doAnswer(client, request, response);\n        response.getWriter().print(result);\n    }\n}\n```\n\n如果您想自己处理一些动作的逻辑，只需要继承```net.renfei.discuz.ucenter.api.UCClient```然后重写覆盖里面的```doAnswer(Client client, HttpServletRequest request, HttpServletResponse response)```方法即可。\n\n#### 注册\n```java\nClient client = new Client(\"http://localhost/uc_server\", null, \"key\", \"2\",\"\");\nString string = client.ucUserRegister(\"username\",\"password\",\"email\");\n```\n\n#### 登陆\n```java\nClient client = new Client(\"http://localhost/uc_server\", null, \"key\", \"2\",\"\");\nString string = client.ucUserLogin(\"username\",\"password\");\n```\n\n#### 同步登陆\n```java\nClient client = new Client(\"http://localhost/uc_server\", null, \"key\", \"2\",\"\");\nint UID = 21; //此处是用户的UID\nString string = client.ucUserSynlogin(uid);\n```\n\n#### 先登陆再同步登陆\n```java\nClient client = new Client(\"http://localhost/uc_server\", null, \"key\", \"2\",\"\");\n// 登陆\nString result = client.ucUserLogin(uid);\nLinkedList\u003cString\u003e rs = XMLHelper.ucUnserialize(result);\nif(rs.size() \u003e 0){\n\tint uid = Integer.parseInt(rs.get(0));\n\tString username = rs.get(1);\n\tString password = rs.get(2);\n\tString email = rs.get(3);\n\tif(uid \u003e 0) {\n\t\t//同步登陆\n\t\tString string = client.ucUserSynlogin(uid);\n\t\t//本地登陆代码\n\t\t//TODO ... ....\n\t} else if(uid == -1) {\n\t\tSystem.out.println(\"用户不存在,或者被删除\");\n\t} else if(uid == -2) {\n\t\tSystem.out.println(\"密码错\");\n\t} else {\n\t\tSystem.out.println(\"未定义\");\n\t}\n}else{\n\tSystem.out.println(\"Login failed\");\n\tSystem.out.println(result);\n}\n```\n\n## 常见问题\n在网友使用中出现问题，和我们一起讨论交流的结果将在 [https://www.renfei.net/posts/1003429](https://www.renfei.net/posts/1003429) 文章末尾常见问题中更新补充。\n\n## 代码仓库\n- [Main] Github:[https://github.com/renfei/discuz-ucenter-api-for-java](https://github.com/renfei/discuz-ucenter-api-for-java)\n- [Mirror] Gitlab:[https://gitlab.com/renfei/discuz-ucenter-api-for-java](https://gitlab.com/renfei/discuz-ucenter-api-for-java)\n- [Mirror] Gitee:[https://gitee.com/rnf/discuz-ucenter-api-for-java](https://gitee.com/rnf/discuz-ucenter-api-for-java)\n- [Mirror] Coding:[https://githubi.coding.net/public/renfei/discuz-ucenter-api-for-java/git/files](https://githubi.coding.net/public/renfei/discuz-ucenter-api-for-java/git/files)\n\n## 更多信息\n\n更多信息请阅读源代码，此处不再一一演示。您可以提出[issues](https://github.com/renfei/discuz-ucenter-api-for-java/issues)或者到我的社区论坛一起讨论：[https://bbs.renfei.net/forum-44-1.html](https://bbs.renfei.net/forum-44-1.html)\n\n## 求鼓励\n\n如果这个项目帮助到了你，是否能给我点个免费的星星 (Star) 给个鼓励呢。高星项目我将持续关注努力更新的。","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frenfei%2Fdiscuz-ucenter-api-for-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frenfei%2Fdiscuz-ucenter-api-for-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frenfei%2Fdiscuz-ucenter-api-for-java/lists"}