Spring boot h2-console无法正常工作

时间:2015-11-13 16:40:41

标签: spring spring-boot h2

您好我无法使用spring boot打开h2-console

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RC1</version>
</parent>

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

SpringSecurity Cfg:

@Configuration

@EnableWebSecurity 公共类SecurityConfig扩展了WebSecurityConfigurerAdapter {

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
        .withUser("admin").password("admin").roles("ADMIN").and()
        .withUser("user").password("user").roles("USER");
}

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {  
    httpSecurity.authorizeRequests()
    .anyRequest().authenticated()
    .and()
        .formLogin().loginPage("/login").permitAll()
    .and()
        .logout().permitAll()
    .and()
        .exceptionHandling().accessDeniedPage("/Access_Denied")
    .and()
        .csrf();
}

}

@SpringBootApplication

公共类SpringBootWebSecureApplication {

@Bean
public Java8TimeDialect java8TimeDialect() {
    return new Java8TimeDialect();
}

public static void main(String[] args) {
    SpringApplication.run(SpringBootWebSecureApplication.class);
}

}

我开始启动:

-Dserver.port=8090
-Dspring.h2.console.enabled=true
-Dspring.datasource.url=jdbc:h2:mem:testdb
-Dspring.datasource.username=sa
-Dspring.datasource.driverClassName=org.h2.Driver

日志:

2015-11-13 17:37:47 [restartedMain] DEBUG c.m.a.SpringBootWebSecureApplication - Running with Spring Boot v1.3.0.RC1, Spring v4.2.2.RELEASE
2015-11-13 17:37:49 [restartedMain] INFO  o.s.b.c.e.t.TomcatEmbeddedServletContainer - Tomcat initialized with port(s): 8090 (http)
2015-11-13 17:37:50 [localhost-startStop-1] INFO  o.s.b.c.e.ServletRegistrationBean - Mapping servlet: 'webServlet' to [/h2-console/*]
2015-11-13 17:37:51 [localhost-startStop-1] INFO  o.s.s.web.DefaultSecurityFilterChain - Creating filter chain: Ant [pattern='/h2-console/**'], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@11ffd6f0, org.springframework.security.web.context.SecurityContextPersistenceFilter@615c57c5, org.springframework.security.web.header.HeaderWriterFilter@6b2cbcbf, org.springframework.security.web.authentication.logout.LogoutFilter@af7bece, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@17d03ab2, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@613197b, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@8d48442, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@7fa13240, org.springframework.security.web.session.SessionManagementFilter@9f16a1c, org.springframework.security.web.access.ExceptionTranslationFilter@1f3f02ef, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@115de1f6]

但是当我用我的应用程序打开浏览器时 控制台没有出现

http://localhost:8090/h2-console/

任何提示?

谢谢

3 个答案:

答案 0 :(得分:0)

在这里,我认为它是Java8TimeDialect Bean,在你的情况下。

在我的情况下,我有一个DandelionDialect Bean和当我删除它 h2-console再次运行....尝试删除Java8TimeDialect以查看控制台是否正常工作。

与Spring引导DispatcherServletAutoConfiguration?相关的东西,servlet创建或映射的顺序?不太确定......

答案 1 :(得分:0)

http.headers()frameOptions()禁用();

答案 2 :(得分:0)

您可以尝试以下配置类:

import org.h2.server.web.WebServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ShallowEtagHeaderFilter;

import javax.servlet.DispatcherType;
import java.util.EnumSet;

@Configuration
public class WebConfiguration {

    private static final String mapping = "/console/*";

    @Bean
    public ServletRegistrationBean h2servletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
        registrationBean.addUrlMappings(mapping);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean shallowEtagHeaderFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new ShallowEtagHeaderFilter());
        registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
        registration.addUrlPatterns(mapping);
        return registration;
    }

}