-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 以桥接模式和适配器模式两种方式各实现第三方 Oauth(github) 登录 [demo建设] (#57)
* feat: 以桥接和适配器两种方式实现第三方 Oauth(github) 登录 [demo]
- Loading branch information
Showing
88 changed files
with
2,327 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# 适配器模式(Adapter)说明 | ||
|
||
适配器:意在将一个类的接口适配成用户所需的接口,它能帮助不兼容的接口变得兼容,宏观做法是将用户自定义的接口包裹在想要适配的接口里,就好比苹果的数据线.. | ||
|
||
适配器模式有三个角色 | ||
|
||
+ Target: 目标角色, 在 im-register-login-demo#adapter#Login3rdTarget, 是暴露给用户的接口, 根据设计模式六大原则之迪米特法则,一个类最好只暴露实现方法,而不暴露具体细节 | ||
+ Adaptee: 被适配角色, 在 im-register-login-demo#adapter#UserService,适配器将继承 UserService 类以达到扩展新功能而不改动原有类的需求,这是设计模式六大原则的开闭原则,即对修改关闭,对扩展开放 | ||
+ Adapter: 适配器角色, 在 im-register-login-demo#adapter#Login3rdAdapter,他将扩展出第三方登陆的核心逻辑方法,并且还具有 UserService 已实现的查询数据库是否有账号和注册逻辑 | ||
|
||
适配器根据适配的对象不同,可分为对象适配器和类适配器 | ||
+ 前者适配器关联一个包裹它的类实例 | ||
+ 后者适配器继承被适配的类对象(一般采用这种方式) | ||
|
||
对象适配器的一种实现方式: | ||
|
||
```java | ||
@Component | ||
public class Login3rdAdapter { | ||
@Resource | ||
private UserService userService; | ||
// ... | ||
} | ||
``` | ||
|
||
类适配器的一种实现方式: | ||
|
||
```java | ||
@Component | ||
public class Login3rdAdapter extends UserService { | ||
|
||
} | ||
``` | ||
|
||
Target 是接口,自然需要子类真正实现,在这里子类自然是 Adapter | ||
不难写出这样的代码 | ||
|
||
```java | ||
public class Login3rdAdapter extends UserService implements Login3rdTarget { | ||
|
||
public Login3rdAdapter(UserRepository userRepository) { | ||
super(userRepository); | ||
} | ||
|
||
@Override | ||
public String loginByGithub(String code, String state) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String loginByWechat() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String loginByQQ() { | ||
return null; | ||
} | ||
} | ||
``` | ||
+ 继承 UserService,以实现不侵入原有方法前提下进行第三方登录的扩展 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# 第三方登录的实现以及使用说明 | ||
|
||
## 使用说明 | ||
|
||
+ im-design-demo/im-register-login-demo/login-design-adapter-demo | ||
+ im-design-demo/im-register-login-demo/login-design-bridge-demo | ||
|
||
这两个子模块都对接了第三方登录的功能 | ||
|
||
体验步骤: | ||
1. 运行项目 | ||
2. 点击命令行里出现的蓝链 进行权限校验 | ||
|
||
## Github Oauth 第三方登录实现 | ||
参考这篇文章: | ||
|
||
[官方文档](https://docs.github.com/zh/apps/oauth-apps/building-oauth-apps/creating-an-oauth-app) | ||
|
||
[github第三方登录超详细流程及分析(小白笔记)](https://blog.csdn.net/qq_43516238/article/details/105884926) | ||
|
||
[GitHub OAuth 第三方登录示例教程 - 阮一峰的网络日志](https://ruanyifeng.com/blog/2019/04/github-oauth.html) | ||
|
||
```yml | ||
client_id: cf00a9382ce8110c2a70 | ||
client_secret: fd348b2050f64c7a99c07294b390a5adfaa21e8c | ||
redirect_uri: http://localhost:21001/github | ||
``` | ||
|
||
yml 配置 | ||
```yml | ||
github: | ||
state: GITHUB | ||
user_prefix: ${github.state}@ | ||
|
||
# ========= 自己申请 client_id\secret 用完之后记得删除 ========= | ||
client_id: cf00a9382ce8110c2a70 | ||
client_secret: fd348b2050f64c7a99c07294b390a5adfaa21e8c | ||
# ========================================================== | ||
|
||
callback: http://localhost:21001/github # github 回调 callback 会携带 code 参数 | ||
token_url: https://github.com/login/oauth/access_token?client_id=${github.client_id}&client_secret=${github.client_secret}&redirect_uri=${github.callback}&code= # 拼接 code | ||
user_url: https://api.github.com/user # 使用访问令牌访问 API | ||
``` | ||
|
||
![img.png](../../../im-design-demo/im-register-login-demo/login-design-adapter-demo/assert/img.png) | ||
|
||
权限访问: | ||
|
||
https://github.com/login/oauth/authorize?client_id=cf00a9382ce8110c2a70&redirect_uri=http://localhost:21001/github&state=GITHUB | ||
|
||
可能会出现超时的情况 | ||
|
||
![timeout.png](../../../im-design-demo/im-register-login-demo/login-design-adapter-demo/assert/timeout.png) | ||
|
||
成功 | ||
|
||
![success.png](../../../im-design-demo/im-register-login-demo/login-design-adapter-demo/assert/success.png) | ||
|
||
数据库存在该数据 | ||
|
||
![数据库存有github账号.png](../../../im-design-demo/im-register-login-demo/login-design-adapter-demo/assert/数据库存有github账号.png) |
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE DATABASE IF NOT EXISTS im_register_login_demo; | ||
USE im_register_login_demo; | ||
|
||
CREATE TABLE user ( | ||
id BIGINT AUTO_INCREMENT | ||
PRIMARY KEY, | ||
username VARCHAR(64), | ||
password VARCHAR(64), | ||
create_time DATE, | ||
user_email VARCHAR(64) | ||
); | ||
|
||
INSERT INTO user (id, username, password, create_time, user_email) VALUES ('10001', 'admin', 'admin', now(), '[email protected]') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ services: | |
timeout: 5s | ||
retries: 3 | ||
ports: | ||
- "3306:3306" | ||
- "13306:3306" | ||
networks: | ||
- im-network | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE DATABASE IF NOT EXISTS im_register_login_demo; | ||
USE im_register_login_demo; | ||
|
||
CREATE TABLE user ( | ||
id BIGINT AUTO_INCREMENT | ||
PRIMARY KEY, | ||
username VARCHAR(64), | ||
password VARCHAR(64), | ||
create_time DATE, | ||
user_email VARCHAR(64) | ||
); | ||
|
||
INSERT INTO user (id, username, password, create_time, user_email) VALUES ('10001', 'admin', 'admin', now(), '[email protected]') |
63 changes: 63 additions & 0 deletions
63
im-design-demo/im-register-login-demo/login-design-adapter-demo/assert/adapter.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# 适配器模式(Adapter)说明 | ||
|
||
适配器:意在将一个类的接口适配成用户所需的接口,它能帮助不兼容的接口变得兼容,宏观做法是将用户自定义的接口包裹在想要适配的接口里,就好比苹果的数据线.. | ||
|
||
适配器模式有三个角色 | ||
|
||
+ Target: 目标角色, 在 im-register-login-demo#adapter#Login3rdTarget, 是暴露给用户的接口, 根据设计模式六大原则之迪米特法则,一个类最好只暴露实现方法,而不暴露具体细节 | ||
+ Adaptee: 被适配角色, 在 im-register-login-demo#adapter#UserService,适配器将继承 UserService 类以达到扩展新功能而不改动原有类的需求,这是设计模式六大原则的开闭原则,即对修改关闭,对扩展开放 | ||
+ Adapter: 适配器角色, 在 im-register-login-demo#adapter#Login3rdAdapter,他将扩展出第三方登陆的核心逻辑方法,并且还具有 UserService 已实现的查询数据库是否有账号和注册逻辑 | ||
|
||
适配器根据适配的对象不同,可分为对象适配器和类适配器 | ||
+ 前者适配器关联一个包裹它的类实例 | ||
+ 后者适配器继承被适配的类对象(一般采用这种方式) | ||
|
||
对象适配器的一种实现方式: | ||
|
||
```java | ||
@Component | ||
public class Login3rdAdapter { | ||
@Resource | ||
private UserService userService; | ||
// ... | ||
} | ||
``` | ||
|
||
类适配器的一种实现方式: | ||
|
||
```java | ||
@Component | ||
public class Login3rdAdapter extends UserService { | ||
|
||
} | ||
``` | ||
|
||
Target 是接口,自然需要子类真正实现,在这里子类自然是 Adapter | ||
不难写出这样的代码 | ||
|
||
```java | ||
public class Login3rdAdapter extends UserService implements Login3rdTarget { | ||
|
||
public Login3rdAdapter(UserRepository userRepository) { | ||
super(userRepository); | ||
} | ||
|
||
@Override | ||
public String loginByGithub(String code, String state) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String loginByWechat() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String loginByQQ() { | ||
return null; | ||
} | ||
} | ||
``` | ||
+ 继承 UserService,以实现不侵入原有方法前提下进行第三方登录的扩展 | ||
|
||
适配器的好处在于不修改原有逻辑就能实现扩展与替换,但如果需要扩展的子类过多,例如 demo 里所展示的第三方账号越来越多,手机短信验证码、CSDN账号、Gitee、twitter 等等,可能会导致适配器适配的种类越来越多 |
Binary file added
BIN
+61.4 KB
im-design-demo/im-register-login-demo/login-design-adapter-demo/assert/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.