spring boot security不允许我访问h2-console
spring boot security不允许我访问h2-console
我正在尝试在Spring Boot中实现JWT。出于某些调试目的,我需要一个H2控制台。\n所以在我的WebSecurityConfiguration中,我写了以下代码:\n
@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); }
\n在我的应用程序配置中,我有以下配置:\n
spring.h2.console.enabled=true spring.h2.console.path=/h2
\n当我访问“:8080/h2”时,它给我返回403错误。\n因此问题仍然存在,我如何正确配置Spring Boot Web Security。\n在包含/h2/**
之后,我得到了这个UI:\n
spring boot security doesn't let me access h2-console的问题出现的原因是Spring Boot Security默认配置了防止跨域请求的功能,导致无法访问h2-console。解决方法是在Spring Security的配置中添加以下代码:
httpSecurity.authorizeRequests().antMatchers("/h2/**").permitAll(); httpSecurity.headers().frameOptions().disable();
这样就可以允许访问h2-console了。更多解决方法可以参考How to disable 'X-Frame-Options' response header in Spring Security?。请再次检查问题,h2-console应该可以正常加载,我在Chrome(版本84.0.4147.89)和MS Edge浏览器中尝试过,可以正常访问。我不知道你是如何使其工作的,但我需要添加httpSecurity.headers().frameOptions().disable();
的代码。我已经编辑了答案,请接受它,然后我将接受这个答案。