1. 注册码云账户
2. 创建码云第三方应用
登录码云管理台,依次进入:“设置”/“第三方应用”/“创建应用”,填写“应用名称”、“应用描述”、“应用主页”、“应用回调地址”等关键信息,创建“码云应用”。
3. 获取码云应用令牌
完成码云应用添加后,即返回应用列表,双击对于的码云应用后,即可获取应用的令牌信息(主要关注:Client ID、Client Secret)。
4. 权限分配
5. 接入登录
设置界面同上一步骤,填写“应用主页”及“应用回调地址”,用于扫描登录后跳转回应用程序。
6. 应用系统集成
下载
**JustAuth-demo**
工程,gitee或github均可。git clone https://gitee.com/justauth/JustAuth-demo.git
修改配置参数。
- application.properties
根据实际情况修改端口、Redis等配置信息。
server.port=8443
spring.thymeleaf.cache=false
spring.devtools.restart.additional-paths=src/main/java
spring.devtools.restart.exclude=static/**,public/**
# Redis配置
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
- me.zhyd.justauth.RestAuthController
将获取的码云应用令牌信息(Client ID -> clientId、Client Secret -> clientSecret)更新至控制器,同时需要确保在开发者后台配置的回调地址与“redirectUri”保持一致。
/**
* 根据具体的授权来源,获取授权请求工具类
*/
private AuthRequest getAuthRequest(String source) {
AuthRequest authRequest = null;
switch (source.toLowerCase()) {
case "gitee":
authRequest = new AuthDingTalkRequest(AuthConfig.builder()
.clientId("${Client ID}")
.clientSecret("${Client Secret}")
.redirectUri("http://localhost:8443/oauth/callback/gitee")
.build());
break;
// ....
default:
break;
}
if (null == authRequest) {
throw new AuthException("未获取到有效的Auth配置");
}
return authRequest;
}
扫描登录处理逻辑,需根据实际需要进行修改,此处示例登录成功后跳转至第三方站点。
@RequestMapping("/callback/{source}")
public ModelAndView login(@PathVariable("source") String source, AuthCallback callback, HttpServletRequest request) {
AuthRequest authRequest = getAuthRequest(source);
AuthResponse<AuthUser> response = authRequest.login(callback);
log.info(JSONObject.toJSONString(response));
if (response.ok()) {
userService.save(response.getData());
// 示例:登录成功后跳转至第三方站点
return new ModelAndView("redirect:http://localhost:8089/bcm-szx-sati/index.html#/home");
}
Map<String, Object> map = new HashMap<>(1);
map.put("errorMsg", response.getMsg());
return new ModelAndView("error", map);
}
- 启动工程,选择“Gitee”,手机扫描二维码,进行验证。
- 响应消息。
根据响应消息,可获取**token**
(包含:**accessToken**
),之后通过这些信息可获取扫描用户更多授权信息。
{
"code": 2000,
"data": {
"avatar": "https://portrait.gitee.com//1780/****************.png",
"gender": "UNKNOWN",
"nickname": "****************",
"rawUserInfo": {
"gists_url": "https://gitee.com/api/v5/users/****************/gists{/gist_id}",
"repos_url": "https://gitee.com/api/v5/users/****************/repos",
"following_url": "https://gitee.com/following_url{/other_user}",
"created_at": "2019-09-27T17:32:42+08:00",
"remark": "",
"login": "****************",
"type": "User",
"subscriptions_url": "https:///users/****************/subscriptions",
"updated_at": "2021-11-19T13:59:02+08:00",
"id": 5341492,
"public_repos": 12,
"organizations_url": "https://gitee.com/api/v5/users/****************/orgs",
"starred_url": "https://****************/starred{/owner}{/repo}",
"followers_url": "https://gitee.com/api/v5/users/****************/followers",
"public_gists": 0,
"url": "https://gitee.com/api/v5/users/****************",
"received_events_url": "https://****************/received_events",
"watched": 48,
"followers": 1,
"avatar_url": "https:///****************.png",
"events_url": "https://gitee.com//****************/events{/privacy}",
"html_url": "https://gitee.com/****************",
"following": 3,
"name": "****************",
"stared": 0
},
"source": "GITEE",
"token": {
"accessToken": "****************",
"expireIn": 86400,
"refreshToken": "****************",
"refreshTokenExpireIn": 0,
"scope": "user_info projects **************** groups gists enterprises emails",
"tokenType": "bearer"
},
"username": "****************",
"uuid": "****************"
}
}
参考
JustAuth:集成第三方企业平台指南-码云登录
https://justauth.wiki/guide/oauth/gitee