diff --git a/src/main/java/cokr/xit/foundation/boot/MvcConfig.java b/src/main/java/cokr/xit/foundation/boot/MvcConfig.java
index 72c4dda..378728e 100644
--- a/src/main/java/cokr/xit/foundation/boot/MvcConfig.java
+++ b/src/main/java/cokr/xit/foundation/boot/MvcConfig.java
@@ -1,6 +1,9 @@
package cokr.xit.foundation.boot;
+import javax.annotation.Resource;
+
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@@ -24,6 +27,8 @@ import cokr.xit.foundation.web.AccessInitializer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
protected static String[] URL_PATTERNS = {"/", "/**/*.do"};
+ @Resource(name = "staticResource")
+ private StaticResourceConfig staticResource;
/**AccessInitializer를 반환한다.
* @return AccessInitializer
@@ -33,6 +38,9 @@ public class MvcConfig implements WebMvcConfigurer {
return new AccessInitializer();
}
+ @Value("${spring.web.resources.static-locations}")
+ private String staticLocations;
+
/**정적 파일 자원 접근에 대한 설정을 추가한다.
*
- url: /resources/**
* - 위치: /resources/
@@ -40,9 +48,11 @@ public class MvcConfig implements WebMvcConfigurer {
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry
- .addResourceHandler("/resources/**", "/files/**")
- .addResourceLocations("/resources/", "/files/");
+ staticResource.getMappings().forEach((k, v) ->
+ registry
+ .addResourceHandler(k)
+ .addResourceLocations(v)
+ );
}
/**AccessInitializer를 interceptor로 추가한다.
diff --git a/src/main/java/cokr/xit/foundation/boot/ServletConfig.java b/src/main/java/cokr/xit/foundation/boot/ServletConfig.java
index 4ea9d4d..0d20766 100644
--- a/src/main/java/cokr/xit/foundation/boot/ServletConfig.java
+++ b/src/main/java/cokr/xit/foundation/boot/ServletConfig.java
@@ -1,6 +1,8 @@
package cokr.xit.foundation.boot;
-import java.util.HashMap;
+import java.util.Map;
+
+import javax.annotation.Resource;
import org.apache.catalina.servlets.DefaultServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
@@ -17,6 +19,8 @@ import cokr.xit.foundation.web.AccessFilter;
@Configuration
public class ServletConfig {
protected static String[] URL_PATTERNS = {"/", "*.do"};
+ @Resource(name = "staticResource")
+ private StaticResourceConfig staticResource;
/**CharacterEncodingFilter를 등록한다.
* @return FilterRegistrationBean
@@ -47,18 +51,20 @@ public class ServletConfig {
*/
@Bean
public ServletRegistrationBean defaultServlet() {
- ServletRegistrationBean bean = new ServletRegistrationBean<>(
- new DefaultServlet(),
- "/resources/*",
- "/files/*"
+ String[] urls = staticResource.getURLs(entry -> !entry.getValue().startsWith("file:"));
+ for (int i = 0; i bean = new ServletRegistrationBean<>(
+ new DefaultServlet(),
+ urls
);
bean.setLoadOnStartup(1);
+ bean.setInitParameters(Map.of(
+ "debug", "0",
+ "listings", "false"
+ ));
- HashMap params = new HashMap<>();
- params.put("debug", "0");
- params.put("listings", "false");
-
- bean.setInitParameters(params);
return bean;
}
}
\ No newline at end of file
diff --git a/src/main/java/cokr/xit/foundation/boot/StaticResourceConfig.java b/src/main/java/cokr/xit/foundation/boot/StaticResourceConfig.java
new file mode 100644
index 0000000..2ec3a37
--- /dev/null
+++ b/src/main/java/cokr/xit/foundation/boot/StaticResourceConfig.java
@@ -0,0 +1,55 @@
+package cokr.xit.foundation.boot;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Predicate;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+
+import cokr.xit.foundation.Assert;
+import cokr.xit.foundation.data.StringMap;
+
+@Configuration("staticResource")
+public class StaticResourceConfig {
+ @Value("${spring.mvc.static-path-pattern}")
+ private String staticURLs;
+ @Value("${spring.web.resources.static-locations}")
+ private String staticLocations;
+
+ public Map getMappings() {
+ if (Assert.isEmpty(staticURLs) && Assert.isEmpty(staticLocations))
+ return Collections.emptyMap();
+
+ List
+ urls = Arrays.asList(staticURLs.split(",")),
+ locations = Arrays.asList(staticLocations.split(","));
+ if (urls.size() != locations.size())
+ throw new RuntimeException("URLs and locations do not match in size for static resources");
+
+ StringMap resMap = new StringMap<>();
+ for (int i = 0; i < urls.size(); ++i) {
+ String url = urls.get(i),
+ location = locations.get(i);
+ resMap.put(url.trim(), location.trim());
+ }
+
+ return resMap;
+ }
+
+ public String[] getURLs(Predicate> filter) {
+ Predicate> test = filter != null ? filter : entry -> true;
+ List urls = getMappings().entrySet().stream()
+ .filter(test)
+ .map(entry -> entry.getKey())
+ .toList();
+ return urls.toArray(new String[urls.size()]);
+ }
+
+ public String[] getLocations() {
+ List locations = getMappings().values().stream().toList();
+ return locations.toArray(new String[locations.size()]);
+ }
+}
\ No newline at end of file