From 13237626f3956d93a91a94bee6fee6aa86134a06 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 27 Mar 2018 22:13:16 +0300 Subject: www: spring boot autoconfigured static resources + cache busting using Spring --- juick-www/src/main/java/com/juick/www/WebApp.java | 25 ++++------------------ .../juick/www/configuration/WebSecurityConfig.java | 3 ++- .../www/configuration/WwwAppConfiguration.java | 22 ------------------- 3 files changed, 6 insertions(+), 44 deletions(-) (limited to 'juick-www/src/main/java/com/juick') diff --git a/juick-www/src/main/java/com/juick/www/WebApp.java b/juick-www/src/main/java/com/juick/www/WebApp.java index 94c12528..4e8b3a11 100644 --- a/juick-www/src/main/java/com/juick/www/WebApp.java +++ b/juick-www/src/main/java/com/juick/www/WebApp.java @@ -16,20 +16,16 @@ */ package com.juick.www; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.Tag; import com.juick.service.TagService; -import org.apache.commons.io.IOUtils; import org.springframework.stereotype.Component; +import org.springframework.web.servlet.resource.ResourceUrlProvider; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.stream.Stream; @@ -42,20 +38,7 @@ public class WebApp { @Inject private TagService tagService; @Inject - private ObjectMapper jsonMapper; - - private String scriptsUrl; - private String styleUrl; - - @PostConstruct - public void init() throws IOException { - String manifestString = IOUtils.toString(getClass().getClassLoader(). - getResourceAsStream("manifest.json"), StandardCharsets.UTF_8); - HashMap manifest = jsonMapper.readValue(manifestString, - new TypeReference>() {}); - scriptsUrl = manifest.get("scripts.js"); - styleUrl = manifest.get("style.css"); - } + private ResourceUrlProvider resourceUrlProvider; public List parseTags(String tagsStr) { List tags = new ArrayList<>(); @@ -79,10 +62,10 @@ public class WebApp { } public String getStyleUrl() { - return styleUrl; + return resourceUrlProvider.getForLookupPath("/style.css"); } public String getScriptsUrl() { - return scriptsUrl; + return resourceUrlProvider.getForLookupPath("/scripts.js"); } } diff --git a/juick-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java b/juick-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java index 19329dad..65871088 100644 --- a/juick-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java +++ b/juick-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java @@ -72,7 +72,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { http.addFilterAfter(hashParamAuthenticationFilter(), BasicAuthenticationFilter.class); http .authorizeRequests() - .antMatchers("/settings", "/pm/**", "/**/bl", "/_twitter", "/post", "/comment").authenticated() + .antMatchers("/settings", "/pm/**", "/**/bl", "/_twitter", "/post", "/comment") + .authenticated() .anyRequest().permitAll() .and() .anonymous().principal(JuickUser.ANONYMOUS_USER).authorities(JuickUser.ANONYMOUS_AUTHORITY) diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java index 9c35f1c2..34720c33 100644 --- a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java @@ -41,16 +41,11 @@ import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; -import org.springframework.core.Ordered; -import org.springframework.http.CacheControl; import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import org.springframework.web.servlet.resource.PathResourceResolver; import javax.inject.Inject; import java.util.Collections; -import java.util.concurrent.TimeUnit; /** * Created by aalexeev on 11/22/16. @@ -97,23 +92,6 @@ public class WwwAppConfiguration implements WebMvcConfigurer { public CloudflareCache cloudflareCache() { return new CloudflareCache(); } - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.setOrder(Ordered.HIGHEST_PRECEDENCE); - registry.addResourceHandler( - "/scripts*.js*", - "/style*.css*", - "/*.png", - "/favicon.ico") - .addResourceLocations("classpath:/") - .setCacheControl(CacheControl.maxAge(30, TimeUnit.DAYS).mustRevalidate().cachePublic()) - .resourceChain(true) - .addResolver(new PathResourceResolver()); - - registry.addResourceHandler("/static/**") - .addResourceLocations("/static/") - .setCacheControl(CacheControl.maxAge(30, TimeUnit.DAYS).mustRevalidate().cachePublic()); - } @Bean public Loader templateLoader() { return new ClasspathLoader(); -- cgit v1.2.3