小编Sta*_*mir的帖子

Spring Boot 社交登录和 Google 日历 API

问题

通过 Spring Security OAuth2 重用最终用户 Google 身份验证来访问 Web 应用程序中的 Google Calendar API

描述

我能够通过 Spring Security 登录创建一个小型 Spring Boot Web 应用程序

应用程序.yaml

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: <id>
            client-secret: <secret>
            scope:
              - email
              - profile
              - https://www.googleapis.com/auth/calendar.readonly
Run Code Online (Sandbox Code Playgroud)

当应用程序启动时,我可以访问http://localhost:8080/user并要求用户登录谷歌。成功登录后,配置文件 json 将作为以下响应显示在浏览器中:

安全控制器

@RestController
class SecurityController {
    @RequestMapping("/user")
    fun user(principal: Principal): Principal {
        return principal
    }
}
Run Code Online (Sandbox Code Playgroud)

安全配置.kt

@Configuration
class SecurityConfiguration : WebSecurityConfigurerAdapter() {
    @Throws(Exception::class)
    override fun configure(http: HttpSecurity) {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .oauth2Login()
    }
}
Run Code Online (Sandbox Code Playgroud)

问题 …

google-calendar-api google-oauth spring-boot spring-security-oauth2 google-signin

3
推荐指数
1
解决办法
1789
查看次数