Skip to content
This repository was archived by the owner on Jul 21, 2024. It is now read-only.

Commit fca7fda

Browse files
committed
* 修复设备踢掉后无法重新登录
+ 支持30分钟静默自动登录
1 parent 25ecc8c commit fca7fda

File tree

3 files changed

+66
-29
lines changed

3 files changed

+66
-29
lines changed

src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,23 @@ public class AliYunDriverClient {
2424
private Runnable onRefreshTokenInvalidListener;
2525

2626
public Request buildCommonRequestHeader(Request request) {
27-
return request.newBuilder()
27+
Request.Builder builder = request.newBuilder()
2828
.removeHeader("User-Agent")
2929
.addHeader("User-Agent", aliYunDriveProperties.agent)
30-
.removeHeader("authorization")
31-
.addHeader("authorization", "Bearer\t" + aliYunDriveProperties.authorization)
3230
.removeHeader("x-device-id")
3331
.addHeader("x-device-id", aliYunDriveProperties.deviceId)
3432
.removeHeader("x-signature")
3533
.addHeader("x-signature", aliYunDriveProperties.session.signature + "01")
3634
.removeHeader("x-canary")
3735
.addHeader("x-canary", "client=web,app=adrive,version=v3.17.0")
3836
.removeHeader("x-request-id")
39-
.addHeader("x-request-id", UUID.randomUUID().toString())
40-
.build();
37+
.addHeader("x-request-id", UUID.randomUUID().toString());
38+
39+
builder.removeHeader("authorization");
40+
if (!StringUtils.isEmpty(aliYunDriveProperties.authorization)) {
41+
builder.addHeader("authorization", "Bearer\t" + aliYunDriveProperties.authorization);
42+
}
43+
return builder.build();
4144
}
4245

4346
public AliYunDriverClient(AliYunDriveProperties aliYunDriveProperties) {
@@ -61,6 +64,20 @@ public Response intercept(Chain chain) throws IOException {
6164
AliYunDriverClient.this.aliYunDriveProperties.session.expireTimeSec = 0;
6265
AliYunDriverClient.this.aliYunDriveProperties.save();
6366
LOGGER.error("登录设备过多, 请进入\"登录设备管理\", 退出一些设备。");
67+
if (!url.endsWith("/token/refresh")) {
68+
requestAuthorization();
69+
Response retryResponse = chain.proceed(buildCommonRequestHeader(request));
70+
ResponseBody retryBody = response.peekBody(40960);
71+
String retryRes = retryBody.string();
72+
if (retryRes.contains("UserDeviceOffline")) {
73+
LOGGER.error("重新登录失败, 设备数过多, 等待30分钟...");
74+
//防止请求数过多
75+
try {
76+
TimeUnit.MINUTES.sleep(30);
77+
} catch (InterruptedException e) {
78+
}
79+
}
80+
}
6481
return response;
6582
}
6683
}
@@ -74,28 +91,7 @@ public Request authenticate(Route route, Response response) throws IOException {
7491
ResponseBody body = response.peekBody(40960);
7592
String res = body.string();
7693
if (res.contains("AccessToken")) {
77-
String refreshTokenResult;
78-
try {
79-
if (StringUtils.isEmpty(aliYunDriveProperties.refreshToken)) {
80-
throw new NullPointerException();
81-
}
82-
refreshTokenResult = post("https://api.aliyundrive.com/token/refresh", Collections.singletonMap("refresh_token", aliYunDriveProperties.refreshToken));
83-
} catch (Exception e) {
84-
refreshTokenResult = post("https://api.aliyundrive.com/token/refresh", Collections.singletonMap("refresh_token", aliYunDriveProperties.refreshTokenNext));
85-
}
86-
String accessToken = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "access_token");
87-
String refreshToken = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "refresh_token");
88-
String userId = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "user_id");
89-
if (StringUtils.isEmpty(accessToken))
90-
throw new IllegalArgumentException("获取accessToken失败");
91-
if (StringUtils.isEmpty(refreshToken))
92-
throw new IllegalArgumentException("获取refreshToken失败");
93-
if (StringUtils.isEmpty(refreshToken))
94-
throw new IllegalArgumentException("获取userId失败");
95-
aliYunDriveProperties.userId = userId;
96-
aliYunDriveProperties.authorization = accessToken;
97-
aliYunDriveProperties.refreshToken = refreshToken;
98-
aliYunDriveProperties.save();
94+
String accessToken = requestAuthorization();
9995
return response.request().newBuilder()
10096
.removeHeader("authorization")
10197
.header("authorization", accessToken)
@@ -116,6 +112,33 @@ public Request authenticate(Route route, Response response) throws IOException {
116112
}
117113
}
118114

