如何使用Spring Social进行REST身份验证?

67
我已经按照Spring security示例(以及spring security java配置)实现了Spring Social + Spring Security。当时我报告了几个问题(请参见https://jira.springsource.org/browse/SEC-2204),但这些问题都得到了解决,我的安全性能良好。
然而,我想改变我的安全实现方式,并使用RESTful身份验证。Spring oauth/oauth2(http://projects.spring.io/spring-security-oauth/)可以解决这个问题,但我不知道Spring Social如何适应这种情况?虽然在幕后,Spring social使用oauth与Facebook/Twitter通信,但我认为Spring Social的注册表单和其他特征并不适用于restful API。
任何示例或想法都会有所帮助。
此帖子的更新:(4/6/2014) 我建立了一个(PHP)网站,用于消费我的API。这个PHP网站(我们称之为客户端网站),使用Facebook PHP SDK注册自己的用户。这是一种完全独立的方式来收集自己的成员。然而,一旦用户在客户端网站注册,客户端网站会传递用户名、电子邮件、密码、名字和姓氏数据以及其客户端ID和客户端密钥,并使用OAuth2授权类型“client_credentials”进行身份验证。传入的用户数据将在主系统上创建用户记录!(主应用程序)此后,每次客户端网站通过OAuth2授权类型密码调用主系统并发送“client_id”、“client_secret”、“username”和“password”,都会得到一个“身份验证令牌”,并能够使用此令牌与主网站通信。看起来路程有点长,但解决了将用户记录保留在主系统中的问题。我想知道是否还有其他方法?请给予建议。

6
你看过这个例子吗?https://github.com/joshlong/the-spring-rest-stack - void
1
请查看https://dev59.com/nVwY5IYBdhLWcg3wsplj#33963286。 - rbarriuso
1
https://geowarin.github.io/social-login-with-spring.html-- 这可能会对你非常有帮助。 - Shashi Dk
3个回答

2

您想在应用程序中使用Oauth2,并且想要使用密码流程。您可以使用spring security oauth2-resource-server项目来实现资源服务器。在您的资源服务器中,您可以使用ResourceOwnerPasswordResourceDetails提供client_id、client_secret、username和password信息。可以使用Oauth2RestTemplate来调用资源服务器。


0

哇,其他人已经提供了很多好的信息,但Spring文档提供了示例配置yaml文件,以与Google和Okta进行身份验证,请参见下面的链接(如果已经提供了,敬请原谅)。

https://docs.spring.io/spring-security/reference/5.8/reactive/oauth2/login/core.html#webflux-oauth2-login-common-oauth2-provider
Configuring Custom Provider Properties
There are some OAuth 2.0 Providers that support multi-tenancy, which results in different protocol endpoints for each tenant (or sub-domain).

For example, an OAuth Client registered with Okta is assigned to a specific sub-domain and have their own protocol endpoints.

For these cases, Spring Boot 2.x provides the following base property for configuring custom provider properties: spring.security.oauth2.client.provider.[providerId].

The following listing shows an example:
For these cases, Spring Boot 2.x provides the following base property for configuring custom provider properties: spring.security.oauth2.client.provider.[providerId].

The following listing shows an example:

spring:
  security:
    oauth2:
      client:
        registration:
          okta:
            client-id: okta-client-id
            client-secret: okta-client-secret
        provider:
          okta: 
            authorization-uri: https://your-subdomain.oktapreview.com/oauth2/v1/authorize
            token-uri: https://your-subdomain.oktapreview.com/oauth2/v1/token
            user-info-uri: https://your-subdomain.oktapreview.com/oauth2/v1/userinfo
            user-name-attribute: sub
            jwk-set-uri: https://your-subdomain.oktapreview.com/oauth2/v1/keys

基础属性(spring.security.oauth2.client.provider.okta)允许自定义协议端点位置的配置。


0

Spring-social在2019年已被弃用。在这个问题曝光的情况下(早在此版本弃用之前),最简单的解决方案是使用一个能够直接联合“社交”身份的授权服务器。Keycloak是一个免费的“本地”示例,Auth0是一个SaaS示例(具有免费层)。只需在您喜欢的搜索引擎中搜索“OIDC授权服务器”,并选择最符合您需求的。

然后,使用spring-boot-starter-oauth2-resource-server配置REST API作为资源服务器。示例在这里


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接