Reactjs 具有Spring安全性的Spring引导,并从Spring项目外部的React应用程序登录
我正在构建一个网站,后端使用spring boot,前端使用react 前端和后端存储在不同的git存储库中,当我部署到服务器时,它们会组合在一起 现在react能够从后端检索信息,没有问题,我只是在package.json中添加了一个代理,localhost:8080,就可以了 我配置了Spring安全性,创建了我自己的userdetails实现,所有东西都可以从后端URL(如果它们从localhost:8080开始)运行,我在React to login中创建了一个组件,并公开了非公共API,现在我遇到了问题 在后端,我将cors原点设置为允许来自react的请求 在Spring安全配置中,我有以下内容:Reactjs 具有Spring安全性的Spring引导,并从Spring项目外部的React应用程序登录,reactjs,spring-boot,spring-security,Reactjs,Spring Boot,Spring Security,我正在构建一个网站,后端使用spring boot,前端使用react 前端和后端存储在不同的git存储库中,当我部署到服务器时,它们会组合在一起 现在react能够从后端检索信息,没有问题,我只是在package.json中添加了一个代理,localhost:8080,就可以了 我配置了Spring安全性,创建了我自己的userdetails实现,所有东西都可以从后端URL(如果它们从localhost:8080开始)运行,我在React to login中创建了一个组件,并公开了非公共API
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf().disable()
//LOGIN
.formLogin()
.permitAll()
.loginProcessingUrl( "/api/login")
.passwordParameter("password")
.usernameParameter("username")
.defaultSuccessUrl("http://localhost:3000/projects", true)
.failureUrl("http://localhost:3000/users/login")
//LOGOUT
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET"))
.clearAuthentication(true)
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
.logoutSuccessUrl("http://localhost:3000/users/login")
.and()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/index*", "/static/**", "/*.js", "/*.json", "/*.ico").permitAll()
.anyRequest()
.authenticated();
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/*")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("Content-Type", "X-Requested-Width","accept", "Origin","Authorization", "Access-Control-Request-Method", "Access-Control-Allow-Methods", "Access-Control-Request-Headers")
.exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials")
.allowCredentials(true)
.maxAge(3600);
}
经过几次尝试,在最佳情况下,我能够从前端获得200 OK,但我没有重定向到成功的URL,而如果凭据错误,则重定向到失败的URL
有什么想法吗?
谢谢