Skip to content

Commit 1540b6b

Browse files
committed
获取用户信息
1 parent a9a34c8 commit 1540b6b

File tree

10 files changed

+224
-2
lines changed

10 files changed

+224
-2
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
# Spring boot
2-
练习
3-
前端框架 https://v3.bootcss.com/getting-started/#download
2+
练习资料
3+
>[Spring Boot 文档](https://spring.io/quides)
4+
>[Spring Web](https://spring.io/quides/gs/serving-web-content)
5+
>[Bootstrap 前端框架](https://v3.bootcss.com/getting-started/#download)
6+
>[ES](https://elasticsearch.cn/explore)
7+
>[GitHub OAuth2](https://devoloper.github.com/aoos/building-oauth-apps/creating-an-oauth-app/)
8+
>[Okhttp](https://square.github.io/okhttp)
9+
10+
工具
11+
>[Git](https://github.com)
12+
>[Visual Paradigm](https://www.visual-paradigm.com)
13+
14+
> # Git 使用
15+
> ```shell script
16+
> git init # 初始化本地仓库
17+
> git add . # 添加当前目录文件
18+
> git state # 查看状态
19+
> git commit -m "备注" # 提交到本地仓库
20+
> git push origin master # 推送到名为origin的远程仓库的master分支
21+
> git pull origin master # 拉取名为origin的远程仓库的master分支
22+
> ```
23+
24+
## 进度
25+
> 1. `GitHub`登录之调用 `authorize`
26+
> 2. `GitHub`登录之获取`code`
27+
>
428

Wiki.md

Whitespace-only changes.

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
</exclusion>
4141
</exclusions>
4242
</dependency>
43+
<dependency>
44+
<groupId>com.squareup.okhttp3</groupId>
45+
<artifactId>okhttp</artifactId>
46+
<version>4.2.2</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.alibaba</groupId>
50+
<artifactId>fastjson</artifactId>
51+
<version>1.2.62</version>
52+
</dependency>
53+
4354
</dependencies>
4455

4556
<build>

src/main/java/life/CommunityApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package life;
22

3+
/*
4+
* Spring Boot 主函数入口
5+
*/
6+
37
import org.springframework.boot.SpringApplication;
48
import org.springframework.boot.autoconfigure.SpringBootApplication;
59

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package life.majiang.community.controller;
2+
3+
/*
4+
* 获取code
5+
*/
6+
7+
import life.majiang.community.dto.AccessTokenDTO;
8+
import life.majiang.community.provider.GithubProvider;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Controller;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.RequestParam;
13+
14+
@Controller // 把类作为路由Api的承载者
15+
public class AuthorizeController {
16+
17+
@Autowired // 把spring容器的实例加载到当前上下文
18+
private GithubProvider githubProvider;
19+
20+
21+
@GetMapping("/callback") // 登录成功后返回到登录页
22+
// 接收返回后的参数 code, state
23+
public String callback(@RequestParam(name = "code") String code,
24+
@RequestParam(name = "state") String state) {
25+
AccessTokenDTO accessTokenDTO = new AccessTokenDTO();
26+
accessTokenDTO.setClient_id("639f4c8d6441c6136b44");
27+
accessTokenDTO.setClient_secret("024fb99f36d3ce6764d6e00eb15c9545a284cb9");
28+
accessTokenDTO.setCode(code);
29+
accessTokenDTO.setRedirect_uri("http://localhost:8080/callback");
30+
accessTokenDTO.setState(state);
31+
githubProvider.getAccess_token(accessTokenDTO);
32+
return "index";
33+
}
34+
35+
36+
}
37+
38+
39+
40+

src/main/java/life/majiang/community/controller/HelloController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package life.majiang.community.controller;
22

3+
/*
4+
* Spring Boot 初运行
5+
* 访问地址需带参数 name=name
6+
*/
7+
38
import org.springframework.stereotype.Controller;
49
import org.springframework.ui.Model;
510
import org.springframework.web.bind.annotation.GetMapping;

src/main/java/life/majiang/community/controller/IndexController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package life.majiang.community.controller;
22

3+
/*
4+
* 定义自己的 index
5+
*/
6+
37
import org.springframework.stereotype.Controller;
48
import org.springframework.web.bind.annotation.GetMapping;
59

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package life.majiang.community.dto;
2+
3+
public class AccessTokenDTO {
4+
private String client_id;
5+
private String client_secret;
6+
private String code;
7+
private String redirect_uri;
8+
private String state;
9+
// 自动创建get, set 方法 快捷键Alt+Instert
10+
public String getClient_id() {
11+
return client_id;
12+
}
13+
14+
public void setClient_id(String client_id) {
15+
this.client_id = client_id;
16+
}
17+
18+
public String getClient_secret() {
19+
return client_secret;
20+
}
21+
22+
public void setClient_secret(String client_secret) {
23+
this.client_secret = client_secret;
24+
}
25+
26+
public String getCode() {
27+
return code;
28+
}
29+
30+
public void setCode(String code) {
31+
this.code = code;
32+
}
33+
34+
public String getRedirect_uri() {
35+
return redirect_uri;
36+
}
37+
38+
public void setRedirect_uri(String redirect_uri) {
39+
this.redirect_uri = redirect_uri;
40+
}
41+
42+
public String getState() {
43+
return state;
44+
}
45+
46+
public void setState(String state) {
47+
this.state = state;
48+
}
49+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package life.majiang.community.dto;
2+
3+
/*
4+
* 获取Github 用户信息
5+
*/
6+
public class GithubUser {
7+
private String name;
8+
private Long id;
9+
private String bio;
10+
11+
public String getName() {
12+
return name;
13+
}
14+
15+
public void setName(String name) {
16+
this.name = name;
17+
}
18+
19+
public Long getId() {
20+
return id;
21+
}
22+
23+
public void setId(Long id) {
24+
this.id = id;
25+
}
26+
27+
public String getBio() {
28+
return bio;
29+
}
30+
31+
public void setBio(String bio) {
32+
this.bio = bio;
33+
}
34+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package life.majiang.community.provider;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import life.majiang.community.dto.AccessTokenDTO;
5+
import life.majiang.community.dto.GithubUser;
6+
import okhttp3.*;
7+
import org.springframework.stereotype.Component;
8+
import java.io.IOException;
9+
10+
/*
11+
*
12+
*/
13+
@Component // 把类初始化到spring容器的上下文中
14+
public class GithubProvider {
15+
// get方法 获取用户token
16+
public String getAccess_token(AccessTokenDTO assessTokenDTO) {
17+
MediaType mediaType = MediaType.get("application/json; charset=utf-8");
18+
OkHttpClient client = new OkHttpClient();
19+
RequestBody body = RequestBody.create(JSON.toJSONString(assessTokenDTO), mediaType);
20+
Request request = new Request.Builder()
21+
.url("https://github.com/login/oauth/access_token")
22+
.post(body)
23+
.build();
24+
25+
try (Response response = client.newCall(request).execute()) {
26+
String string = response.body().string();
27+
System.out.println(string); // 输出
28+
return string;
29+
} catch (IOException e) {
30+
}
31+
return null;
32+
}
33+
34+
// get方法 获取用户信息
35+
public GithubUser getUser(String accessToken) {
36+
OkHttpClient client = new OkHttpClient();
37+
Request request = new Request.Builder()
38+
.url("https://api.github.com/user?access_token=" + accessToken)
39+
.build();
40+
try {
41+
Response response = client.newCall(request).execute();
42+
String string = response.body().string();
43+
GithubUser githubUser = JSON.parseObject(string, GithubUser.class); // Json解析为类对象
44+
return githubUser;
45+
} catch (IOException e) {
46+
}
47+
return null;
48+
49+
}
50+
}
51+

0 commit comments

Comments
 (0)