Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbdd3f22

Browse files
authored
Add cloudfront CDN and CORS filter (#84)
1 parent379046a commitbdd3f22

File tree

9 files changed

+62
-14
lines changed

9 files changed

+62
-14
lines changed

‎stubbornjava-common/src/main/java/com/stubbornjava/common/TemplateHelpers.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
importjava.time.format.DateTimeFormatter;
55

66
importcom.github.jknack.handlebars.Options;
7+
importcom.google.common.base.Strings;
8+
importcom.typesafe.config.Config;
79

810
publicclassTemplateHelpers {
911
staticfinalDateTimeFormatterMMMddyyyyFmt =DateTimeFormatter.ofPattern("MMM dd, yyyy");
@@ -12,4 +14,16 @@ public static CharSequence dateFormat(String dateString, Options options) {
1214
LocalDateTimedate =LocalDateTime.parse(dateString);
1315
returnMMMddyyyyFmt.format(date);
1416
}
17+
18+
privatestaticfinalStringcdnHost =Configs.<String>getOrDefault(Configs.properties(),
19+
"cdn.host",
20+
Config::getString,
21+
() ->null);
22+
// This expects the url to be relative (eg. /static/img.jpg)
23+
publicstaticCharSequencecdn(Stringurl) {
24+
if (Strings.isNullOrEmpty(cdnHost)) {
25+
returnurl;
26+
}
27+
returncdnHost +url;
28+
}
1529
}

‎stubbornjava-common/src/main/java/com/stubbornjava/common/Templating.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Templating {
3131
static {
3232
Templating.Builderbuilder =
3333
newTemplating.Builder()
34-
.withHelper("dateFormat",TemplateHelpers::dateFormat)
34+
.withHelpers(newTemplateHelpers())
3535
.withHelper("md",newMarkdownHelper())
3636
.withHelper(AssignHelper.NAME,AssignHelper.INSTANCE)
3737
.register(HumanizeHelper::register);
@@ -148,6 +148,12 @@ public <T> Builder withHelper(String helperName, Helper<T> helper) {
148148
returnthis;
149149
}
150150

151+
public <T>BuilderwithHelpers(Objecthelpers) {
152+
log.debug("using template helpers {}" ,helpers.getClass());
153+
handlebars.registerHelpers(helpers);
154+
returnthis;
155+
}
156+
151157
public <T>Builderregister(Consumer<Handlebars>consumer) {
152158
log.debug("registering helpers");
153159
consumer.accept(handlebars);

‎stubbornjava-common/src/main/java/com/stubbornjava/common/undertow/Headers.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@ default Optional<String> getHeader(HttpServerExchange exchange, String header) {
1717
RequestHeaderAttributereqHeader =newRequestHeaderAttribute(newHttpString(header));
1818
returnOptional.ofNullable(reqHeader.readAttribute(exchange));
1919
}
20+
21+
defaultvoidsetHeader(HttpServerExchangeexchange,HttpStringheader,Stringvalue) {
22+
exchange.getResponseHeaders().add(header,value);
23+
}
24+
25+
defaultvoidsetHeader(HttpServerExchangeexchange,Stringheader,Stringvalue) {
26+
exchange.getResponseHeaders().add(newHttpString(header),value);
27+
}
2028
}

‎stubbornjava-common/src/main/java/com/stubbornjava/common/undertow/handlers/CustomHandlers.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
importjava.io.File;
55
importjava.nio.file.Paths;
6+
importjava.util.Set;
67
importjava.util.SortedMap;
78

89
importorg.slf4j.Logger;
@@ -168,4 +169,18 @@ public static HttpHandler securityHeaders(HttpHandler next, ReferrerPolicy polic
168169
returnsecurity.complete(next);
169170
}
170171
// {{end:securityHeaders}}
172+
173+
publicstaticHttpHandlercorsOriginWhitelist(HttpHandlernext,Set<String>originWhitelist) {
174+
returnexchange -> {
175+
Stringorigin =Exchange.headers()
176+
.getHeader(exchange,Headers.ORIGIN)
177+
.orElse("");
178+
log.debug("Origin: {} Whitelist: {}",origin,originWhitelist);
179+
if (originWhitelist.contains(origin)) {
180+
log.debug("Origin whitelist matched adding CORS header");
181+
Exchange.headers().setHeader(exchange,"Access-Control-Allow-Origin",origin);
182+
}
183+
next.handleRequest(exchange);
184+
};
185+
}
171186
}

‎stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/StubbornJavaWebApp.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
importorg.slf4j.Logger;
88
importorg.slf4j.LoggerFactory;
99

10+
importcom.google.common.collect.Sets;
1011
importcom.stubbornjava.common.seo.SitemapRoutes;
1112
importcom.stubbornjava.common.undertow.SimpleServer;
1213
importcom.stubbornjava.common.undertow.handlers.CustomHandlers;
@@ -37,27 +38,28 @@ private static HttpHandler exceptionHandler(HttpHandler next) {
3738
// {{start:csp}}
3839
privatestaticHttpHandlercontentSecurityPolicy(HttpHandlerdelegate) {
3940
returnnewContentSecurityPolicyHandler.Builder()
40-
.defaultSrc(ContentSecurityPolicy.SELF)
41-
.scriptSrc(ContentSecurityPolicy.SELF.getValue(),"https://www.google-analytics.com")
41+
.defaultSrc(ContentSecurityPolicy.SELF.getValue(),"https://*.stubbornjava.com")
42+
.scriptSrc(ContentSecurityPolicy.SELF.getValue(),"https://*.stubbornjava.com","https://www.google-analytics.com","data:")
4243
// Drop the wildcard when we host our own images.
43-
.imgSrc(ContentSecurityPolicy.SELF.getValue(),"https://www.google-analytics.com","*")
44-
.connectSrc(ContentSecurityPolicy.SELF.getValue(),"https://www.google-analytics.com")
45-
.fontSrc(ContentSecurityPolicy.SELF.getValue(),"data:")
46-
.styleSrc(ContentSecurityPolicy.SELF.getValue(),ContentSecurityPolicy.UNSAFE_INLINE.getValue())
44+
.imgSrc(ContentSecurityPolicy.SELF.getValue(),"https://*.stubbornjava.com","https://www.google-analytics.com","data:")
45+
.connectSrc(ContentSecurityPolicy.SELF.getValue(),"https://*.stubbornjava.com","https://www.google-analytics.com")
46+
.fontSrc(ContentSecurityPolicy.SELF.getValue(),"https://*.stubbornjava.com","data:")
47+
.styleSrc(ContentSecurityPolicy.SELF.getValue(),ContentSecurityPolicy.UNSAFE_INLINE.getValue(),"https://*.stubbornjava.com")
4748
.build(delegate);
4849
}
4950
// {{end:csp}}
5051

5152
// {{start:middleware}}
5253
privatestaticHttpHandlerwrapWithMiddleware(HttpHandlernext) {
53-
returnMiddlewareBuilder.begin(PageRoutes::redirector)
54+
returnMiddlewareBuilder.begin(ex ->CustomHandlers.accessLog(ex,logger))
55+
.next(StubbornJavaWebApp::exceptionHandler)
56+
.next(CustomHandlers::statusCodeMetrics)
5457
.next(handler ->CustomHandlers.securityHeaders(handler,ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN))
5558
.next(StubbornJavaWebApp::contentSecurityPolicy)
5659
.next(CustomHandlers::gzip)
60+
.next(h ->CustomHandlers.corsOriginWhitelist(next,Sets.newHashSet("https://www.stubbornjava.com")))
61+
.next(PageRoutes::redirector)
5762
.next(BlockingHandler::new)
58-
.next(ex ->CustomHandlers.accessLog(ex,logger))
59-
.next(CustomHandlers::statusCodeMetrics)
60-
.next(StubbornJavaWebApp::exceptionHandler)
6163
.complete(next);
6264
}
6365
// {{end:middleware}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cdn {
2+
host="https://cdn.stubbornjava.com"
3+
}

‎stubbornjava-webapp/ui/src/common/head.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<metahttp-equiv="x-ua-compatible"content="ie=edge"/>
44
<metaname="description"content="{{#ifmetaDesc}}{{metaDesc}}{{/if}}"/>
55
{{!-- <link rel="stylesheet" href="/css/3rdparty.css"/>--}}
6-
<linkrel="stylesheet"href="/assets/css/common.css"/>
6+
<linkrel="stylesheet"href="{{cdn"/assets/css/common.css"}}"/>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{{!-- <script async type="text/javascript" src="/js/3rdparty.js"></script> --}}
22
{{!-- Making this script async breaks the jump to id functionality :( --}}
3-
<scripttype="text/javascript"src="/assets/js/common.js"></script>
3+
<scripttype="text/javascript"src="{{cdn"/assets/js/common.js"}}"></script>

‎stubbornjava-webapp/ui/src/widgets/nav/header.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<navclass="navbar navbar-inverse navbar-expand fixed-top"role="navigation">
22
<aclass="navbar-brand logo"href="/">
3-
<spanclass="align-middle"><imgsrc="/assets/images/PenguinHQ_compressed.png"height="40px"> Stubborn<spanclass="java">Java</span></span>
3+
<spanclass="align-middle"><imgsrc="{{cdn"/assets/images/PenguinHQ_compressed.png"}}"height="40px"> Stubborn<spanclass="java">Java</span></span>
44
{{!-- Add this SVG instead later. I can't get CSS to work --}}
55
{{!-- <span>{{> templates/src/widgets/nav/logo }} Stubborn<span class="java">Java</span></span> --}}
66
</a>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp