spring-oauth客户端中OIDC和OAuth2之间的差异



我的目标是通过身份验证服务(如谷歌或github(对用户进行身份验证。

我试着同时使用这两种方法,但我不明白为什么在github中,我的身份验证是用我的OAuth2UserService处理的,而在谷歌中,这是我的OidcUserService,它被调用。

我希望两者都调用OidcUserService,因为这是我唯一需要的身份验证。

那么,为什么会有这样的区别呢?你能告诉我这件事吗?我错过什么了吗?

说明的一些代码

@Service
public class CustomOAuth2UserService extends DefaultOAuth2UserService {
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
OAuth2User user = super.loadUser(userRequest);
log.info("OAuth2User loading");
return user;
}
}
@Service
public class CustomOidcUserService extends OidcUserService {
@Override
public OidcUser loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException {
OidcUser user = super.loadUser(userRequest);
log.info("OidcUser loading");
return user;
}
}
// MyAppSecurityConfig.java
@Configuration
public class MyAppSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2Login()
;
}
}
# application.properties
spring.security.oauth2.client.registration.github.client-id=xxxx
spring.security.oauth2.client.registration.github.client-secret=xxx
spring.security.oauth2.client.registration.google.client-id=xxxx
spring.security.oauth2.client.registration.google.client-secret=xxxx

您观察到的行为是由spring-boot:中预定义的oauth2配置引起的

对于常见的OAuth2和OpenID提供商,包括Google、Github、,Facebook和Okta,我们提供了一组提供商默认值(谷歌,分别为github、facebook和okta(。

如果不需要自定义这些提供程序,可以设置provider属性设置为需要推断其默认值的属性。此外,如果客户端注册的密钥与默认密钥匹配Spring Boot也推断出了这一点。

即spring-boot为google服务预配置了openid connect,为github预配置了通用oauth2。

最新更新