From bbfbd4fb35741d679688043814f1b670ecfe7dcf Mon Sep 17 00:00:00 2001 From: zhouxin Date: Sun, 23 May 2021 09:16:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81refreshToken=EF=BC=8C?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=BB=B6=E9=95=BF=E4=BC=9A=E8=AF=9D=E6=97=B6?= =?UTF-8?q?=E9=95=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/AliYunDriverClient.java | 77 +++++++++++++++++-- .../config/AliYunDriveProperties.java | 20 ++++- .../config/AliYunDriverAutoConfig.java | 27 +++++++ .../config/TeambitionAutoConfig.java | 50 ------------ .../store/AliYunDriverClientService.java | 4 + 5 files changed, 122 insertions(+), 56 deletions(-) create mode 100644 src/main/java/com/github/zxbu/webdavteambition/config/AliYunDriverAutoConfig.java delete mode 100644 src/main/java/com/github/zxbu/webdavteambition/config/TeambitionAutoConfig.java diff --git a/src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java b/src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java index 642f207..ee6b4ba 100644 --- a/src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java +++ b/src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java @@ -6,10 +6,16 @@ import net.sf.webdav.exceptions.WebdavException; import okhttp3.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; import java.io.IOException; 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.LinkedHashMap; import java.util.Map; @@ -19,16 +25,45 @@ public class AliYunDriverClient { private OkHttpClient okHttpClient; 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.aliYunDriveProperties = aliYunDriveProperties; + init(); } private void login() { - if (StringUtils.hasLength(aliYunDriveProperties.getAuthorization())) { - return; - } - // todo 暂不支持登录功能 } @@ -139,4 +174,36 @@ public class AliYunDriverClient { } 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); + } } diff --git a/src/main/java/com/github/zxbu/webdavteambition/config/AliYunDriveProperties.java b/src/main/java/com/github/zxbu/webdavteambition/config/AliYunDriveProperties.java index 3098a54..bc5e88a 100644 --- a/src/main/java/com/github/zxbu/webdavteambition/config/AliYunDriveProperties.java +++ b/src/main/java/com/github/zxbu/webdavteambition/config/AliYunDriveProperties.java @@ -5,7 +5,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "aliyundrive", ignoreUnknownFields = true) public class AliYunDriveProperties { 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 driveId; @@ -37,6 +39,22 @@ public class AliYunDriveProperties { 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) { this.driveId = driveId; } diff --git a/src/main/java/com/github/zxbu/webdavteambition/config/AliYunDriverAutoConfig.java b/src/main/java/com/github/zxbu/webdavteambition/config/AliYunDriverAutoConfig.java new file mode 100644 index 0000000..997371a --- /dev/null +++ b/src/main/java/com/github/zxbu/webdavteambition/config/AliYunDriverAutoConfig.java @@ -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); + } + + + +} diff --git a/src/main/java/com/github/zxbu/webdavteambition/config/TeambitionAutoConfig.java b/src/main/java/com/github/zxbu/webdavteambition/config/TeambitionAutoConfig.java deleted file mode 100644 index f52921e..0000000 --- a/src/main/java/com/github/zxbu/webdavteambition/config/TeambitionAutoConfig.java +++ /dev/null @@ -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; - } - - - -} diff --git a/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java b/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java index a2905fd..246df06 100644 --- a/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java +++ b/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java @@ -96,6 +96,10 @@ public class AliYunDriverClientService { // 如果已存在,先删除 TFile tfile = getTFileByPath(path); if (tfile != null) { + if (tfile.getSize() == size) { + //如果文件大小一样,则不再上传 + return; + } remove(path); }