Spring 安全 OAuth2 clientId 和 clientSecret
Spring Security OAuth2 clientId and clientSecret
我正在评估 Spring 安全 OAuth2 实施。我对 clientId
和 clientSecret
感到困惑。
我按照https://spring.io/guides/tutorials/spring-security-and-angular-js/搭建认证服务器。
我可以通过
获取生成代码
http://localhost:9999/uaa/oauth/authorize?response_type=code&client_id=acme&redirect_uri=http://example.com
我也可以通过
获取accesstoken
curl acme:acmesecret@localhost:9999/uaa/oauth/token \
-d grant_type=authorization_code -d client_id=acme \
-d redirect_uri=http://example.com -d code=jYWioI
{"access_token":"2219199c-966e-4466-8b7e-12bb9038c9bb","token_type":"bearer","refresh_token":"d193caf4-5643-4988-9a4a-1c03c9d657aa","expires_in":43199,"scope":"openid"}
获取访问令牌时,需要clientId
和clientSecret
。
但是如果我有多个客户端,我应该启动多个授权服务器吗?它不能以这种方式工作。
如何在没有 clientId
和 clientSecret
的情况下构建 OAuth2 服务器?
您可以设置多个客户端
记忆中的例子:-
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("acme")
.secret("acmesecret")
.authorizedGrantTypes("authorization_code", "refresh_token",
"password").scopes("openid")
.and()
.withClient("xx")
.secret("xx")
.authorizedGrantTypes("xxx");
}
或者您可以为客户端添加数据库记录
REF - Spring oauth2 DB Schema
为了实现动态客户端注册,您需要将凭据存储在数据库中,而不是硬编码配置。
@Override
public void configure(final ClientDetailsServiceConfigurer clients) throws Exception {
clients.jdbc(dataSource())
// ...
}
请参阅此tutorial了解更多信息。
我正在评估 Spring 安全 OAuth2 实施。我对 clientId
和 clientSecret
感到困惑。
我按照https://spring.io/guides/tutorials/spring-security-and-angular-js/搭建认证服务器。
我可以通过
获取生成代码http://localhost:9999/uaa/oauth/authorize?response_type=code&client_id=acme&redirect_uri=http://example.com
我也可以通过
获取accesstokencurl acme:acmesecret@localhost:9999/uaa/oauth/token \
-d grant_type=authorization_code -d client_id=acme \
-d redirect_uri=http://example.com -d code=jYWioI
{"access_token":"2219199c-966e-4466-8b7e-12bb9038c9bb","token_type":"bearer","refresh_token":"d193caf4-5643-4988-9a4a-1c03c9d657aa","expires_in":43199,"scope":"openid"}
获取访问令牌时,需要clientId
和clientSecret
。
但是如果我有多个客户端,我应该启动多个授权服务器吗?它不能以这种方式工作。
如何在没有 clientId
和 clientSecret
的情况下构建 OAuth2 服务器?
您可以设置多个客户端
记忆中的例子:-
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("acme")
.secret("acmesecret")
.authorizedGrantTypes("authorization_code", "refresh_token",
"password").scopes("openid")
.and()
.withClient("xx")
.secret("xx")
.authorizedGrantTypes("xxx");
}
或者您可以为客户端添加数据库记录
REF - Spring oauth2 DB Schema
为了实现动态客户端注册,您需要将凭据存储在数据库中,而不是硬编码配置。
@Override
public void configure(final ClientDetailsServiceConfigurer clients) throws Exception {
clients.jdbc(dataSource())
// ...
}
请参阅此tutorial了解更多信息。