-
Notifications
You must be signed in to change notification settings - Fork 41.6k
Document how to access the H2 Console in a secured web application #29932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -322,6 +322,21 @@ You can customize the console's path by using the configprop:spring.h2.console.p | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| [[features.sql.h2-web-console.spring-security]] | ||||||||||||||||||||||||||||||||||||
| ==== Configuring Spring Security for H2 Console | ||||||||||||||||||||||||||||||||||||
| 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. | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| For example, Spring Security will ignore the console if the following `WebSecurityCustomizer` is exposed: | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
| @Override | |
| public void configure(HttpSecurity http) throws Exception { | |
| String path = this.console.getPath(); | |
| String antPattern = (path.endsWith("/") ? path + "**" : path + "/**"); | |
| HttpSecurity h2Console = http.antMatcher(antPattern); | |
| h2Console.csrf().disable(); | |
| h2Console.httpBasic(); | |
| h2Console.headers().frameOptions().sameOrigin(); | |
| String[] roles = this.security.getUser().getRole().toArray(new String[0]); | |
| SecurityAuthorizeMode mode = this.security.getBasic().getAuthorizeMode(); | |
| if (mode == null || mode == SecurityAuthorizeMode.ROLE) { | |
| http.authorizeRequests().anyRequest().hasAnyRole(roles); | |
| } | |
| else if (mode == SecurityAuthorizeMode.AUTHENTICATED) { | |
| http.authorizeRequests().anyRequest().authenticated(); | |
| } | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Copyright 2012-2022 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.boot.docs.features.sql.h2webconsole.springsecurity; | ||
|
|
||
| import org.springframework.boot.autoconfigure.security.servlet.PathRequest; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; | ||
|
|
||
| @Configuration(proxyBeanMethods = false) | ||
| public class MySecurityConfiguration { | ||
|
|
||
| @Bean | ||
| public WebSecurityCustomizer webSecurityCustomizer() { | ||
| return (web) -> web.ignoring().requestMatchers(PathRequest.toH2Console()); | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.