使用 Spring 授权服务器 + Spring boot 3.0.4 + Google 的 SSO 流程

问题描述 投票:0回答:2

我试图了解任何提供商(在本例中为 Google)的 SSO 流程。 我使用 Spring boot 3 和 Spring 授权服务器版本 1.0.0 实现了一个授权服务器。

授权/令牌端点运行良好,我按预期获得了 JWT 以及我添加的所有额外声明。 在每个 API 之上,我都有以下注释:

@RolesAllowed({"ADMIN"})

我现在正在尝试添加 SSO 功能,以便用户通过 Google 注册/登录。我已经在 Google 中定义了我的应用程序,将相关信息添加到 application.yaml 中:private、public、redirect-uri 并添加到我的 SecurityConfig 类中:

 @Bean
    @Order(2)
    public SecurityFilterChain defaultSecurity(HttpSecurity http) throws Exception {

        http.csrf().disable()
                .cors().configurationSource(corsConfigurationSource())
                .and()
                .authorizeHttpRequests()
                .requestMatchers("/v1/sign-up/**")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .oauth2ResourceServer()
                .jwt().decoder(jwtDecoder)
                .jwtAuthenticationConverter(customJwtAuthenticationConverter())
                .and()
                .and()
                .formLogin(c ->
                        c.defaultSuccessUrl("/my-ui/index.html"))
                .oauth2Login()
                .userInfoEndpoint()
                .userService(oauthUserService)
                .and()
                .successHandler((request, response, authentication) -> {
                    CustomOAuth2User oauthUser = (CustomOAuth2User) authentication.getPrincipal();
                    response.sendRedirect("http://localhost:8080/my-ui/index.html");
                    });

        return http.build();
    }

我的 CustomOAuth2UserService.java:

@Service
public class CustomOAuth2UserService extends DefaultOAuth2UserService {

    @Override
    public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
        OAuth2User user = super.loadUser(userRequest);
        return new CustomOAuth2User(user);
    }
}

我成功执行了登录,但是当我尝试运行任何 API 时,我失败了,因为当我尝试检查权限时,我当然没有这些权限,因为这不是我的带有额外声明的 JWT 令牌,而且因为启动的令牌是OAuth2AuthenticationToken 而不是 JWTAuthenticationToken。

我的问题:

  1. 用户使用 SSO 注册/登录后,我是否需要创建 JWT?
  2. 如果我没有实现 JWT 编码怎么办?我不想实现与 Spring 在幕后的做法不同的东西。我不想要 2 个实现。
  3. 如果#1为true,创建JWT后,如何将其返回给用户?

非常感谢您的帮助

java spring-boot jwt single-sign-on spring-authorization-server
2个回答
0
投票

现在回答我自己的问题..

我添加到我的用户实体

implements OAuth2User
以及 UserDetails。 现在,当身份验证成功时,返回的主体是我的用户,其中包含所需的所有角色和范围。


-1
投票

@Ran 如果您的组织有多个应用程序(App1、App2、App3)怎么办? 那么 SSO 流程是如何定义的?

© www.soinside.com 2019 - 2024. All rights reserved.