我正在为开发人员构建一个具有 Spring 4 Boot Security 等的骨架项目。 在尝试登录数据库控制台并管理我的数据库时使用 H2 我收到以下错误。该页面是空白的,firebug konsole 中有 4 个错误:
Load denied by X-Frame-Options: http://localhost:8080/console
有链接
/header.jsp?jsessionid=f71207a702c9177e57208414721bbe93 does not permit framing.
/query.jsp?jsessionid=f71207a702c9177e57208414721bbe93 does not permit framing.
/help.jsp?jsessionid=f71207a702c9177e57208414721bbe93 does not permit framing.
/tables.do?jsessionid=f71207a702c9177e57208414721bbe93 does not permit framing.
我使用的配置来自(它适用于带有xml配置的spring 3.2)
spring boot默认H2 jdbc连接(和H2控制台)
使用: spring-boot-starter-父级 1.1.4.发布
也可以通过以下方式简化@chrosciu的答案:
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers().frameOptions().disable();
}
}
将下面的代码添加到Application.java,现在它可以工作,默认在端口8082上,从spring应用程序启动。它没有击中要害,但出于开发目的,一切都可以。
@Bean
org.h2.tools.Server h2Server() {
Server server = new Server();
try {
server.runTool("-tcp");
server.runTool("-tcpAllowOthers");
} catch (Exception e) {
e.printStackTrace();
}
return server;
}
这对我有用:
@EnableWebSecurity
@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers().addHeaderWriter(
new XFrameOptionsHeaderWriter(
new WhiteListedAllowFromStrategy(Arrays.asList("localhost"))));
}
}
当然,当应用程序在不同于本地主机的设备上运行时,应该调整白名单的内容。
有了新的弹簧
dsl
,您可以在headers
中使用SecurityFilterChain
方法,如下
@Configuration
@EnableWebSecurity
public class SecurityConfiguration {
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.headers(headers -> headers.frameOptions().disable()) //disable frame
//rest of your configuration
.csrf( csrf -> csrf.disable())
.authorizeHttpRequests(
authorize -> authorize
.requestMatchers(HttpMethod.OPTIONS).permitAll()
.requestMatchers(HttpMethod.POST, "/users", "/users/login").permitAll()
.requestMatchers(HttpMethod.GET, "/articles/**").permitAll()
.anyRequest().permitAll()
)
return http.build();
}