Spring Boot REST 令牌授权和认证最佳实践 [英] Spring boot REST token authorization and authentication best practices

查看:70
本文介绍了Spring Boot REST 令牌授权和认证最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

REST spring boot 中用户授权和认证的最佳实践是什么?

What is the best practise for authorization and authentication of users in REST spring boot?

我正在使用标准页面 + REST API 为移动设备构建 Web 应用程序.我查看了许多关于 Spring 安全性的文章,基本上大多数文章都采用了某种允许或阻止 REST 调用的更合适的方法.但是,就我而言,我有一些基于用户身份的身份验证逻辑.例如,有一个/update API 更新用户信息,用户可以更新自己,但不能更新其他人.最初我想使用下一个身份验证模式:

I am building web app with standard pages + REST API for mobile. I looked at many articles about Spring security and basically most of them goes with some sort of fitler approach that will allow or block REST calls. In my case, however, I have some auth logic based on who the user is. For example, there is a /update API that updates user information, and user can update himself, but cannot update other person. Initially I thought to use next auth schema:

  • 用户调用身份验证 API 并传递名称/密码或 cookie
  • 系统生成短寿命令牌,保存在数据库中.
  • 用户得到这个令牌,更新他的cookie(这样Web应用程序中的JS可以读取和使用它)
  • 当 REST 调用时,传递 cookie.在控制器处,提取令牌,检查是否过期,查询数据库以验证令牌并获取用户 ID.
  • 根据用户 ID,将允许或阻止 REST.

这是正确的实施方法吗?阅读了有关 Spring Boot 安全性的文章后,我脑子里一片混乱.至少:会话身份验证对我不起作用(REST 是无状态的).我想为移动设备进行身份验证而不在那里存储登录名/密码.

Is this the right approach to implement? I have a pretty big mess in my head after reading articles about spring boot security. At least: session auth will not work for me (REST is stateless). I want to make auth for mobile device without storing login/password there.

在 REST 主体中传递这个令牌是否有意义?如果是 GET 方法呢?

Does it make sense to pass this token in the REST body itself? What in case of GET method?

非常感谢您分享您的知识.

Many thanks for sharing your knowledge.

推荐答案

您找到问题的解决方案了吗?

Did you find a solution to your problem?

我已经在别处回答了这个问题,如果您确定以后不想向其他开发人员/客户开放 API(如果您这样做,那么您应该查看 OAuth),那么一个简单的基于令牌的解决方案将工作.

I have answered this problem elsewhere, if you are sure you won't want to open up the API to other developers/clients in the future (if you do then you should look at OAuth) then a simple token based solution will work.

基本上是这样的:

  • 设置一个标准的 html 登录页面,您可以使用它来让用户登录到应用程序
  • 设置 spring security 以在使用身份验证令牌成功登录时返回 cookie
  • 在您的移动应用程序中,嵌入 WebView(或等效项)并加载此登录表单 - 允许用户通过该 WebView 登录,响应时获取 cookie 并存储令牌(因为移动设备通常是单用户,您可以保留相当长的时间来节省移动用户必须继续登录)
  • 向 REST API 添加安全过滤器以针对令牌进行身份验证(例如,从移动应用程序在标头中传递令牌) - 然后您将能够对当前用户等使用普通的 spring 身份验证上下文.

谷歌在这里建议了这种方法:(谷歌似乎已经改变了我最初阅读的关于使用 Google+ 登录和 OAuth2.0 的页面 - 我看不到他们一般移动/API 文档的链接所以它在网络存档中 :) )

This approach is suggested by Google here: ( Google seems to have changed the page I originally read to be about using Google+ sign in and OAuth2.0 - I can't see a link to their general Mobile/API docs so here it is in the web archive :) )

我也在这里写了我的实现:

I have also written up my implementation here:

使用 Spring 的方法概述安全

代码&详情

虽然这实际上只是一个实验/概念证明,但它可能对您的思考有用.

Although this was really just an experiment/Proof of concept, it might be useful in your thinking.

这篇关于Spring Boot REST 令牌授权和认证最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