Browse Source

REPORT-53647【组件复用涉及到登录相关功能】设计器本身是没有登录的,换成最新的nightly后,打开设计器后无论是否登录,下载在线组件都会提示失败@lanla

zheng-1641779399395
kerry 3 years ago
parent
commit
ddcfd7cea6
  1. 60
      designer-form/src/main/java/com/fr/design/mainframe/share/util/DownloadUtils.java

60
designer-form/src/main/java/com/fr/design/mainframe/share/util/DownloadUtils.java

@ -1,9 +1,9 @@
package com.fr.design.mainframe.share.util;
import com.fr.config.MarketConfig;
import com.fr.design.extra.PluginConstants;
import com.fr.form.share.base.CancelCheck;
import com.fr.form.share.constants.ShareComponentConstants;
import com.fr.ftp.util.Base64;
import com.fr.general.CloudCenter;
import com.fr.log.FineLoggerFactory;
import com.fr.stable.ProductConstants;
@ -21,31 +21,27 @@ import com.fr.third.org.apache.http.impl.client.CloseableHttpClient;
import com.fr.third.org.apache.http.impl.client.HttpClients;
import org.jetbrains.annotations.NotNull;
import javax.crypto.Cipher;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
/**
* created by Harrison on 2020/05/27
**/
public class DownloadUtils {
private static final String MARKET_LOGIN_URL = CloudCenter.getInstance().acquireUrlByKind("market.login");
private static final String REUSES_URL = CloudCenter.getInstance().acquireUrlByKind("af.reuseInfo") + "file/";
private static final String PACKAGE_REUSES_URL = CloudCenter.getInstance().acquireUrlByKind("af.reuseInfo") + "package/download/";
private static final String CERTIFICATE_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtsz62CPSWXZE/IYZRiAuTSZkw\n" +
"1WOwer8+JFktK0uKLAUuQoBr+UjAMFtRA8W7JgKMDwZy/2liEAiXEOSPU/hrdV8D\n" +
"tT541LnGi1X/hXiRwuttPWYN3L2GYm/d5blU+FBNwghBIrdAxXTzYBc6P4KL/oYX\n" +
"nMdTIrkz8tYkG3QoFQIDAQAB";
public static boolean login(CloseableHttpClient client) throws Exception {
HttpUriRequest login = RequestBuilder.post()
.setUri(MARKET_LOGIN_URL)
.setHeader("User-Agent", "Mozilla/5.0")
.addParameter("username", MarketConfig.getInstance().getBbsUsername())
.addParameter("password", MarketConfig.getInstance().getBbsPassword())
.build();
CloseableHttpResponse loginResponse = client.execute(login);
return loginResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
}
private static CloseableHttpClient createClient() {
BasicCookieStore cookieStore = new BasicCookieStore();
@ -59,8 +55,10 @@ public class DownloadUtils {
@NotNull
public static String download(String id, String fileName, com.fr.design.extra.Process<Double> process) throws Exception {
CloseableHttpResponse fileRes = getHttpResponse(REUSES_URL, id);
if (fileRes.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
fileRes = getHttpResponse(fileRes.getHeaders("Location")[0].getValue());
}
if (fileRes.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String realPath = StableUtils.pathJoin(ProductConstants.getEnvHome(), ShareComponentConstants.PLUGIN_CACHE, ShareComponentConstants.DOWNLOAD_SHARE);
String filePath;
@ -95,7 +93,9 @@ public class DownloadUtils {
public static String downloadPackage(String id, String fileName, CancelCheck cancelCheck) throws Exception {
CloseableHttpResponse fileRes = getHttpResponse(PACKAGE_REUSES_URL, id);
if (fileRes.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
fileRes = getHttpResponse(fileRes.getHeaders("Location")[0].getValue());
}
String realPath = StableUtils.pathJoin(ProductConstants.getEnvHome(), ShareComponentConstants.PLUGIN_CACHE, ShareComponentConstants.DOWNLOAD_PACKAGE_SHARE);
String filePath;
if (fileRes.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
@ -133,11 +133,35 @@ public class DownloadUtils {
//先登录一下。不然可能失败
CloseableHttpClient client = createClient();
FineLoggerFactory.getLogger().info("login fr-market");
login(client);
FineLoggerFactory.getLogger().info("start download widget {}", id);
HttpUriRequest file = RequestBuilder.post()
.setHeader("User-Agent", "Mozilla/5.0")
.setUri(url).addParameter("id", encrypt(id))
.build();
return client.execute(file);
}
private static CloseableHttpResponse getHttpResponse(String url) throws Exception {
//先登录一下。不然可能失败
CloseableHttpClient client = createClient();
HttpUriRequest file = RequestBuilder.get()
.setUri(url + id)
.setUri(url)
.build();
return client.execute(file);
}
private static String encrypt(String str) throws Exception {
//base64编码的公钥
byte[] decoded = Base64.decodeBase64(CERTIFICATE_PUBLIC_KEY);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
return outStr;
}
}

Loading…
Cancel
Save