Spring boot 前端rest api调用未重定向到Auth0登录,因为CORS错误
我是Auth0新手,前端使用react,后端使用spring引导。我想做的是调用RESTAPI重定向auth0登录页面。但是当我调用Spring boot 前端rest api调用未重定向到Auth0登录,因为CORS错误,spring-boot,spring-security,auth0,Spring Boot,Spring Security,Auth0,我是Auth0新手,前端使用react,后端使用spring引导。我想做的是调用RESTAPI重定向auth0登录页面。但是当我调用adminLoginapi重定向到auth0登录时,不会发生。Chrome控制台显示: 我在这一点上感到困惑。按钮调用rest,调试器转到控制器,但由于我在前端使用localhost:3000,在后端使用localhost:8080,因此无法重定向auth0登录。 这是我的控制器: @ResponseBody public void login(H
adminLogin
api重定向到auth0登录时,不会发生。Chrome控制台显示:
我在这一点上感到困惑。按钮调用rest,调试器转到控制器,但由于我在前端使用localhost:3000
,在后端使用localhost:8080
,因此无法重定向auth0登录。
这是我的控制器:
@ResponseBody
public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
String redirectUrl = "http://localhost:8080/adminLogin";
String authorizeUrl = authenticationController.buildAuthorizeUrl(request, response, redirectUrl)
.withScope("openid email")
.build();
response.sendRedirect(authorizeUrl);
}
和安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Value(value = "${com.auth0.domain}")
private String domain;
@Value(value = "${com.auth0.clientId}")
private String clientId;
@Value(value = "${com.auth0.clientSecret}")
private String clientSecret;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http
.authorizeRequests()
.antMatchers("/adminLogin", "/admins", "/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/adminLogin");
}
@Bean
public AuthenticationController authenticationController() throws UnsupportedEncodingException {
JwkProvider jwkProvider = new JwkProviderBuilder(domain).build();
return AuthenticationController.newBuilder(domain, clientId, clientSecret)
.withJwkProvider(jwkProvider)
.build();
}
}
和前端axios呼叫:
loginWithAuth0 = (e) => {
axios.get('adminLogin')
.then(response => {
const data = response.data
this.setState({data: data})
})
}
最后,在auth0设置中设置我的回调url
在Auth0 Dashbaord应用程序设置中,用
http://localhost:3000