Skip to content
This repository was archived by the owner on Jan 20, 2026. It is now read-only.

Commit 0d314cf

Browse files
committed
feat(web): 增加 refresh token 功能
- 在 ApiConfig 中添加 refreshToken 字段 - 在 IWebConsoleClient 接口中添加 refreshToken 方法 - 新增 TokenList 类用于存储 access token 和 refresh token - 修改 WebAuthService.login 方法返回 TokenList 对象 - 更新 WebClientBuilder 和 WebClientImpl 以支持 refresh token
1 parent 745adcb commit 0d314cf

File tree

6 files changed

+42
-17
lines changed

6 files changed

+42
-17
lines changed

src/main/java/io/github/yuanbaobaoo/dify/types/ApiConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ public class ApiConfig {
1717
* Api Key
1818
*/
1919
private String apiKey;
20+
21+
/**
22+
* refreshToken
23+
*/
24+
private String refreshToken;
2025
}

src/main/java/io/github/yuanbaobaoo/dify/utils/WebClientBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.github.yuanbaobaoo.dify.types.WebConfig;
66
import io.github.yuanbaobaoo.dify.web.WebAuthService;
77
import io.github.yuanbaobaoo.dify.web.IWebConsoleClient;
8+
import io.github.yuanbaobaoo.dify.web.entity.TokenList;
89
import io.github.yuanbaobaoo.dify.web.impl.WebClientImpl;
910

1011
/**
@@ -89,8 +90,8 @@ public IWebConsoleClient connect() {
8990

9091
try {
9192
WebConfig config = WebConfig.builder().server(server).userName(userName).password(password).build();
92-
String token = WebAuthService.login(config);
93-
return new WebClientImpl(server, token);
93+
TokenList tokenList = WebAuthService.login(config);
94+
return new WebClientImpl(server, tokenList);
9495
} catch (DifyClientException | DifyException e) {
9596
throw e;
9697
} catch (Exception e) {

src/main/java/io/github/yuanbaobaoo/dify/web/IWebConsoleClient.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.alibaba.fastjson2.JSONObject;
44
import io.github.yuanbaobaoo.dify.SimpleHttpClient;
55
import io.github.yuanbaobaoo.dify.types.DifyPage;
6+
import io.github.yuanbaobaoo.dify.web.entity.TokenList;
67

78
public interface IWebConsoleClient {
89
/**
@@ -14,7 +15,10 @@ public interface IWebConsoleClient {
1415
* 获取登录的Access token
1516
*/
1617
String token();
17-
18+
/**
19+
* 获取登录的Refresh token
20+
*/
21+
String refreshToken();
1822
/**
1923
* 查询应用列表
2024
* @param page int

src/main/java/io/github/yuanbaobaoo/dify/web/WebAuthService.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.github.yuanbaobaoo.dify.types.DifyClientException;
88
import io.github.yuanbaobaoo.dify.SimpleHttpClient;
99
import io.github.yuanbaobaoo.dify.types.WebConfig;
10+
import io.github.yuanbaobaoo.dify.web.entity.TokenList;
1011
import lombok.Getter;
1112
import lombok.Setter;
1213
import lombok.extern.slf4j.Slf4j;
@@ -22,19 +23,11 @@ private static class LoginResult {
2223
private String result;
2324
}
2425

25-
@Getter
26-
@Setter
27-
@JSONType(naming = PropertyNamingStrategy.SnakeCase)
28-
private static class TokenList {
29-
private String accessToken;
30-
private String refreshToken;
31-
}
32-
3326
/**
3427
* 登录
3528
* @param config DifyWebConfig
3629
*/
37-
public static String login(WebConfig config) {
30+
public static TokenList login(WebConfig config) {
3831
SimpleHttpClient client = SimpleHttpClient.newHttpClient(config.getServer());
3932

4033
String result = client.requestJson(ConsoleRoutes.LOGIN, null, new HashMap<>() {{
@@ -47,7 +40,7 @@ public static String login(WebConfig config) {
4740
LoginResult loginResult = JSON.parseObject(result, LoginResult.class);
4841

4942
if ("success".equals(loginResult.getResult())) {
50-
return loginResult.getData().getAccessToken();
43+
return loginResult.getData();
5144
}
5245

5346
throw new DifyClientException("登录失败: " + result);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.yuanbaobaoo.dify.web.entity;
2+
3+
import com.alibaba.fastjson2.PropertyNamingStrategy;
4+
import com.alibaba.fastjson2.annotation.JSONType;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
8+
@Getter
9+
@Setter
10+
@JSONType(naming = PropertyNamingStrategy.SnakeCase)
11+
public class TokenList {
12+
private String accessToken;
13+
private String refreshToken;
14+
}

src/main/java/io/github/yuanbaobaoo/dify/web/impl/WebClientImpl.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.github.yuanbaobaoo.dify.SimpleHttpClient;
1010
import io.github.yuanbaobaoo.dify.types.DifyRoute;
1111
import io.github.yuanbaobaoo.dify.web.IWebConsoleClient;
12+
import io.github.yuanbaobaoo.dify.web.entity.TokenList;
1213
import lombok.extern.slf4j.Slf4j;
1314

1415
import java.util.HashMap;
@@ -20,11 +21,12 @@ public class WebClientImpl implements IWebConsoleClient {
2021

2122
/**
2223
* constructor
24+
*
2325
* @param server Dify 服务地址
24-
* @param token Dify access token
26+
* @param token Dify access token
2527
*/
26-
public WebClientImpl(String server, String token) {
27-
config = ApiConfig.builder().server(server).apiKey(token).build();
28+
public WebClientImpl(String server, TokenList token) {
29+
config = ApiConfig.builder().server(server).apiKey(token.getAccessToken()).refreshToken(token.getRefreshToken()).build();
2830
}
2931

3032
@Override
@@ -37,6 +39,11 @@ public String token() {
3739
return config.getApiKey();
3840
}
3941

42+
@Override
43+
public String refreshToken() {
44+
return config.getRefreshToken();
45+
}
46+
4047
@Override
4148
public DifyPage<JSONObject> queryApps(int page, int limit, String name) {
4249
String result = SimpleHttpClient.get(config).requestJson(ConsoleRoutes.APPS, new HashMap<>() {{
@@ -45,7 +52,8 @@ public DifyPage<JSONObject> queryApps(int page, int limit, String name) {
4552
put("name", name);
4653
}});
4754

48-
return JSON.parseObject(result, new TypeReference<DifyPage<JSONObject>>() {});
55+
return JSON.parseObject(result, new TypeReference<DifyPage<JSONObject>>() {
56+
});
4957
}
5058

5159
}

0 commit comments

Comments
 (0)