文档

官方文档地址:https://gitee.com/api/v5/oauth_doc#/

应用申请地址:https://gitee.com/oauth/applications

申请

image-20230304175816998

查看参数信息

image-20230304175910575

配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
RestTemplate restTemplate = new RestTemplate(factory);
// 支持中文编码
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
return restTemplate;
}

@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//单位为ms
factory.setConnectTimeout(5000);//单位为ms
return factory;
}
}
1
2
3
4
5
6
7
8
9
10
login:
gitee:
clientId:
clientSecret:
appHomeUrl: 应用首页
callbackUrl: 登录成功回调地址
# token获取地址
getTokenUrl: https://gitee.com/oauth/token
# 用户信息获取,get请求,Query请求参数【access_token getTokenUrl地址返回的access_token值 】
getUserInfoUrl: https://gitee.com/api/v5/user

使用

实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Data
public class GiteeTokenEntity {

/**
* 访问令牌
*/
@JsonProperty("access_token")
private String accessToken;

/**
* 令牌类型
*/
@JsonProperty("token_type")
private String tokenType;

/**
* 令牌过期时间
*/
@JsonProperty("expires_in")
private Long expiresIn;

/**
* 刷新token需要的token,用于延长令牌过期时间
*/
@JsonProperty("refresh_token")
private String refreshToken;

/**
* token访问范围
*/
private String scope;

/**
* 创建时间
*/
@JsonProperty("created_at")
private Long createdAt;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

@Data
public class GiteeUserEntity {

/**
* 用户id
*/
@JsonProperty("id")
private Long id;

/**
* 登录用户名,在Gitee唯一
*/
@JsonProperty("login")
private String login;

/**
* 用户信息,非真实姓名,在Gitee不唯一
*/
@JsonProperty("name")
private String name;

/**
* 头像地址
*/
@JsonProperty("avatar_url")
private String avatarUrl;

/**
* 该Entity信息访问地址
*/
@JsonProperty("url")
private String url;

/**
* gitee用户主页访问地址
*/
@JsonProperty("html_url")
private String htmlUrl;

/**
* 邮箱地址
*/
@JsonProperty("email")
private String email;

/**
* 手机号
*/
@JsonProperty("phone")
private String phone;

/**
* gitee 对应的token信息
*/
private GiteeTokenEntity giteeTokenEntity;
}

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Service
public class OAuth2ServiceImpl implements OAuth2Service {

@Autowired
private RestTemplate restTemplate;

@Value("${login.gitee.clientId}")
private String clientId;
@Value("${login.gitee.clientSecret}")
private String clientSecret;
@Value("${login.gitee.appHomeUrl}")
private String appHomeUrl;
@Value("${login.gitee.callbackUrl}")
private String callbackUrl;
@Value("${login.gitee.getTokenUrl}")
private String getTokenUrl;
@Value("${login.gitee.getUserInfoUrl}")
private String getUserInfoUrl;

/**
* 获取token
*
* @return gitee token信息
*/
@Override
public GiteeTokenEntity getToken(String code) {

Map<String, String> bodyQuery = new HashMap<>();
bodyQuery.put("grant_type", "authorization_code");
bodyQuery.put("code", code);
bodyQuery.put("client_id", clientId);
bodyQuery.put("redirect_uri", callbackUrl);
bodyQuery.put("client_secret", clientSecret);
//封装请求体信息
HttpEntity<Map<String, String>> tokenHeaderHttpEntity = new HttpEntity<>(bodyQuery);

//获取token
ResponseEntity<GiteeTokenEntity> tokenResponse = restTemplate.exchange(getTokenUrl, HttpMethod.POST, tokenHeaderHttpEntity, GiteeTokenEntity.class);
if (tokenResponse.getStatusCode() == HttpStatus.OK) {
return tokenResponse.getBody();
}
return null;
}

/**
* 获取用户信息
*
* @return gitee 用户信息
*/
@Override
public GiteeUserEntity getGiteeUser(String code) {
//获取token
GiteeTokenEntity giteeTokenEntity = this.getToken(code);

//获取用户信息
if (giteeTokenEntity != null) {
ResponseEntity<GiteeUserEntity> userResponse = restTemplate.getForEntity(getUserInfoUrl + "?access_token=" + giteeTokenEntity.getAccessToken(), GiteeUserEntity.class);
if (userResponse.getStatusCode() == HttpStatus.OK) {
GiteeUserEntity giteeUserEntity = userResponse.getBody();
if (giteeUserEntity != null) {
giteeUserEntity.setGiteeTokenEntity(giteeTokenEntity);
}
return giteeUserEntity;
}
}
return null;
}
}