115+
private String requestAuthorization() {
116+
String refreshTokenResult;
117+
aliYunDriveProperties.authorization = null;
118+
try {
119+
if (StringUtils.isEmpty(aliYunDriveProperties.refreshToken)) {
120+
throw new NullPointerException();
121+
}
122+
refreshTokenResult = post("https://api.aliyundrive.com/token/refresh", Collections.singletonMap("refresh_token", aliYunDriveProperties.refreshToken));
123+
} catch (Exception e) {
124+
refreshTokenResult = post("https://api.aliyundrive.com/token/refresh", Collections.singletonMap("refresh_token", aliYunDriveProperties.refreshTokenNext));
125+
}
126+
String accessToken = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "access_token");
127+
String refreshToken = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "refresh_token");
128+
String userId = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "user_id");
129+
if (StringUtils.isEmpty(accessToken))
130+
throw new IllegalArgumentException("获取accessToken失败");
131+
if (StringUtils.isEmpty(refreshToken))
132+
throw new IllegalArgumentException("获取refreshToken失败");
133+
if (StringUtils.isEmpty(refreshToken))
134+
throw new IllegalArgumentException("获取userId失败");
135+
aliYunDriveProperties.userId = userId;
136+
aliYunDriveProperties.authorization = accessToken;
137+
aliYunDriveProperties.refreshToken = refreshToken;
138+
aliYunDriveProperties.save();
139+
return accessToken;
140+
}
141+
119142
private void login() {
120143
// todo 暂不支持登录功能
121144
}

src/main/java/com/github/zxbu/webdavteambition/config/AliYunDriveProperties.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class AliYunDriveProperties implements InitializingBean {
2121
private static final Logger LOGGER = LoggerFactory.getLogger(AliYunDriveProperties.class);
2222
private static final String META_FILE_NAME = "meta.json";
2323
public String url = "https://api.aliyundrive.com/v2";
24-
public String authorization = "";
24+
public transient String authorization = "";
2525
public String refreshToken;
2626
public String refreshTokenNext;
2727
public String workDir = "/workspace/etc/aliyun-driver/";
@@ -35,7 +35,7 @@ public class AliYunDriveProperties implements InitializingBean {
3535
public Auth auth = new Auth();
3636

3737
public void save() {
38-
String json = JsonUtil.toJson(this);
38+
String json = JsonUtil.toJsonPretty(this);
3939
File metaFile = new File(workDir, META_FILE_NAME);
4040
if (!metaFile.exists()) {
4141
metaFile.getParentFile().mkdirs();
@@ -66,6 +66,7 @@ public void afterPropertiesSet() throws Exception {
6666
String refreshToken = this.refreshToken;
6767
AliYunDriveProperties other = load(workDir);
6868
BeanUtils.copyProperties(other, this);
69+
this.authorization = null;
6970
if (StringUtils.isEmpty(this.deviceId)) {
7071
this.deviceId = UUID.randomUUID().toString().replace("-", "").substring(0, 24);
7172
}

src/main/java/com/github/zxbu/webdavteambition/util/JsonUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.core.type.TypeReference;
66
import com.fasterxml.jackson.databind.DeserializationFeature;
77
import com.fasterxml.jackson.databind.JsonNode;
8+
import com.fasterxml.jackson.databind.MapperFeature;
89
import com.fasterxml.jackson.databind.ObjectMapper;
910
import net.sf.webdav.exceptions.WebdavException;
1011

@@ -15,12 +16,24 @@
1516
public class JsonUtil {
1617
private static ObjectMapper objectMapper = new ObjectMapper();
1718

19+
static {
20+
objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
21+
}
22+
1823
public static String toJson(Object o) {
1924
try {
2025
return objectMapper.writeValueAsString(o);
2126
} catch (JsonProcessingException e) {
2227
throw new WebdavException(e);
2328
}
29+
};
30+
31+
public static String toJsonPretty(Object o) {
32+
try {
33+
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(o);
34+
} catch (JsonProcessingException e) {
35+
throw new WebdavException(e);
36+
}
2437
}
2538

2639
public static <T> T readValue(String json, TypeReference<T> valueTypeRef) {

0 commit comments

Comments
 (0)