SSO 示例项目 http 请求工具改为 Forest

This commit is contained in:
click33 2022-10-24 00:31:17 +08:00
parent 10b6e8ac9b
commit 00886066e3
9 changed files with 59 additions and 33 deletions

View File

@ -60,10 +60,11 @@
<!-- Http请求工具在模式三的单点注销功能下用到如不需要可以注释掉 -->
<dependency>
<groupId>com.ejlchina</groupId>
<artifactId>okhttps</artifactId>
<version>3.1.1</version>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-spring-boot-starter</artifactId>
<version>1.5.26</version>
</dependency>
</dependencies>

View File

@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.ejlchina.okhttps.OkHttps;
import com.dtflys.forest.Forest;
import cn.dev33.satoken.config.SaSsoConfig;
import cn.dev33.satoken.sso.SaSsoHandle;
@ -55,8 +55,8 @@ public class SsoServerController {
sso.setSendHttp(url -> {
try {
// 发起 http 请求
System.out.println("发起请求:" + url);
return OkHttps.sync(url).get().getBody().toString();
System.out.println("------ 发起请求:" + url);
return Forest.get(url).executeAsString();
} catch (Exception e) {
e.printStackTrace();
return null;

View File

@ -49,8 +49,10 @@ spring:
# 连接池中的最小空闲连接
min-idle: 0
forest:
# 关闭 forest 请求日志打印
log-enabled: false

View File

@ -26,7 +26,7 @@
<dependency>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-spring-boot-starter</artifactId>
<version>1.5.19</version>
<version>1.5.26</version>
</dependency>
</dependencies>

View File

@ -56,9 +56,9 @@
<!-- Http请求工具 -->
<dependency>
<groupId>com.ejlchina</groupId>
<artifactId>okhttps</artifactId>
<version>3.1.1</version>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-spring-boot-starter</artifactId>
<version>1.5.26</version>
</dependency>

View File

@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ejlchina.okhttps.OkHttps;
import com.dtflys.forest.Forest;
import cn.dev33.satoken.config.SaSsoConfig;
import cn.dev33.satoken.sso.SaSsoHandle;
@ -46,8 +46,8 @@ public class SsoClientController {
private void configSso(SaSsoConfig sso) {
// 配置Http请求处理器
sso.setSendHttp(url -> {
System.out.println("发起请求:" + url);
return OkHttps.sync(url).get().getBody().toString();
System.out.println("------ 发起请求:" + url);
return Forest.get(url).executeAsString();
});
}

View File

@ -45,7 +45,9 @@ spring:
# 连接池中的最小空闲连接
min-idle: 0
forest:
# 关闭 forest 请求日志打印
log-enabled: false

View File

@ -45,9 +45,9 @@
<!-- Http请求工具在模式三的单点注销功能下用到如不需要可以注释掉 -->
<dependency>
<groupId>com.ejlchina</groupId>
<artifactId>okhttps</artifactId>
<version>3.1.1</version>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-spring-boot-starter</artifactId>
<version>1.5.26</version>
</dependency>
```
<!-------- tab:Gradle 方式 -------->
@ -66,7 +66,7 @@ implementation 'org.apache.commons:commons-pool2'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
// Http请求工具在模式三的单点注销功能下用到如不需要可以注释掉
implementation 'com.ejlchina:okhttps:3.1.1'
implementation 'com.dtflys.forest:forest-spring-boot-starter:1.5.26'
```
<!---------------------------- tabs:end ---------------------------->
@ -125,8 +125,8 @@ public class SsoServerController {
sso.setSendHttp(url -> {
try {
// 发起 http 请求
System.out.println("发起请求:" + url);
return OkHttps.sync(url).get().getBody().toString();
System.out.println("------ 发起请求:" + url);
return Forest.get(url).executeAsString();
} catch (Exception e) {
e.printStackTrace();
return null;
@ -199,6 +199,10 @@ spring:
port: 6379
# Redis服务器连接密码默认为空
password:
forest:
# 关闭 forest 请求日志打印
log-enabled: false
```
<!------------- tab:properties 风格 ------------->
``` properties
@ -225,7 +229,7 @@ sa-token.sso.isHttp=true
sa-token.sso.secretkey=kQwIOrYvnXmSDkwEiFngrKidMcdrgKor
# ---- 除了以上配置项,你还需要为 Sa-Token 配置http请求处理器文档有步骤说明
################## Redis配置 SSO模式一和模式二使用Redis来同步会话 ##################
# Redis数据库索引默认为0
spring.redis.database=1
@ -235,6 +239,9 @@ spring.redis.host=127.0.0.1
spring.redis.port=6379
# Redis服务器连接密码默认为空
spring.redis.password=
# 关闭 forest 请求日志打印
forest.log-enabled: false
```
<!---------------------------- tabs:end ---------------------------->

View File

@ -27,20 +27,20 @@
``` xml
<!-- Http请求工具 -->
<dependency>
<groupId>com.ejlchina</groupId>
<artifactId>okhttps</artifactId>
<version>3.1.1</version>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-spring-boot-starter</artifactId>
<version>1.5.26</version>
</dependency>
```
<!-------- tab:Gradle 方式 -------->
``` gradle
// Http请求工具
implementation 'com.ejlchina:okhttps:3.1.1'
implementation 'com.dtflys.forest:forest-spring-boot-starter:1.5.26'
```
<!---------------------------- tabs:end ---------------------------->
> OkHttps是一个轻量级http请求工具详情参考[OkHttps](https://gitee.com/ejlchina-zhxu/okhttps)
> Forest 是一个轻量级 http 请求工具,详情参考:[Forest](https://forest.dtflyx.com/)
#### 2.2、配置 http 请求处理器
在SSO-Client端的 `SsoClientController` 中,新增以下配置
@ -52,8 +52,8 @@ private void configSso(SaSsoConfig sso) {
// 配置 Http 请求处理器
sso.setSendHttp(url -> {
System.out.println("发起请求:" + url);
return OkHttps.sync(url).get().getBody().toString();
System.out.println("------ 发起请求:" + url);
return Forest.get(url).executeAsString();
});
}
```
@ -69,6 +69,10 @@ sa-token:
is-http: true
# SSO-Server端 ticket校验地址
check-ticket-url: http://sa-sso-server.com:9000/sso/checkTicket
forest:
# 关闭 forest 请求日志打印
log-enabled: false
```
<!------------- tab:properties 风格 ------------->
``` properties
@ -76,11 +80,21 @@ sa-token:
sa-token.sso.is-http=true
# SSO-Server端 ticket校验地址
sa-token.sso.check-ticket-url=http://sa-sso-server.com:9000/sso/checkTicket
# 关闭 forest 请求日志打印
forest.log-enabled: false
```
<!---------------------------- tabs:end ---------------------------->
因为我们已经在控制台手动打印 url 请求日志了,所以此处 `forest.log-enabled=false` 关闭 Forest 框架自身的日志打印,这不是必须的,你可以将其打开。
#### 2.4、启动项目测试
重启项目,访问测试:[http://sa-sso-client1.com:9001/](http://sa-sso-client1.com:9001/)
重启项目,访问测试:
- [http://sa-sso-client1.com:9001/](http://sa-sso-client1.com:9001/)
- [http://sa-sso-client2.com:9001/](http://sa-sso-client2.com:9001/)
- [http://sa-sso-client3.com:9001/](http://sa-sso-client3.com:9001/)
> 注如果已测试运行模式二可先将Redis中的数据清空以防旧数据对测试造成干扰