Skip to content

Commit 1573e32

Browse files
committed
Adjustments according to feedback
1 parent 69fcaef commit 1573e32

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/sql.adoc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,22 @@ You can customize the console's path by using the configprop:spring.h2.console.p
324324

325325
[[features.sql.h2-web-console.spring-security]]
326326
==== Configuring Spring Security for H2 Console
327-
H2 Console uses frames and, as it's intended for development only, does not implement CSRF protection measures. If your application uses Spring Security, you need to configure it accordingly.
327+
H2 Console uses frames and, as it's intended for development only, does not implement CSRF protection measures. If your application uses Spring Security, you need to configure it to
328328

329-
For example, Spring Security will ignore the console if the following `WebSecurityCustomizer` is exposed:
329+
* disable CSRF protection for requests against the console,
330+
* set the header `X-Frame-Options` to `SAMEORIGIN` on responses from the console.
331+
332+
More information on {spring-security-docs}#csrf[CSRF] and the header {spring-security-docs}#headers-frame-options[X-Frame-Options] can be found in the Spring Security Reference Guide.
333+
334+
In simple setups, a `SecurityFilterChain` like the following can be used:
330335

331336
[source,java,indent=0,subs="verbatim"]
332337
----
333-
include::{docs-java}/features/sql/h2webconsole/springsecurity/MySecurityConfiguration.java[]
338+
include::{docs-java}/features/sql/h2webconsole/springsecurity/DevProfileSecurityConfiguration.java[]
334339
----
335340

341+
WARNING: The H2 console is only intended for use during development. In production, disabling CSRF protection or allowing frames for a website may create severe security risks.
342+
336343
TIP: `PathRequest.toH2Console()` returns the correct request matcher also when the console's path has been customized.
337344

338345

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,26 @@
1919
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
2020
import org.springframework.context.annotation.Bean;
2121
import org.springframework.context.annotation.Configuration;
22-
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
22+
import org.springframework.context.annotation.Profile;
23+
import org.springframework.core.Ordered;
24+
import org.springframework.core.annotation.Order;
25+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
26+
import org.springframework.security.web.SecurityFilterChain;
2327

28+
@Profile("dev")
2429
@Configuration(proxyBeanMethods = false)
25-
public class MySecurityConfiguration {
30+
public class DevProfileSecurityConfiguration {
2631

2732
@Bean
28-
public WebSecurityCustomizer webSecurityCustomizer() {
29-
return (web) -> web.ignoring().requestMatchers(PathRequest.toH2Console());
33+
@Order(Ordered.HIGHEST_PRECEDENCE)
34+
SecurityFilterChain h2ConsoleSecurityFilterChain(HttpSecurity http) throws Exception {
35+
// @formatter:off
36+
return http.requestMatcher(PathRequest.toH2Console())
37+
// ... configuration for authorization
38+
.csrf().disable()
39+
.headers().frameOptions().sameOrigin().and()
40+
.build();
41+
// @formatter:on
3042
}
3143

3244
}

0 commit comments

Comments
 (0)