我收到以下错误消息: -由于访问控制检查,Fetch API无法加载http:// localhost:8080 / oauth2 / authorization / google -无法加载资源:跨域重定向到https://accounts.google ...,但被CORS策略拒绝:Access-Control-Allow-Origin不允许来源http:// localhost:3000
我制作了Kotlin-SpringBoot API,并且正在开发React-TypeScript的前端。 我使用SwaggerCodegen导入了函数,以便通过OpenAPI文档与我的API进行交互。
我希望我的用户通过google OAUTH进行身份验证,因此我决定使用Spring Security使我的所有端点都需要身份验证。我是使用此类完成的:
@Configuration
class SecurityConfig : WebSecurityConfigurerAdapter() {
override fun configure(http: HttpSecurity) {
http
.antMatcher("/**").authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.oauth2Login()
}
}
现在,api在端口8080中,并且前端在3000上 因此,如果我访问localhost:8080 / restricted,则会提示我登录,并且按预期运行。 但是,如果我在localhost:3000上的前端尝试从API检索任何数据,我认为它无论如何都会重定向我,但上面出现了CORS错误。 我到处都看过了,只想知道我的方法在概念上是否错误,或者如何授权这种重定向(或者我什至应该这样做)。
感谢您的帮助!