文档
官方文档地址:https://gitee.com/api/v5/oauth_doc#/
应用申请地址:https://gitee.com/oauth/applications
申请

查看参数信息

配置类
| 12
 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);
 factory.setConnectTimeout(5000);
 return factory;
 }
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | login:gitee:
 clientId:
 clientSecret:
 appHomeUrl: 应用首页
 callbackUrl: 登录成功回调地址
 
 getTokenUrl: https://gitee.com/oauth/token
 
 getUserInfoUrl: https://gitee.com/api/v5/user
 
 | 
使用
实体类
| 12
 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
 
 | @Datapublic class GiteeTokenEntity {
 
 
 
 
 @JsonProperty("access_token")
 private String accessToken;
 
 
 
 
 @JsonProperty("token_type")
 private String tokenType;
 
 
 
 
 @JsonProperty("expires_in")
 private Long expiresIn;
 
 
 
 
 @JsonProperty("refresh_token")
 private String refreshToken;
 
 
 
 
 private String scope;
 
 
 
 
 @JsonProperty("created_at")
 private Long createdAt;
 }
 
 | 
| 12
 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 {
 
 
 
 
 @JsonProperty("id")
 private Long id;
 
 
 
 
 @JsonProperty("login")
 private String login;
 
 
 
 
 @JsonProperty("name")
 private String name;
 
 
 
 
 @JsonProperty("avatar_url")
 private String avatarUrl;
 
 
 
 
 @JsonProperty("url")
 private String url;
 
 
 
 
 @JsonProperty("html_url")
 private String htmlUrl;
 
 
 
 
 @JsonProperty("email")
 private String email;
 
 
 
 
 @JsonProperty("phone")
 private String phone;
 
 
 
 
 private GiteeTokenEntity giteeTokenEntity;
 }
 
 | 
使用
| 12
 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;
 
 
 
 
 
 
 @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);
 
 
 ResponseEntity<GiteeTokenEntity> tokenResponse = restTemplate.exchange(getTokenUrl, HttpMethod.POST, tokenHeaderHttpEntity, GiteeTokenEntity.class);
 if (tokenResponse.getStatusCode() == HttpStatus.OK) {
 return tokenResponse.getBody();
 }
 return null;
 }
 
 
 
 
 
 
 @Override
 public GiteeUserEntity getGiteeUser(String code) {
 
 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;
 }
 }
 
 |