# springsecurityoauth2 **Repository Path**: hw50205/springsecurityoauth2 ## Basic Information - **Project Name**: springsecurityoauth2 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-06-18 - **Last Updated**: 2024-06-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README = Samples [[default-sample]] ==默认样本 默认示例提供了开始使用Spring Authorization Server的最低配置。 [[demo-sample]] ==演示示例 演示示例为Spring Authorization Server实现的各种功能提供了自定义配置。 [[运行演示示例]] ===运行示例 *运行授权服务器->`./gradlew -b samples/demo-authorizationserver/samples-demo-authorizationserver.gradle bootRun` *运行客户端->`./gradlew -b samples/demo-client/samples-demo-client.gradle bootRun` *运行资源服务器-> `./gradlew -b samples/messages-resource/samples-messages-resource.gradle bootRun` 访问 http://127.0.0.1:8080` **使用凭据登录-> user1 password [[一般使用]] default-authorizationserver 服务端 demo-client 客户端 其他拓展用 [[configuring-social-login]] ===配置社交登录 演示样本可以被配置为提供社交登录能力。 [[google-login]] ===使用谷歌登录 本节展示了如何将谷歌配置为社交登录提供商。 [[谷歌初始设置]] =====初始设置 若要使用Google的OAuth 2.0身份验证系统进行登录,您必须在Google API控制台中设置一个项目以获取OAuth 2.0凭据。 注意:https://developers.google.com/identity/protocols/OpenIDConnect用于身份验证的[Google的OAuth 2.0实现]符合 https://openid.net/connect/[OpenID Connect 1.0]规范,并且是https://openid.net/certification/[OpenID认证]。 请按照上的说明进行操作https://developers.google.com/identity/protocols/OpenIDConnect[OpenID Connect]页面,从“设置OAuth 2.0”部分开始。 完成“获取OAuth 2.0凭据”说明后,您应该有一个新的OAuth客户端,其凭据包括客户端ID和客户端机密。 [[谷歌重定向URI]] =====设置重定向URI 重定向URI是最终用户的用户代理在通过Google身份验证后重定向回的应用程序中的路径 并已在同意页面上授予对OAuth客户端_(在上一步骤中创建)_的访问权限。 在“设置重定向URI”小节中,确保将*Authorized redirect URIs*字段设置为`http://localhost:9000/login/oauth2/code/google-idp`。 提示:默认重定向URI模板为“{baseUrl}/login/oauth2/code/{registrationId}”。 *_registrationId_*是“ClientRegistration”的唯一标识符。 [[谷歌应用程序配置]] =====配置application.yml 现在您在Google中有了一个新的OAuth客户端,您需要将应用程序配置为将OAuth Client用于_authentication流程。为此: 。转到“application.yml”并设置以下配置: + [source,yaml] ---- spring: security: oauth2: client: registration: <1> google-idp: <2> provider: google client-id: google-client-id client-secret: google-client-secret ---- + .OAuth客户端属性 ==== <1> “spring.security.oauth2.client.registration”是OAuth客户端属性的基本属性前缀。 <2> 基本属性前缀后面是“ClientRegistration”的ID,例如google idp。 ==== 。将“client id”和“client secret”属性中的值替换为先前创建的OAuth 2.0凭据。 或者,您可以在Spring Boot应用程序中设置以下环境变量: *`GOOGLE_CLIENT_ID` *`GOOGLE_CLIENT_SECRET` [[github-login]] ===使用GitHub登录 本节展示如何将GitHub配置为社交登录提供商。 [[github注册应用程序]] =====注册OAuth应用程序 要使用GitHub的OAuth 2.0身份验证系统进行登录,您必须https://github.com/settings/applications/new[注册新的OAuth应用程序]。 注册OAuth应用程序时,请确保将*Authorization callback URL*设置为`http://localhost:9000/login/oauth2/code/github-idp`。 授权回调URL(重定向URI)是最终用户的用户代理在使用GitHub进行身份验证后重定向回的应用程序中的路径 并已在_授权应用程序_页面上授予对OAuth应用程序的访问权限。 提示:默认重定向URI模板为“{baseUrl}/login/oauth2/code/{registrationId}”。 *_registrationId_*是“ClientRegistration”的唯一标识符。 [[github应用程序配置]] =====配置application.yml 现在您有了一个带有GitHub的新OAuth应用程序,您需要将该应用程序配置为将OAuth程序用于_authentication流程。为此: 。转到“application.yml”并设置以下配置: + [source,yaml] ---- spring: security: oauth2: client: registration: <1> github-idp: <2> provider: github client-id: github-client-id client-secret: github-client-secret ---- + .OAuth客户端属性 ==== <1> “spring.security.oauth2.client.registration”是OAuth客户端属性的基本属性前缀。 <2> 基本属性前缀后面是“ClientRegistration”的ID,例如github idp。 ==== 。将“client id”和“client secret”属性中的值替换为先前创建的OAuth 2.0凭据。 或者,您可以在Spring Boot应用程序中设置以下环境变量: *`GITHUB_CLIENT_ID` *`GITHUB_CLIENT_SECRET`