Skip to content

Commit 9d46a18

Browse files
hpoettkersnicoll
authored andcommitted
Document WebSecurityCustomizer for H2 Console
See gh-29932
1 parent 82c0582 commit 9d46a18

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,28 @@ You can customize the console's path by using the configprop:spring.h2.console.p
322322

323323

324324

325+
[[features.sql.h2-web-console.spring-security]]
326+
==== 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 to
328+
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:
335+
336+
[source,java,indent=0,subs="verbatim"]
337+
----
338+
include::{docs-java}/features/sql/h2webconsole/springsecurity/DevProfileSecurityConfiguration.java[]
339+
----
340+
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+
343+
TIP: `PathRequest.toH2Console()` returns the correct request matcher also when the console's path has been customized.
344+
345+
346+
325347
[[features.sql.jooq]]
326348
=== Using jOOQ
327349
jOOQ Object Oriented Querying (https://www.jooq.org/[jOOQ]) is a popular product from https://www.datageekery.com/[Data Geekery] which generates Java code from your database and lets you build type-safe SQL queries through its fluent API.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.sql.h2webconsole.springsecurity;
18+
19+
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
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;
27+
28+
@Profile("dev")
29+
@Configuration(proxyBeanMethods = false)
30+
public class DevProfileSecurityConfiguration {
31+
32+
@Bean
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
42+
}
43+
44+
}

0 commit comments

Comments
 (0)