gpt4 book ai didi

java - 我如何将 Spring Security 与我的 React-Spring Boot 应用程序一起使用

转载 作者:行者123 更新时间:2023-12-04 17:31:44 24 4
gpt4 key购买 nike

开始这个问题的上下文很少,我在 springboot 中做了前端与后端实现的 react 。

当我开发时,我在 package.json 中设置了 "proxy": "http://localhost:8080/", 这样它就可以将我的请求代理到 localhost:8080,这是 spring 的tomcat 服务器。

对于生产,我使用 frontend-maven-plugin 构建 React 应用程序,使用 maven-antrun-plugin 将 React 生产构建复制到后端目录 (target/classes/public),最后部署到 heroku。

在我的项目中添加 spring security 后,我意识到它添加了对“/login”上的登录页面的支持,我通过更改 http.loginPage() 将其更改为“/my-login”

@Override
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/my-login")
.permitAll(true)
.and()
.logout()
.logoutSuccessUrl("/my-login?logout")
.permitAll();
http.cors();

现在的问题是我的登录页面在 react 应用程序目录中,我不能在 spring boot 目录中有另一个登录页面。我尝试将 .jsp 登录页面作为中间件,它会与 Spring Security 对话并从 React 端发出发布请求,但实际上无法用这种方法做任何事情。

那么有没有其他方法可以让登录页面由 react 提供,它仍然可以与 spring security 交互?

谢谢,

段落

最佳答案

我刚刚开始使用 spring ...但通常对于 React 应用程序,身份验证是无状态的...我们设置包含 token 的授权 header (例如:jwt)并且我们有一个注销端点使 token 。因此,注销请求中的 200 =“/my-login?logout”。任何路由中的 403 都被“客户端”路由器重定向到“/my-login”

您采用的方法要求 View 是“服务器端”呈现的……据我所知,React SSR(服务器端呈现)是 NodeJS 服务器独有的。

Link to a "JWT authentication with spring" article

关于java - 我如何将 Spring Security 与我的 React-Spring Boot 应用程序一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59025961/

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