当前位置: 首页 > 知识库问答 >
问题:

Spring Boot社交登录和Google日历API

左丘楷
2023-03-14

通过Spring Security OAuth2重用终端用户Google身份验证以访问Web应用程序中的Google日历API

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

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: <id>
            client-secret: <secret>
            scope:
              - email
              - profile
              - https://www.googleapis.com/auth/calendar.readonly

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

@RestController
class SecurityController {
    @RequestMapping("/user")
    fun user(principal: Principal): Principal {
        return principal
    }
}
@Configuration
class SecurityConfiguration : WebSecurityConfigurerAdapter() {
    @Throws(Exception::class)
    override fun configure(http: HttpSecurity) {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .oauth2Login()
    }
}

我想重用此身份验证来检索所有用户的日历事件。下面的代码摘自google关于访问日历API的教程,但它创建了一个完全独立的授权流,并要求用户登录。

    @Throws(IOException::class)
    private fun getCredentials(httpTransport: NetHttpTransport): Credential {
        val clientSecrets = loadClientSecrets()
        return triggerUserAuthorization(httpTransport, clientSecrets)
    }

    private fun loadClientSecrets(): GoogleClientSecrets {
        val `in` = CalendarQuickstart::class.java.getResourceAsStream(CREDENTIALS_FILE_PATH)
                ?: throw FileNotFoundException("Resource not found: $CREDENTIALS_FILE_PATH")
        return GoogleClientSecrets.load(JSON_FACTORY, InputStreamReader(`in`))
    }

    private fun triggerUserAuthorization(httpTransport: NetHttpTransport, clientSecrets: GoogleClientSecrets): Credential {
        val flow = GoogleAuthorizationCodeFlow.Builder(
                httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(FileDataStoreFactory(File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build()
        val receiver = LocalServerReceiver.Builder().setPort(8880).build()
        return AuthorizationCodeInstalledApp(flow, receiver).authorize("user")
    }

如何重用已完成的身份验证来访问最终用户在Google帐户上的日历事件?

共有1个答案

莘睿
2023-03-14

如果我理解正确,那么重用身份验证的意思是,您希望使用Spring为您检索的访问和刷新令牌,以便将它们用于针对Google API的请求。

用户身份验证详细信息可以注入到endpoint方法中,如下所示:

import org.springframework.security.oauth2.client.OAuth2AuthorizedClient
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class FooController(val historyService: HistoryService) {

    @GetMapping("/foo")
    fun foo(@RegisteredOAuth2AuthorizedClient("google") user: OAuth2AuthorizedClient) {
        user.accessToken
    }

}

有了OAuth2AuthorizedClient中的详细信息,您应该可以使用google API执行任何需要的操作。

如果您需要在没有用户向您的服务发出请求的情况下访问API,可以将OAuth2AuthorizedClientService注入到托管组件中,并按如下方式使用:

import org.springframework.security.oauth2.client.OAuth2AuthorizedClient
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService
import org.springframework.stereotype.Service

@Service
class FooService(val clientService: OAuth2AuthorizedClientService) {

    fun foo() {
        val user = clientService.loadAuthorizedClient<OAuth2AuthorizedClient>("google", "principal-name")
        user.accessToken
    }

}
 类似资料:
  • 我想从react js客户端和后端节点js使用KeyClope社交登录。我找不到任何方法从KeyClope生成红色标记的链接。 http://localhost:8080/auth/realms/softspace/broker/github/login?client_id=account 我不知道他们是如何生成标签id和会话代码的。

  • 我正在开发一个rest服务,它将通过浏览器、单页应用程序和移动应用程序在浏览器中提供。现在我的服务没有Spring就可以工作了。oauth2客户机是在过滤器内部实现的,可以说是“手工”。 我正在尝试将其迁移到spring Boot。阅读了大量的手册和谷歌搜索了大量的信息,我正在努力了解以下内容是否对客户来说是可能的: > 通过Spring-Security-Oauth2提供的所有帮助,使用face

  • 我想使用谷歌的REST API访问公共日历。 Google的日历API建议我需要OAuth令牌才能访问日历: https://developers.google.com/google-apps/calendar/auth 然而,我正在访问一个公共日历,并且正在服务器上执行此操作,因此不能/不应该要求用户进行身份验证。 我使用node.jsapi: 这将返回“您的客户端发出了一个格式错误或非法的请求

  • 1. 申请应用 1.1 创建第三方授权应用 登录 Google 开发者中心:Google 开发者中心 (opens new window) 点击新建项目:新建项目 (opens new window) 确保在第二步新建的项目下,选择“API 和服务”-“凭据” 创建凭据 创建凭据时,选择 “OAuth 客户端 ID” 首次创建凭据时系统会提示“如需创建 OAuth 客户端 ID,您必须先在同意屏幕

  • 我从快速入门开始(https://developers.google.com/google-apps/calendar/quickstart/python)而且效果很好。然后我尝试用这个指南插入事件(https://developers.google.com/google-apps/calendar/create-events)。我将此代码添加到quickstart的代码中,但出现错误。如何查看我

  • 如果这是基本的/愚蠢的,请原谅我。使用Firebase,注册新用户的“正确”方法是什么? > 收集他的电子邮件并以平淡无奇的形式传递,然后手动发送到以创建一个新用户? 使用并以某种方式将其连接到Firebase?https://developers.google.com/identity/sign-in/web/build-button 关于Facebook有什么明显的方法吗?