gpt4 book ai didi

java - H2 控制台和 Spring Security - permitAll() 不工作

转载 作者:搜寻专家 更新时间:2023-11-01 01:32:02 25 4
gpt4 key购买 nike

我正在创建 rest api 并实现 Spring Security - 一切正常,但我希望(目前,当我仍在开发时)能够让任何未经授权的人打开 localhost:8080/console。我的代码:

@Override
protected void configure(HttpSecurity http) throws Exception {
// allow everyone to register an account; /console is just for testing
http.authorizeRequests().antMatchers("/register", "/console").permitAll();

http.authorizeRequests().anyRequest().fullyAuthenticated();

// making H2 console working
http.headers().frameOptions().disable();

/*
https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html#when-to-use-csrf-protection
for non-browser APIs there is no need to use csrf protection
*/
http.csrf().disable();
}

真正奇怪的是 - localhost:8080/register 不需要任何身份验证但/console 返回:

{
"timestamp": 1485876313847,
"status": 403,
"error": "Forbidden",
"message": "Access Denied",
"path": "/console"
}

有人知道怎么解决吗?

最佳答案

有同样的问题,就我而言:

csrf().ignoringAntMatchers("/h2-console/**")

最终WebSecurityConfigurerAdapter:

http.authorizeRequests().antMatchers("/").permitAll()
.and()
.authorizeRequests().antMatchers("/h2-console/**").permitAll()
.and()
.headers().frameOptions().disable()
.and()
.csrf().ignoringAntMatchers("/h2-console/**")
.and()
.cors().disable();

关于java - H2 控制台和 Spring Security - permitAll() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41961270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com