|
|
@@ -8,9 +8,17 @@ import com.uas.console.donate.util.ContextUtils;
|
|
|
import com.uas.console.donate.web.filter.SSOInterceptor;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.util.ResourceUtils;
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
|
|
|
|
|
+import java.io.*;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.Enumeration;
|
|
|
+import java.util.Properties;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipFile;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* SSOconfig 配置
|
|
|
@@ -55,10 +63,59 @@ public class SSOConfiguration extends WebMvcConfigurerAdapter {
|
|
|
* @return
|
|
|
*/
|
|
|
private AccountConfigurer accountConfigurer(String profile) {
|
|
|
- AccountConfigurer accountConfigurer = new AccountConfigurer("classpath:" + profile + "/account.properties");
|
|
|
+ AccountConfigurer accountConfigurer = new AccountConfigurer();
|
|
|
accountConfigurer.setApplicationContext(ContextUtils.getApplicationContext());
|
|
|
- accountConfigurer.init();
|
|
|
+ Properties properties = new Properties();
|
|
|
+ String configPath = "/" + profile + "/account.properties";
|
|
|
+
|
|
|
+ try {
|
|
|
+ InputStream inputStream;
|
|
|
+ URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();
|
|
|
+ // 是否以 spring boot jar 形式运行
|
|
|
+ if (ResourceUtils.isJarURL(location)) {
|
|
|
+ // 提取 war 中的 config
|
|
|
+ File warFile = ResourceUtils.getFile(ResourceUtils.extractJarFileURL(location));
|
|
|
+ inputStream = extractConfig(warFile, configPath);
|
|
|
+ } else {
|
|
|
+ // 通过 getResourceAsStream 加载配置
|
|
|
+ inputStream = this.getClass().getResourceAsStream(configPath);
|
|
|
+ }
|
|
|
+ properties.load(inputStream);
|
|
|
+ } catch (Throwable e) {
|
|
|
+ throw new IllegalStateException("配置加载失败" + configPath);
|
|
|
+ }
|
|
|
+ accountConfigurer.initProperties(properties);
|
|
|
return accountConfigurer;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 加载 war 中的文件
|
|
|
+ *
|
|
|
+ * @param warFile war 文件
|
|
|
+ * @param path 要加载的文件路径
|
|
|
+ * @return 加载的字节流
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private static InputStream extractConfig(File warFile, String path) throws IOException {
|
|
|
+ try (ZipFile zipFile = new ZipFile(warFile)) {
|
|
|
+ Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
|
|
+ while (entries.hasMoreElements()) {
|
|
|
+ ZipEntry zipEntry = entries.nextElement();
|
|
|
+ // 只提取指定路径的文件
|
|
|
+ if (zipEntry.getName().endsWith(path)) {
|
|
|
+ try (InputStream inputStream = zipFile.getInputStream(zipEntry); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
|
|
+ byte[] buffer = new byte[32 * 1024];
|
|
|
+ int bytesRead;
|
|
|
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ outputStream.flush();
|
|
|
+ return new ByteArrayInputStream(outputStream.toByteArray());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
}
|