reactjs - 在Spring Security中向/oauth/token发送请求的安全方式是什么?

标签 reactjs spring-boot axios spring-security-oauth2

从客户端应用程序发送请求以获取访问 token 的理想/安全方法是什么?

我有一个 REST API(使用 Spring Boot 开发),由该 API 的客户端应用程序(使用 React.js 开发)使用。例如,Stackoverflow 有后端 API,其前端客户端使用该 API。 REST API 使用 OAuth2 进行保护。

API 返回访问 token 的端点是

http://192.168.43.70:8085/api/v1/oauth/token?grant_type=password&<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="64111701160a050901595656240309050d084a070b09" rel="noreferrer noopener nofollow">[email protected]</a>&password=mypassword

以及客户端 key 。这是来自 React.js 的请求:

axios
    .create({
      baseURL:
        "http://192.168.43.70:8085/api/v1/oauth/token?grant_type=password&<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04717761766a656961393636446369656d682a676b69" rel="noreferrer noopener nofollow">[email protected]</a>&password=mypassword",
      auth: {
        username: "my-client",
        password: "ZS10ZXN0"
      }
    })

这是正确的方式吗,因为我们将客户端 secret (“my-client”“bGl2ZS10ZXN0”)暴露给浏览器,任何人都可以看到用于此请求的客户端凭据?

最佳答案

您是对的,这是不安全的,不建议这样做。最好使用不需要客户端凭据的不同授权流程。

我建议您查看 authorization_code 授予流程(不含客户端 key ),而不是密码授予流程。有coupleresources这将为您提供更多关于为什么不使用密码流程的背景知识,但从一开始就简单介绍一下:

Single-page apps (or browser-based apps) run entirely in the browser after loading the Javascript and HTML source code from a web page. Since the entire source is available to the browser, they cannot maintain the confidentiality of a client secret, so the secret is not used for these apps. The flow is exactly the same as the authorization code flow, but at the last step, the authorization code is exchanged for an access token without using the client secret.

更好的是,如果您可以引入一个服务器端组件,通过 JSESSIONID(意味着更改为 session 后端)与您的客户端进行协商,那么我相信您会发现 secrets are much easier to maintain on the backend 。你可以看看Spring Security's OAuth 2.0 Client support为此。

关于reactjs - 在Spring Security中向/oauth/token发送请求的安全方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53544660/

相关文章:

java - 如何使用 Spring Boot 从文件中获取数据列表?

java - Springboot/Thymeleaf : Links on html-template cannot be parsed

javascript - 使用 Axios 的 HTTP get 请求在 URL 的开头使用本地主机 IP 发送

javascript - Vuejs ajax 调用不将更改映射到基础 html 表

javascript - 缺少 ) 在使用异步等待的参数列表之后

javascript - 排序最新日期时间

reactjs - 找不到所需的文件。 - 将 TypeScript 添加到 React 项目

java - Spring 启动(带 jpa 的 mysql): No bean named 'entityManagerFactory' available

reactjs - 我如何才能仅在 react Material 用户界面中的特定尺寸上调整内容?

reactjs - 如何从 React JS 中的 Promise 对象检索数据