使用自定义令牌保护 REST API(无状态、无 UI、无 cookie、无基本身份验证、无 OAuth、无登录页面) [英] Securing REST API using custom tokens (stateless, no UI, no cookies, no basic authentication, no OAuth, no login page)

查看:122
本文介绍了使用自定义令牌保护 REST API(无状态、无 UI、无 cookie、无基本身份验证、无 OAuth、无登录页面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多指南和示例代码展示了如何使用 Spring Security 保护 REST API,但其中大多数都假设是一个 Web 客户端,并讨论登录页面、重定向、使用 cookie 等.甚至可能是一个简单的过滤器检查 HTTP 标头中的自定义令牌可能就足够了.我如何实现以下要求的安全性?是否有任何 gist/github 项目在做同样的事情?我对 spring security 的了解有限,所以如果有更简单的方法来实现 spring security,请告诉我.

There are lots of guidelines, sample codes that show how to secure REST API with Spring Security, but most of them assume a web client and talk about login page, redirection, using cookie, etc. May be even a simple filter that checks for the custom token in HTTP header might be enough. How do I implement security for below requirements? Is there any gist/github project doing the same? My knowledge in spring security is limited, so if there is a simpler way to implement this with spring security, please let me know.

  • 由无状态后端通过 HTTPS 提供的 REST API
  • 客户端可以是网络应用、移动应用、任何 SPA 风格的应用、第三方 API
  • 无基本身份验证、无 Cookie、无 UI(无 JSP/HTML/静态资源)、无重定向、无 OAuth 提供程序.
  • 在 HTTPS 标头上设置的自定义令牌
  • 针对外部存储(如 MemCached/Redis/甚至任何 RDBMS)完成的令牌验证
  • 除了选定的路径(如/login、/signup、/public 等)之外,所有 API 都需要进行身份验证.

我使用 Springboot、spring security 等.更喜欢使用 Java 配置(无 XML)的解决方案

I use Springboot, spring security, etc.. prefer a solution with Java config (no XML)

推荐答案

我的示例应用正是这样做的 - 在无状态场景中使用 Spring Security 保护 REST 端点.单个 REST 调用使用 HTTP 标头进行身份验证.身份验证信息存储在服务器端的内存缓存中,并提供与典型 Web 应用程序中 HTTP 会话所提供的语义相同的语义.该应用程序使用完整的 Spring Security 基础设施和最少的自定义代码.没有裸过滤器,没有 Spring Security 基础架构之外的代码.

My sample app does exactly this - securing REST endpoints using Spring Security in a stateless scenario. Individual REST calls are authenticated using an HTTP header. Authentication information is stored on the server side in an in-memory cache and provides the same semantics as those offered by the HTTP session in a typical web application. The app uses the full Spring Security infrastructure with very minimum custom code. No bare filters, no code outside of the Spring Security infrastructure.

基本思想是实现以下四个 Spring Security 组件:

The basic idea is to implement the following four Spring Security components:

  1. org.springframework.security.web.AuthenticationEntryPoint 用于捕获需要身份验证但缺少所需身份验证令牌的 REST 调用,从而拒绝请求.
  2. org.springframework.security.core.Authentication 用于保存 REST API 所需的身份验证信息.
  3. org.springframework.security.authentication.AuthenticationProvider 以执行实际身份验证(针对数据库、LDAP 服务器、Web 服务等).
  4. org.springframework.security.web.context.SecurityContextRepository 在 HTTP 请求之间保存身份验证令牌.在示例中,实现将令牌保存在 EHCACHE 实例中.
  1. org.springframework.security.web.AuthenticationEntryPoint to trap REST calls requiring authentication but missing the required authentication token and thereby deny the requests.
  2. org.springframework.security.core.Authentication to hold the authentication information required for the REST API.
  3. org.springframework.security.authentication.AuthenticationProvider to perform the actual authentication (against a database, an LDAP server, a web service, etc.).
  4. org.springframework.security.web.context.SecurityContextRepository to hold the authentication token in between HTTP requests. In the sample, the implementation saves the token in an EHCACHE instance.

该示例使用 XML 配置,但您可以轻松提出等效的 Java 配置.

The sample uses XML configuration but you can easily come up with the equivalent Java config.

这篇关于使用自定义令牌保护 REST API(无状态、无 UI、无 cookie、无基本身份验证、无 OAuth、无登录页面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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