我想保护我的 Spring RESTful 后端。一种方法(对吗?)是使用 OAuth 2.0,如下所示:
http://www.youtube.com/watch?v=8uBcpsIEz2I
在我的架构中,资源服务器和授权服务器不是相同的实体。我真的只是提供一些 JSON REST 服务。没有用户界面。如果我阅读 OAuth2 RFC他们只是说:
The interaction between the authorization server and resource server is beyond the scope of this specification. The authorization server may be the same server as the resource server or a separate entity. A single authorization server may issue access tokens accepted by multiple resource servers.
我在 cloudfoundry.com 上找到了一个很好的图表(与上述 youtube 视频相关)我用来说明我的观点:
“token”提供者:这可能/应该是 google 或 facebook 例如。
RESTful 后端:这实际上是我的代码。 Spring RESTful 服务,例如:
@Controller
@RequestMapping("/api/v1")
public class MyResourceToProtect {
@Autowired
private MyService service;
@RequestMapping(value = "/resource/delete/{name}",
method = RequestMethod.DELETE,
consumes = MediaType.APPLICATION_JSON_VALUE,
headers = "Content-Type=application/json")
@ResponseStatus(HttpStatus.OK)
public void delete(@PathVariable("name") String name) {
service.delete(name);
}
}
(这只是一些示例代码)
现在我的问题是:是否有可能验证由 AuthServer(Facebook、Google)生成的访问 token ?我知道我需要在 ResourceServer 的某处有一个“用户 token ”映射(数据库)。基本上我想设计我的 RESTful API,就像 PayPal 中的一样:
https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/
但是,如果我想使用 Facebook 或 Google 作为身份验证提供商,我该如何处理第 1 步和第 2 步?这甚至可能吗?
额外的想法:可能我需要提供自己的 /oauth2/token
端点,然后委托(delegate)给底层的 AuthProvider。
最佳答案
不知道如何用一个漂亮的弓来回答所有的问题,所以我将在此提出以下几点:
- 您是否只是想以 OAuth 安全方式使用 Facebook 等 OAuth 安全 API?使用 Spring Social。
- 如果您尝试创建自己的 OWN REST API 并使用您的 OWN 用户上下文,请使用 Spring Security OAuth。在这种情况下,您需要客户端使用 OAuth 对您的 API 进行身份验证,而不是 Facebook 或 LinkedIn 等。
- Spring Social Security(在 1.2.x 系列中)支持通过 OAuth 连接“登录”用户(例如,“使用 facebook 登录”、“..twitter”、“..linkedin”等.,并且您的应用程序最终会在 session 中以 Spring Security 主体结束,就像您使用 HTTP 表单手动登录用户一样。
- Spring Security OAuth 并不关心您从哪里获得 Spring Security 主体。它只关心主体是否具有 Spring Security OAuth 客户端所需的正确角色/范围。因此,您没有理由不能使用 Spring Social 安全地连接到 Facebook,使用 Spring Social Security 让该连接创建一个 Spring Security Authentication 对象,然后使用 Spring Security OAuth 来保护对您的 API 的任何访问,这在turn 可能会在幕后安全地连接到 Facebook 的 API。客户会为您的 API 使用 OAuth 访问 token ,而不是 Facebook。这在服务中得到处理。
关于java - 从 Spring RESTful 资源服务器验证 OAuth 2.0 访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17642224/