Skip to content

Commit 86b25fc

Browse files
committed
refactor: Refactored to remove hasH2Console boolean flag and replace it with a check on the H2 console properties/path.
Fix #1307
1 parent 98c17a3 commit 86b25fc

File tree

3 files changed

+6
-19
lines changed

3 files changed

+6
-19
lines changed

src/main/java/ru/mystamps/web/support/spring/security/ContentSecurityPolicyHeaderWriter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ class ContentSecurityPolicyHeaderWriter implements HeaderWriter {
151151

152152
private final boolean useCdn;
153153
private final boolean useSingleHost;
154-
private final boolean hasH2Console;
155154
private final String host;
156155
private final String h2ConsolePath;
157156

@@ -167,7 +166,7 @@ public void writeHeaders(HttpServletRequest request, HttpServletResponse respons
167166
protected String constructDirectives(String uri) {
168167
boolean onCollectionInfoPage = uri.startsWith(COLLECTION_INFO_PAGE_PATTERN);
169168
boolean onAddSeriesPage = uri.equals(SeriesUrl.ADD_SERIES_PAGE);
170-
boolean onH2ConsolePage = hasH2Console && uri.startsWith(h2ConsolePath);
169+
boolean onH2ConsolePage = h2ConsolePath != null && uri.startsWith(h2ConsolePath);
171170

172171
StringBuilder sb = new StringBuilder();
173172

src/main/java/ru/mystamps/web/support/spring/security/SecurityConfig.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,21 @@ public void configure(WebSecurity web) throws Exception {
8787
protected void configure(HttpSecurity http) throws Exception {
8888
boolean useSingleHost = !environment.acceptsProfiles("prod");
8989
boolean useCdn = environment.getProperty("app.use-cdn", Boolean.class, Boolean.TRUE);
90-
boolean hasH2Console = environment.acceptsProfiles("test");
9190

9291
// @todo #226 Introduce app.use-public-hostname property
9392
boolean usePublicHostname = environment.acceptsProfiles("prod");
9493
String hostname = usePublicHostname ? SiteUrl.PUBLIC_URL : SiteUrl.SITE;
9594

96-
String h2ConsolePath = hasH2Console ? h2ConsoleProperties.getPath() : null;
95+
String h2ConsolePath = h2ConsoleProperties != null ? h2ConsoleProperties.getPath() : null;
9796

9897
// Allow unsecured requests to H2 consoles if available.
9998
// See also spring.h2.console.path in application-test.properties
10099
String[] pathsToIgnore =
101-
hasH2Console ? new String[]{h2ConsolePath + "/**", SiteUrl.CSP_REPORTS_HANDLER}
102-
: new String[]{SiteUrl.CSP_REPORTS_HANDLER};
100+
h2ConsolePath != null ? new String[]{h2ConsolePath + "/**", SiteUrl.CSP_REPORTS_HANDLER}
101+
: new String[]{SiteUrl.CSP_REPORTS_HANDLER};
103102

104103
ContentSecurityPolicyHeaderWriter cspWriter =
105-
new ContentSecurityPolicyHeaderWriter(useCdn, useSingleHost, hasH2Console, hostname, h2ConsolePath);
104+
new ContentSecurityPolicyHeaderWriter(useCdn, useSingleHost, hostname, h2ConsolePath);
106105

107106
http
108107
.authorizeRequests()

src/test/java/ru/mystamps/web/support/spring/security/ContentSecurityPolicyHeaderWriterTest.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public class ContentSecurityPolicyHeaderWriterTest implements WithAssertions {
5151
public void writeContentSecurityPolicyHeader() {
5252
// given
5353
ContentSecurityPolicyHeaderWriter writer = new ContentSecurityPolicyHeaderWriter(
54-
bool(),
5554
bool(),
5655
bool(),
5756
Random.host(),
@@ -85,7 +84,6 @@ public void onIndexPageWithLocalResources() {
8584
ContentSecurityPolicyHeaderWriter writer = new ContentSecurityPolicyHeaderWriter(
8685
false,
8786
true,
88-
bool(),
8987
SiteUrl.SITE,
9088
H2_CONSOLE_PATH
9189
);
@@ -108,7 +106,6 @@ public void onIndexPageWithResourcesFromCdn() {
108106
ContentSecurityPolicyHeaderWriter writer = new ContentSecurityPolicyHeaderWriter(
109107
true,
110108
false,
111-
bool(),
112109
SiteUrl.PUBLIC_URL,
113110
H2_CONSOLE_PATH
114111
);
@@ -144,7 +141,6 @@ public void onCollectionInfoPageWithLocalResources() {
144141
ContentSecurityPolicyHeaderWriter writer = new ContentSecurityPolicyHeaderWriter(
145142
false,
146143
true,
147-
bool(),
148144
Random.host(),
149145
H2_CONSOLE_PATH
150146
);
@@ -176,7 +172,6 @@ public void onCollectionInfoPageWithResourcesFromCdn() {
176172
ContentSecurityPolicyHeaderWriter writer = new ContentSecurityPolicyHeaderWriter(
177173
true,
178174
false,
179-
bool(),
180175
Random.host(),
181176
H2_CONSOLE_PATH
182177
);
@@ -211,7 +206,6 @@ public void onSeriesAddImagePageWithLocalResources() {
211206
ContentSecurityPolicyHeaderWriter writer = new ContentSecurityPolicyHeaderWriter(
212207
false,
213208
true,
214-
bool(),
215209
Random.host(),
216210
H2_CONSOLE_PATH
217211
);
@@ -239,7 +233,6 @@ public void onSeriesAddImagePageWithResourcesFromCdn() {
239233
ContentSecurityPolicyHeaderWriter writer = new ContentSecurityPolicyHeaderWriter(
240234
true,
241235
false,
242-
bool(),
243236
Random.host(),
244237
H2_CONSOLE_PATH
245238
);
@@ -277,7 +270,6 @@ public void onSeriesAddPageWithLocalResources() {
277270
ContentSecurityPolicyHeaderWriter writer = new ContentSecurityPolicyHeaderWriter(
278271
false,
279272
true,
280-
bool(),
281273
Random.host(),
282274
H2_CONSOLE_PATH
283275
);
@@ -310,7 +302,6 @@ public void onSeriesAddPageWithResourcesFromCdn() {
310302
ContentSecurityPolicyHeaderWriter writer = new ContentSecurityPolicyHeaderWriter(
311303
true,
312304
false,
313-
bool(),
314305
Random.host(),
315306
H2_CONSOLE_PATH
316307
);
@@ -346,7 +337,6 @@ public void onH2ConsoleWithLocalResources() {
346337
ContentSecurityPolicyHeaderWriter writer = new ContentSecurityPolicyHeaderWriter(
347338
false,
348339
true,
349-
true,
350340
Random.host(),
351341
H2_CONSOLE_PATH
352342
);
@@ -379,9 +369,8 @@ public void onH2ConsoleWithResourcesFromCdn() {
379369
ContentSecurityPolicyHeaderWriter writer = new ContentSecurityPolicyHeaderWriter(
380370
true,
381371
false,
382-
false,
383372
Random.host(),
384-
H2_CONSOLE_PATH
373+
null
385374
);
386375
String[] directives = writer.constructDirectives("/console/").split(";");
387376

0 commit comments

Comments
 (0)