首页 > 解决方案 > spring boot security 不允许我访问 h2-console

问题描述

我正在尝试在 Spring Boot 中实现 JWT。出于某些调试目的,我需要 H2 控制台。

所以在我的WebSecurityConfiguration我写道:

@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        //httpSecurity.headers().frameOptions().disable();
        httpSecurity.authorizeRequests().antMatchers("/h2").permitAll();
        httpSecurity
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/auth/check/username").permitAll()
                .antMatchers("/auth/signup").permitAll()
                .antMatchers("/auth/login").permitAll()
                .anyRequest().authenticated().and()
                .exceptionHandling().and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);

    }

在我的应用程序属性中,我有这个配置:

spring.h2.console.enabled=true
spring.h2.console.path=/h2

当我点击 ":8080/h2" 时,它给了我403

所以问题仍然存在,如何正确配置 Spring Boot Web Security。

包括后/h2/**,我得到了这个用户界面: 在此处输入图像描述

标签: springspring-bootspring-securityh2

解决方案


请尝试“h2”模式为:

httpSecurity.authorizeRequests().antMatchers("/h2/**").permitAll();

这也是:

        httpSecurity.headers().frameOptions().disable();

更多可以在这里找到:如何在 Spring Security 中禁用“X-Frame-Options”响应标头?


推荐阅读