Skip to content

Commit 2ba0100

Browse files
Dave SyerPhillip Webb
authored andcommitted
Add headers external properties for security filters
1 parent 759aa78 commit 2ba0100

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/SecurityAutoConfiguration.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;
3030
import org.springframework.boot.actuate.properties.ManagementServerProperties;
3131
import org.springframework.boot.actuate.properties.SecurityProperties;
32+
import org.springframework.boot.actuate.properties.SecurityProperties.Headers;
3233
import org.springframework.boot.actuate.properties.SecurityProperties.User;
3334
import org.springframework.boot.actuate.web.ErrorController;
3435
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -52,8 +53,11 @@
5253
import org.springframework.security.config.annotation.web.builders.WebSecurity.IgnoredRequestConfigurer;
5354
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
5455
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
56+
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
5557
import org.springframework.security.web.AuthenticationEntryPoint;
5658
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
59+
import org.springframework.security.web.header.writers.HstsHeaderWriter;
60+
import org.springframework.security.web.util.AnyRequestMatcher;
5761

5862
/**
5963
* {@link EnableAutoConfiguration Auto-configuration} for security of a web application or
@@ -149,11 +153,15 @@ protected void configure(HttpSecurity http) throws Exception {
149153
.and().httpBasic() //
150154
.and().anonymous().disable();
151155
}
152-
// Remove this when session creation is disabled by default
153-
http.csrf().disable();
156+
if (!this.security.isEnableCsrf()) {
157+
http.csrf().disable();
158+
}
154159
// No cookies for application endpoints by default
155160
http.sessionManagement().sessionCreationPolicy(this.security.getSessions());
156161

162+
SecurityAutoConfiguration.configureHeaders(http.headers(),
163+
this.security.getHeaders());
164+
157165
}
158166

159167
private String[] getSecureApplicationPaths() {
@@ -234,6 +242,9 @@ protected void configure(HttpSecurity http) throws Exception {
234242
http.sessionManagement().sessionCreationPolicy(
235243
this.security.getManagement().getSessions());
236244

245+
SecurityAutoConfiguration.configureHeaders(http.headers(),
246+
this.security.getHeaders());
247+
237248
}
238249

239250
@Override
@@ -299,4 +310,26 @@ public AuthenticationManager authenticationManager() throws Exception {
299310

300311
}
301312

313+
private static void configureHeaders(HeadersConfigurer<?> configurer,
314+
SecurityProperties.Headers headers) throws Exception {
315+
if (headers.getHsts() != Headers.HSTS.none) {
316+
boolean includeSubdomains = headers.getHsts() == Headers.HSTS.all;
317+
HstsHeaderWriter writer = new HstsHeaderWriter(includeSubdomains);
318+
writer.setRequestMatcher(new AnyRequestMatcher());
319+
configurer.addHeaderWriter(writer);
320+
}
321+
if (headers.isContentType()) {
322+
configurer.contentTypeOptions();
323+
}
324+
if (headers.isXss()) {
325+
configurer.xssProtection();
326+
}
327+
if (headers.isCache()) {
328+
configurer.cacheControl();
329+
}
330+
if (headers.isFrame()) {
331+
configurer.frameOptions();
332+
}
333+
}
334+
302335
}

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/properties/SecurityProperties.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ public class SecurityProperties {
3232

3333
private boolean requireSsl;
3434

35+
// Flip this when session creation is disabled by default
36+
private boolean enableCsrf = false;
37+
3538
private Basic basic = new Basic();
3639

40+
private Headers headers = new Headers();
41+
3742
private SessionCreationPolicy sessions = SessionCreationPolicy.STATELESS;
3843

3944
private String[] ignored = new String[] { "/css/**", "/js/**", "/images/**",
@@ -43,6 +48,10 @@ public class SecurityProperties {
4348

4449
private User user = new User();
4550

51+
public Headers getHeaders() {
52+
return this.headers;
53+
}
54+
4655
public User getUser() {
4756
return this.user;
4857
}
@@ -75,6 +84,14 @@ public void setRequireSsl(boolean requireSsl) {
7584
this.requireSsl = requireSsl;
7685
}
7786

87+
public boolean isEnableCsrf() {
88+
return this.enableCsrf;
89+
}
90+
91+
public void setEnableCsrf(boolean enableCsrf) {
92+
this.enableCsrf = enableCsrf;
93+
}
94+
7895
public void setIgnored(String... ignored) {
7996
this.ignored = ignored;
8097
}
@@ -83,6 +100,64 @@ public String[] getIgnored() {
83100
return this.ignored;
84101
}
85102

103+
public static class Headers {
104+
105+
public static enum HSTS {
106+
none, domain, all
107+
}
108+
109+
private boolean xss;
110+
111+
private boolean cache;
112+
113+
private boolean frame;
114+
115+
private boolean contentType;
116+
117+
private HSTS hsts = HSTS.all;
118+
119+
public boolean isXss() {
120+
return this.xss;
121+
}
122+
123+
public void setXss(boolean xss) {
124+
this.xss = xss;
125+
}
126+
127+
public boolean isCache() {
128+
return this.cache;
129+
}
130+
131+
public void setCache(boolean cache) {
132+
this.cache = cache;
133+
}
134+
135+
public boolean isFrame() {
136+
return this.frame;
137+
}
138+
139+
public void setFrame(boolean frame) {
140+
this.frame = frame;
141+
}
142+
143+
public boolean isContentType() {
144+
return this.contentType;
145+
}
146+
147+
public void setContentType(boolean contentType) {
148+
this.contentType = contentType;
149+
}
150+
151+
public HSTS getHsts() {
152+
return this.hsts;
153+
}
154+
155+
public void setHsts(HSTS hsts) {
156+
this.hsts = hsts;
157+
}
158+
159+
}
160+
86161
public static class Basic {
87162

88163
private boolean enabled = true;

0 commit comments

Comments
 (0)