외부 static resource 서비스 추가

master
mjkhan21 1 year ago
parent 08e522e745
commit 73199eab12

@ -1,6 +1,9 @@
package cokr.xit.foundation.boot; package cokr.xit.foundation.boot;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@ -24,6 +27,8 @@ import cokr.xit.foundation.web.AccessInitializer;
@Configuration @Configuration
public class MvcConfig implements WebMvcConfigurer { public class MvcConfig implements WebMvcConfigurer {
protected static String[] URL_PATTERNS = {"/", "/**/*.do"}; protected static String[] URL_PATTERNS = {"/", "/**/*.do"};
@Resource(name = "staticResource")
private StaticResourceConfig staticResource;
/**AccessInitializer . /**AccessInitializer .
* @return AccessInitializer * @return AccessInitializer
@ -33,6 +38,9 @@ public class MvcConfig implements WebMvcConfigurer {
return new AccessInitializer(); return new AccessInitializer();
} }
@Value("${spring.web.resources.static-locations}")
private String staticLocations;
/** . /** .
* <ul><li>url: /resources/** </li> * <ul><li>url: /resources/** </li>
* <li>: /resources/</li> * <li>: /resources/</li>
@ -40,9 +48,11 @@ public class MvcConfig implements WebMvcConfigurer {
*/ */
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
staticResource.getMappings().forEach((k, v) ->
registry registry
.addResourceHandler("/resources/**", "/files/**") .addResourceHandler(k)
.addResourceLocations("/resources/", "/files/"); .addResourceLocations(v)
);
} }
/**AccessInitializer interceptor . /**AccessInitializer interceptor .

@ -1,6 +1,8 @@
package cokr.xit.foundation.boot; 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.apache.catalina.servlets.DefaultServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
@ -17,6 +19,8 @@ import cokr.xit.foundation.web.AccessFilter;
@Configuration @Configuration
public class ServletConfig { public class ServletConfig {
protected static String[] URL_PATTERNS = {"/", "*.do"}; protected static String[] URL_PATTERNS = {"/", "*.do"};
@Resource(name = "staticResource")
private StaticResourceConfig staticResource;
/**CharacterEncodingFilter . /**CharacterEncodingFilter .
* @return FilterRegistrationBean * @return FilterRegistrationBean
@ -47,18 +51,20 @@ public class ServletConfig {
*/ */
@Bean @Bean
public ServletRegistrationBean<DefaultServlet> defaultServlet() { public ServletRegistrationBean<DefaultServlet> defaultServlet() {
String[] urls = staticResource.getURLs(entry -> !entry.getValue().startsWith("file:"));
for (int i = 0; i <urls.length; ++i)
urls[i] = urls[i].replace("/**", "/*");
ServletRegistrationBean<DefaultServlet> bean = new ServletRegistrationBean<>( ServletRegistrationBean<DefaultServlet> bean = new ServletRegistrationBean<>(
new DefaultServlet(), new DefaultServlet(),
"/resources/*", urls
"/files/*"
); );
bean.setLoadOnStartup(1); bean.setLoadOnStartup(1);
bean.setInitParameters(Map.of(
"debug", "0",
"listings", "false"
));
HashMap<String, String> params = new HashMap<>();
params.put("debug", "0");
params.put("listings", "false");
bean.setInitParameters(params);
return bean; return bean;
} }
} }

@ -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<String, String> getMappings() {
if (Assert.isEmpty(staticURLs) && Assert.isEmpty(staticLocations))
return Collections.emptyMap();
List<String>
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<String> 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<Map.Entry<String, String>> filter) {
Predicate<Map.Entry<String, String>> test = filter != null ? filter : entry -> true;
List<String> urls = getMappings().entrySet().stream()
.filter(test)
.map(entry -> entry.getKey())
.toList();
return urls.toArray(new String[urls.size()]);
}
public String[] getLocations() {
List<String> locations = getMappings().values().stream().toList();
return locations.toArray(new String[locations.size()]);
}
}
Loading…
Cancel
Save