|
|
|
@ -60,9 +60,15 @@ public class WebSecurityConfig extends |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
.frameOptions().sameOrigin() |
|
|
|
headers |
|
|
|
.httpStrictTransportSecurity().disable(); |
|
|
|
.frameOptions(frameOptions -> |
|
|
|
|
|
|
|
frameOptions.sameOrigin() |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.httpStrictTransportSecurity(hsts -> |
|
|
|
|
|
|
|
hsts.disable() |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
@ -92,15 +98,17 @@ If you are using Spring Security's Java Configuration the following will only ad |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
|
|
|
|
headers |
|
|
|
// do not use any default headers unless explicitly listed |
|
|
|
// do not use any default headers unless explicitly listed |
|
|
|
.defaultsDisabled() |
|
|
|
.defaultsDisabled() |
|
|
|
.cacheControl(); |
|
|
|
.cacheControl(withDefaults()) |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -126,12 +134,14 @@ If necessary, you can disable all of the HTTP Security response headers with the |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers().disable(); |
|
|
|
.headers(headers -> |
|
|
|
} |
|
|
|
headers.disable() |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -182,14 +192,16 @@ Similarly, you can enable only cache control within Java Configuration with the |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
|
|
|
|
headers |
|
|
|
.defaultsDisabled() |
|
|
|
.defaultsDisabled() |
|
|
|
.cacheControl(); |
|
|
|
.cacheControl(withDefaults()) |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -263,14 +275,16 @@ If you want more control over the headers, you can explicitly specify the conten |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
|
|
|
|
headers |
|
|
|
.defaultsDisabled() |
|
|
|
.defaultsDisabled() |
|
|
|
.contentTypeOptions(); |
|
|
|
.contentTypeOptions(withDefaults()) |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -327,16 +341,20 @@ Similarly, you can enable only HSTS headers with Java Configuration: |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
.httpStrictTransportSecurity() |
|
|
|
headers |
|
|
|
.includeSubdomains(true) |
|
|
|
.httpStrictTransportSecurity(hsts -> |
|
|
|
|
|
|
|
hsts |
|
|
|
|
|
|
|
.includeSubDomains(true) |
|
|
|
.preload(true) |
|
|
|
.preload(true) |
|
|
|
.maxAgeSeconds(31536000); |
|
|
|
.maxAgeInSeconds(31536000) |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -403,11 +421,15 @@ WebSecurityConfigurerAdapter { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
.httpPublicKeyPinning() |
|
|
|
headers |
|
|
|
.includeSubdomains(true) |
|
|
|
.httpPublicKeyPinning(hpkp -> |
|
|
|
|
|
|
|
hpkp |
|
|
|
|
|
|
|
.includeSubDomains(true) |
|
|
|
.reportUri("https://example.net/pkp-report") |
|
|
|
.reportUri("https://example.net/pkp-report") |
|
|
|
.addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=", "E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g="; |
|
|
|
.addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=", "E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g=") |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
@ -461,14 +483,18 @@ Similarly, you can customize frame options to use the same origin within Java Co |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
.frameOptions() |
|
|
|
headers |
|
|
|
.sameOrigin(); |
|
|
|
.frameOptions(frameOptions -> |
|
|
|
} |
|
|
|
frameOptions |
|
|
|
|
|
|
|
.sameOrigin() |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -511,14 +537,18 @@ Similarly, you can customize XSS protection within Java Configuration with the f |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
.xssProtection() |
|
|
|
headers |
|
|
|
.block(false); |
|
|
|
.xssProtection(xssProtection -> |
|
|
|
} |
|
|
|
xssProtection |
|
|
|
|
|
|
|
.block(false) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -625,13 +655,18 @@ Similarly, you can enable the CSP header using Java configuration as shown below |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
.contentSecurityPolicy("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/"); |
|
|
|
headers |
|
|
|
} |
|
|
|
.contentSecurityPolicy(csp -> |
|
|
|
|
|
|
|
csp |
|
|
|
|
|
|
|
.policyDirectives("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/") |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -643,14 +678,19 @@ To enable the CSP _'report-only'_ header, provide the following Java configurati |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
.contentSecurityPolicy("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/") |
|
|
|
headers |
|
|
|
.reportOnly(); |
|
|
|
.contentSecurityPolicy(csp -> |
|
|
|
} |
|
|
|
csp |
|
|
|
|
|
|
|
.policyDirectives("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/") |
|
|
|
|
|
|
|
.reportOnly() |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -707,13 +747,18 @@ Similarly, you can enable the Referrer Policy header using Java configuration as |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
.referrerPolicy(ReferrerPolicy.SAME_ORIGIN); |
|
|
|
headers |
|
|
|
} |
|
|
|
.referrerPolicy(referrerPolicy -> |
|
|
|
|
|
|
|
referrerPolicy |
|
|
|
|
|
|
|
.policy(ReferrerPolicy.SAME_ORIGIN) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -757,13 +802,15 @@ Similarly, you can enable the Feature Policy header using Java configuration as |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
.featurePolicy("geolocation 'self'"); |
|
|
|
headers |
|
|
|
} |
|
|
|
.featurePolicy("geolocation 'self'") |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -804,13 +851,15 @@ Similarly, the headers could be added to the response using Java Configuration a |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
.addHeaderWriter(new StaticHeadersWriter("X-Custom-Security-Header","header-value")); |
|
|
|
headers |
|
|
|
} |
|
|
|
.addHeaderWriter(new StaticHeadersWriter("X-Custom-Security-Header","header-value")) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -849,13 +898,15 @@ We could also restrict framing of content to the same origin with Java configura |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsMode.SAMEORIGIN)); |
|
|
|
headers |
|
|
|
} |
|
|
|
.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsMode.SAMEORIGIN)) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
@ -903,17 +954,21 @@ We could also prevent framing of content to the log in page using java configura |
|
|
|
public class WebSecurityConfig extends |
|
|
|
public class WebSecurityConfig extends |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
RequestMatcher matcher = new AntPathRequestMatcher("/login"); |
|
|
|
RequestMatcher matcher = new AntPathRequestMatcher("/login"); |
|
|
|
DelegatingRequestMatcherHeaderWriter headerWriter = |
|
|
|
DelegatingRequestMatcherHeaderWriter headerWriter = |
|
|
|
new DelegatingRequestMatcherHeaderWriter(matcher,new XFrameOptionsHeaderWriter()); |
|
|
|
new DelegatingRequestMatcherHeaderWriter(matcher,new XFrameOptionsHeaderWriter()); |
|
|
|
http |
|
|
|
http |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
.headers() |
|
|
|
.headers(headers -> |
|
|
|
.frameOptions().disabled() |
|
|
|
headers |
|
|
|
.addHeaderWriter(headerWriter); |
|
|
|
.frameOptions(frameOptions -> |
|
|
|
} |
|
|
|
frameOptions.disable() |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.addHeaderWriter(headerWriter) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
|