简体   繁体   English

保护有状态的Web服务

[英]Securing a stateful web service

We are planning on developing a layer of REST services to expose services hosted on a legacy system. 我们正计划开发一层REST服务,以公开遗留系统上托管的服务。 These services will be used by a classic web application and native mobile phone applications. 这些服务将由经典Web应用程序和本机移动电话应用程序使用。

This legacy system is secured in such a way that an initial username + password authentication is required (a process that can take 5 to 10 seconds). 这种遗留系统的安全性使得需要初始用户名+密码验证(一个可能需要5到10秒的过程)。 After the initial authentication, a time-constrained token is returned. 初始身份验证后,将返回时间受限的令牌。 This token must then be included in all further requests or else requests will be rejected. 然后,此令牌必须包含在所有其他请求中,否则请求将被拒绝。

Due to a security requirement, the legacy security token cannot be returned outside of the REST service layer. 由于安全性要求,无法在REST服务层之外返回旧安全令牌。 This means that the REST service layer needs to keep this token in some form of user session, or else the expensive username + password authentication process would need to be repeated for every call to the legacy system. 这意味着REST服务层需要将此令牌保留在某种形式的用户会话中,否则每次调用遗留系统时都需要重复昂贵的用户名+密码身份验证过程。

The REST service layer will be implemented using a Java 6 + Spring 3 + Spring Security 3 stack. REST服务层将使用Java 6 + Spring 3 + Spring Security 3堆栈实现。 At first sight, it looks like this setup will run fine: Spring-based REST services will be secured using a rather standard Spring Security configuration, the legacy security token will be stored in the user's HTTP session and every call will retrieve this token using the user's session and send it to the legacy system. 乍一看,看起来这个设置运行正常:基于Spring的REST服务将使用相当标准的Spring Security配置进行保护,旧的安全令牌将存储在用户的HTTP会话中,并且每个调用都将使用用户的会话并将其发送到旧系统。

But there lies the question: how will REST clients send the necessary data so that the user's HTTP session is retrieved properly? 但问题在于:REST客户端将如何发送必要的数据,以便正确检索用户的HTTP会话? This is normally done transparently by the web browser using the JSESSIONID cookie, but no browser is involved in this process. 这通常由Web浏览器使用JSESSIONID cookie透明地完成,但此过程中不涉及任何浏览器。 Sure, REST clients could add cookie management to their code, but is this an easy task for all Spring RestTemplate, iPhone, BlackBerry and Android clients? 当然,REST客户端可以在其代码中添加cookie管理,但对于所有Spring RestTemplate,iPhone,BlackBerry和Android客户端来说,这是一项简单的任务吗?

The alternative would be to bypass the HTTP session at the REST service layer and use some other form of user session, maybe using a database, that would be identified using some key that would be sent by REST clients through a HTTP header or simple request query. 另一种方法是绕过REST服务层的HTTP会话,并使用其他形式的用户会话,可能使用数据库,使用REST客户端通过HTTP头或简单请求查询发送的密钥来识别。 The question then becomes, how can Spring Security be configured to use this alternative session mechanism instead of the standard Servlet HttpSession? 接下来的问题是,Spring Security如何配置为使用这种备用会话机制而不是标准的Servlet HttpSession?

Surely I am not the first dealing with this situation. 当然,我不是第一个处理这种情况的人。 What am I missing? 我错过了什么?

Thanks! 谢谢!

There's nothing magical about cookies. 饼干没什么神奇之处。 They're just strings in HTTP headers . 它们只是HTTP标头中的字符串 Any decent client API can handle them, although many require explicit configuration to enable cookie processing. 任何体面的客户端API都可以处理它们,尽管许多客户端需要显式配置才能启用cookie处

An alternative to using cookies is to put the JSESSIONID into the URL. 使用cookie的另一种方法是将JSESSIONID放入URL中。 I don't know anything about spring-security, but it seems that that's actually the default for at least some types of URL requests, unless disable-url-rewriting is explicitly set to true . 我对spring-security一无所知,但看起来这实际上是至少某些类型的URL请求的默认值,除非disable-url-rewriting明确地设置为true。 This can be considered as ecurity weakness , though. 不过,这可以被视为安全漏洞

Unfortunately authentication is highly problematic -- a bit of a blind spot in terms of web standards and browser implementations. 不幸的是,身份验证很成问题 - 在Web标准和浏览器实现方面有点盲点。 You are right that cookies are not considered "RESTful" but purists, but even on fully-featured browsers avoiding takes quite a bit of hackery, as described in this article: Rest based authentication . 你是对的,cookie不被认为是“RESTful”,而是纯粹主义者,但即使在功能齐全的浏览器上也避免了相当多的hackery,如本文所述: 基于Rest的身份验证

Unfortunately I haven't done any mobile development, so I can't suggest what the best compromise is. 不幸的是,我没有做过任何移动开发,所以我不能建议最好的妥协是什么。 You might want to start by checking what authentication models each of your targetted platforms does support. 你可能想通过检查什么您的每一个针对性的平台的验证模型支持启动。 In particular, two main options are: 特别是,两个主要选项是:

  • HTTP authentication (ideally "digest", not "basic") HTTP身份验证(理想情况下是“摘要”,而不是“基本”)
  • Cookies 饼干

One possibility would be to provide both options. 一种可能性是提供两种选择。 Obviously not ideal from a technical or security point of view, but could have merits in terms of usability. 从技术或安全的角度来看,显然不是理想的,但在可用性方面可能有优点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM