gpt4 book ai didi

java - Spring Boot 安全 OAuth2 : WebSecurityConfigurerAdapter: 302 Redirect to/error

转载 作者:行者123 更新时间:2023-12-01 19:14:17 27 4
gpt4 key购买 nike

仅存在一个空的 WebSecurityConfigurerAdapter 就会破坏我的应用程序的 OAuth2。我明白了

$ curl -i -X POST -H "Content-Type: application/json" -H "Authorization: Bearer 27f9e2b7-4441-4c03-acdb-7e7dc358f783" -d '{"apiKey": "key", "tag": "tag"}' localhost:8080/isTagAvailable
HTTP/1.1 302
Location: http://localhost:8080/error

当我期待的时候

$ curl -i -X POST -H "Content-Type: application/json" -H "Authorization: Bearer 27f9e2b7-4441-4c03-acdb-7e7dc358f783" -d '{"apiKey": "key", "tag": "tag"}' localhost:8080/isTagAvailable
HTTP/1.1 401

{"error":"invalid_token","error_description":"Invalid access token: 27f9e2b7-4441-4c03-acdb-7e7dc358f783"}

我必须注释掉 ENTIRE 类才能使 Oauth2 正常工作。即使只是注释 configure 方法也不起作用。为什么?

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/robots.txt").permitAll()
.and()
.authorizeRequests()
.antMatchers("/isTagAvailable").hasRole("USER")
// .anyRequest().authenticated()
.and()
.httpBasic().disable();
}

}

I learned how to add security logging ,但是it didn't print out any useful information .

最佳答案

我扔掉了@EnableWebSecurityWebSecurityConfigurerAdapter,这完全破坏了应用程序。我认为他们需要访问我认为我需要的 HttpSecurity 。我发现这个简单的新类可以解决问题。

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

String[] ignoredPaths = new String[]{...};

@Override
public void configure(HttpSecurity http) throws Exception{

http.authorizeRequests()
.antMatchers(ignoredPaths).permitAll()
.anyRequest().authenticated()
.and()
.httpBasic();
}

关于java - Spring Boot 安全 OAuth2 : WebSecurityConfigurerAdapter: 302 Redirect to/error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50520642/

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