【问题标题】:Spring Security: Purpose of .oauth2Client(withDefaults()); in HttpSecuritySpring Security:.oauth2Client(withDefaults()) 的目的;在 HttpSecurity
【发布时间】:2021-12-15 07:17:24
【问题描述】:
这是来自doc
公共 HttpSecurity
oauth2Client(定制器
oauth2ClientCustomizer) 抛出 java.lang.Exception
配置 OAuth 2.0 客户端支持。
示例配置
以下示例演示如何启用 OAuth 2.0 客户端
支持所有端点。
@Configuration
@EnableWebSecurity
public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests((authorizeRequests) ->
authorizeRequests
.anyRequest().authenticated()
)
.oauth2Client(withDefaults());
}
}
参数:auth2ClientCustomizer - 提供更多的定制器
OAuth2ClientConfigurer 的选项
返回:用于进一步自定义的 HttpSecurity
我的理解是任何到达此服务器的请求都应该经过身份验证。
如何
.oauth2Client(withDefaults()); 在这种情况下有帮助吗?
如果我没记错的话,一个 oAuth2 客户端是发送请求的那个,我们实际上可以配置什么?文档并没有真正解释太多。
【问题讨论】:
标签:
java
spring-boot
spring-security
oauth-2.0
spring-boot-starter-oauth2-client
【解决方案1】:
HttpSecurity的http实例是一个“bean设置服务器/应用端”。
其方法 oauth2Client 与客户端配置无关,而是服务器/应用程序应如何以及在何处处理它们。
例子:
- 哪些客户端已获得授权
- 授权客户的存储位置
- 如何授权客户
- 如何删除旧的授权客户端
【解决方案2】:
我认为here,您可以找到有关 oauth2Client 默认值的更多详细信息。
@EnableWebSecurity
public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.oauth2Client(oauth2Client ->
oauth2Client
.clientRegistrationRepository(this.clientRegistrationRepository())
.authorizedClientRepository(this.authorizedClientRepository())
.authorizedClientService(this.authorizedClientService())
.authorizationCodeGrant(authorizationCodeGrant ->
authorizationCodeGrant
.authorizationRequestRepository(this.authorizationRequestRepository())
.authorizationRequestResolver(this.authorizationRequestResolver())
.accessTokenResponseClient(this.accessTokenResponseClient())
)
);
}
}