【问题标题】:Difference between OIDC and OAuth2 in spring oauth clientspring oauth 客户端中 OIDC 和 OAuth2 的区别
【发布时间】:2022-01-20 20:31:36
【问题描述】:

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

我尝试同时使用两者,但我不明白为什么在 github 上我的身份验证由我的 OAuth2UserService 处理,而在 google 中,这是我的 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

【问题讨论】:

    标签: java spring oauth-2.0 openid-connect


    【解决方案1】:

    你所观察到的行为是由predefined oauth2 configurations in spring-boot引起的:

    对于常见的 OAuth2 和 OpenID 提供程序,包括 Google、Github、 Facebook 和 Okta,我们提供了一组提供程序默认值(google、 github、facebook 和 okta)。

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

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

    【讨论】:

    • 好的,但是预配置中的什么表明一个人使用 oidc 而其他人使用 oauth2 ?以及如何配置我的应用程序以同时使用 oidc ?在CommonOAuth2Provider.. 中没有任何迹象表明这种行为
    • “在 CommonOAuth2Provider 中没有什么可以引用此行为” - 实际上,在配置的范围中存在“openid”是有道理的。如果要覆盖默认值,则需要按照此处所述配置提供程序:docs.spring.io/spring-boot/docs/current/reference/htmlsingle/…
    猜你喜欢
    • 2014-08-29
    • 2018-08-22
    • 2012-10-22
    • 2021-05-31
    • 1970-01-01
    • 2016-10-31
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多