支持refreshToken,自动延长会话时长
This commit is contained in:
parent
f3decc21ff
commit
bbfbd4fb35
|
@ -6,10 +6,16 @@ import net.sf.webdav.exceptions.WebdavException;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.LinkOption;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -19,16 +25,45 @@ public class AliYunDriverClient {
|
||||||
private OkHttpClient okHttpClient;
|
private OkHttpClient okHttpClient;
|
||||||
private AliYunDriveProperties aliYunDriveProperties;
|
private AliYunDriveProperties aliYunDriveProperties;
|
||||||
|
|
||||||
public AliYunDriverClient(OkHttpClient okHttpClient, AliYunDriveProperties aliYunDriveProperties) {
|
public AliYunDriverClient(AliYunDriveProperties aliYunDriveProperties) {
|
||||||
|
|
||||||
|
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
|
||||||
|
@Override
|
||||||
|
public Response intercept(Chain chain) throws IOException {
|
||||||
|
Request request = chain.request();
|
||||||
|
request = request.newBuilder()
|
||||||
|
.removeHeader("User-Agent")
|
||||||
|
.addHeader("User-Agent", aliYunDriveProperties.getAgent())
|
||||||
|
.removeHeader("authorization")
|
||||||
|
.addHeader("authorization", aliYunDriveProperties.getAuthorization())
|
||||||
|
.build();
|
||||||
|
return chain.proceed(request);
|
||||||
|
}
|
||||||
|
}).authenticator(new Authenticator() {
|
||||||
|
@Override
|
||||||
|
public Request authenticate(Route route, Response response) throws IOException {
|
||||||
|
if (response.code() == 401 && response.body() != null && response.body().string().contains("AccessToken")) {
|
||||||
|
String refreshTokenResult = post("https://websv.aliyundrive.com/token/refresh", Collections.singletonMap("refresh_token", readRefreshToken()));
|
||||||
|
String accessToken = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "access_token");
|
||||||
|
String refreshToken = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "refresh_token");
|
||||||
|
Assert.hasLength(accessToken, "获取accessToken失败");
|
||||||
|
Assert.hasLength(refreshToken, "获取refreshToken失败");
|
||||||
|
aliYunDriveProperties.setAuthorization(accessToken);
|
||||||
|
writeRefreshToken(refreshToken);
|
||||||
|
return response.request().newBuilder()
|
||||||
|
.removeHeader("authorization")
|
||||||
|
.header("authorization", accessToken)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}).build();
|
||||||
this.okHttpClient = okHttpClient;
|
this.okHttpClient = okHttpClient;
|
||||||
this.aliYunDriveProperties = aliYunDriveProperties;
|
this.aliYunDriveProperties = aliYunDriveProperties;
|
||||||
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void login() {
|
private void login() {
|
||||||
if (StringUtils.hasLength(aliYunDriveProperties.getAuthorization())) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo 暂不支持登录功能
|
// todo 暂不支持登录功能
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,4 +174,36 @@ public class AliYunDriverClient {
|
||||||
}
|
}
|
||||||
return aliYunDriveProperties.getUrl() + url;
|
return aliYunDriveProperties.getUrl() + url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String readRefreshToken() {
|
||||||
|
Path path = Paths.get(aliYunDriveProperties.getRefreshTokenPath());
|
||||||
|
|
||||||
|
if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
|
||||||
|
try {
|
||||||
|
Files.createDirectories(path.getParent());
|
||||||
|
Files.createFile(path);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
byte[] bytes = Files.readAllBytes(path);
|
||||||
|
if (bytes.length != 0) {
|
||||||
|
return new String(bytes, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.warn("读取refreshToken文件 {} 失败: ", aliYunDriveProperties.getRefreshTokenPath(), e);
|
||||||
|
}
|
||||||
|
writeRefreshToken(aliYunDriveProperties.getRefreshToken());
|
||||||
|
return aliYunDriveProperties.getRefreshToken();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeRefreshToken(String newRefreshToken) {
|
||||||
|
try {
|
||||||
|
Files.write(Paths.get(aliYunDriveProperties.getRefreshTokenPath()), newRefreshToken.getBytes(StandardCharsets.UTF_8));
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.warn("写入refreshToken文件 {} 失败: ", aliYunDriveProperties.getRefreshTokenPath(), e);
|
||||||
|
}
|
||||||
|
aliYunDriveProperties.setRefreshToken(newRefreshToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
@ConfigurationProperties(prefix = "aliyundrive", ignoreUnknownFields = true)
|
@ConfigurationProperties(prefix = "aliyundrive", ignoreUnknownFields = true)
|
||||||
public class AliYunDriveProperties {
|
public class AliYunDriveProperties {
|
||||||
private String url = "https://api.aliyundrive.com/v2";
|
private String url = "https://api.aliyundrive.com/v2";
|
||||||
private String authorization;
|
private String authorization = "";
|
||||||
|
private String refreshToken;
|
||||||
|
private String refreshTokenPath = "/etc/AliYunDriver-RefreshToken";
|
||||||
private String agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36";
|
private String agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36";
|
||||||
private String driveId;
|
private String driveId;
|
||||||
|
|
||||||
|
@ -37,6 +39,22 @@ public class AliYunDriveProperties {
|
||||||
return driveId;
|
return driveId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getRefreshToken() {
|
||||||
|
return refreshToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRefreshToken(String refreshToken) {
|
||||||
|
this.refreshToken = refreshToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRefreshTokenPath() {
|
||||||
|
return refreshTokenPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRefreshTokenPath(String refreshTokenPath) {
|
||||||
|
this.refreshTokenPath = refreshTokenPath;
|
||||||
|
}
|
||||||
|
|
||||||
public void setDriveId(String driveId) {
|
public void setDriveId(String driveId) {
|
||||||
this.driveId = driveId;
|
this.driveId = driveId;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.github.zxbu.webdavteambition.config;
|
||||||
|
|
||||||
|
import com.github.zxbu.webdavteambition.client.AliYunDriverClient;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableConfigurationProperties(AliYunDriveProperties.class)
|
||||||
|
public class AliYunDriverAutoConfig {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(AliYunDriverAutoConfig.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AliYunDriveProperties aliYunDriveProperties;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public AliYunDriverClient teambitionClient(ApplicationContext applicationContext) throws Exception {
|
||||||
|
return new AliYunDriverClient(aliYunDriveProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,50 +0,0 @@
|
||||||
package com.github.zxbu.webdavteambition.config;
|
|
||||||
|
|
||||||
import com.github.zxbu.webdavteambition.client.AliYunDriverClient;
|
|
||||||
import okhttp3.*;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.util.CollectionUtils;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@EnableConfigurationProperties(AliYunDriveProperties.class)
|
|
||||||
public class TeambitionAutoConfig {
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(TeambitionAutoConfig.class);
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private AliYunDriveProperties aliYunDriveProperties;
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public AliYunDriverClient teambitionClient(ApplicationContext applicationContext) throws Exception {
|
|
||||||
|
|
||||||
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
|
|
||||||
@Override
|
|
||||||
public Response intercept(Chain chain) throws IOException {
|
|
||||||
Request request = chain.request();
|
|
||||||
request = request.newBuilder()
|
|
||||||
.removeHeader("User-Agent")
|
|
||||||
.addHeader("User-Agent", aliYunDriveProperties.getAgent())
|
|
||||||
.addHeader("authorization", aliYunDriveProperties.getAuthorization())
|
|
||||||
.build();
|
|
||||||
return chain.proceed(request);
|
|
||||||
}
|
|
||||||
}).build();
|
|
||||||
AliYunDriverClient aliYunDriverClient = new AliYunDriverClient(okHttpClient, aliYunDriveProperties);
|
|
||||||
aliYunDriverClient.init();
|
|
||||||
return aliYunDriverClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -96,6 +96,10 @@ public class AliYunDriverClientService {
|
||||||
// 如果已存在,先删除
|
// 如果已存在,先删除
|
||||||
TFile tfile = getTFileByPath(path);
|
TFile tfile = getTFileByPath(path);
|
||||||
if (tfile != null) {
|
if (tfile != null) {
|
||||||
|
if (tfile.getSize() == size) {
|
||||||
|
//如果文件大小一样,则不再上传
|
||||||
|
return;
|
||||||
|
}
|
||||||
remove(path);
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue