Spring Boot + Spring Security 5 + OAuth2/OIDC 客户端 - 基础知识 步骤 1:步骤 2:步骤 3:步骤 4:疑惑:如何在 Spring Security 6 中禁用显示登录页面?我尝试这样做:}

2025-06-10

Spring boot + Spring Security 5 + OAuth2/OIDC 客户端 - 基础知识

步骤1:

第 2 步:

步骤3:

步骤4:

令人震惊:

如何在 Spring Security 6 中禁用显示登录页面?我尝试这样做来禁用它:

}

很久以来,我一直想使用 Spring Security 集成 OpenID Connect 提供程序。上次尝试时,我觉得它非常复杂,所以写了自己的。由于 Spring Security 5 原生支持 OAuth2 客户端,并扩展了其对 OpenID Connect 的使用,我想看看它集成起来有多么容易。

在这个例子中,我们将构建一个简单的应用程序,当我们尝试访问受保护的端点时,重定向到谷歌

步骤1:

从https://start.spring.io创建具有以下依赖项的spring boot 项目

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
    implementation 'org.jetbrains.kotlin:kotlin-reflect'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
}

第 2 步:

创建一个将显示当前用户身份验证数据的端点

@RestController
class HelloController {

    @GetMapping("/me")
    fun hello(currentUser: OAuth2AuthenticationToken): ResponseEntity<OAuth2AuthenticationToken> {
        return ResponseEntity.ok(currentUser)
    }

}

步骤3:

在 application.yml 中配置 OAuth2 客户端信息。在 Google 开发者控制台中,将应用的重定向 URI 配置为http://localhost:8080/login/oauth2/code/google

# @see https://console.developers.google.com/apis/ to create your client credentials
logging.level.org.springframework: INFO
spring:
  security:
    oauth2:
      client:
        registration:
          google:
            provider: google
            client-id: <<your-client-id>>
            client-secret: <<your-client-secret>> 
            client-authentication-method: basic
            authorization-grant-type: authorization_code
            scope:
              - openid
              - email
              - profile
              - https://www.googleapis.com/auth/tasks.readonly
        provider:
          google:
            issuer-uri: https://accounts.google.com

步骤4:

运行应用程序,转到http://localhost:8080/me,完成登录过程,您将看到此内容。

{
"authorities": [
{
"authority": "ROLE_USER",
"attributes": {
"at_hash": "28AV0o6xKM8f3UQlljlGuw",
"sub": "10080000000000000",
"email_verified": true,
"iss": "https://accounts.google.com",
"given_name": "Syamala",
"locale": "en",
"picture": "https://lh6.googleusercontent.com/photo.jpg",
"aud": [
"client-id"
],
"azp": "client-id",
"name": "Syamala Umamaheswaran",
"exp": "2019-03-24T18:27:19Z",
"family_name": "Umamaheswaran",
"iat": "2019-03-24T17:27:19Z",
"email": "xxxx@gmail.com"
},
"idToken": {...},
"userInfo": null
}
],
"details": null,
"authenticated": true,
"principal": {},
"authorizedClientRegistrationId": "google",
"credentials": "",
"name": "10080000000000000"
}

令人震惊:

令人震惊

无需编写任何安全代码就能与 OpenID Connect 提供商集成,这真是让我惊叹不已,但我还是想知道这一切是如何如此轻松地实现的。细节决定成败,敬请期待我的下一篇博文,我将在其中详细解释幕后机制,以及如何访问受保护的资源以及如何自动刷新令牌。

完整源代码@ https://github.com/shyamz-22/oidc-spring-security-5

鏂囩珷鏉ユ簮锛�https://dev.to/shyamala_u/spring-boot--spring-security-5--oauth2oidc-client---basics-4ibo
PREV
A complete beginner friendly Python Flask tutorial 🐍. Learn from basic template rendering to deploying in web servers. A complete Flask tutorial for beginners Quickstart
NEXT
Minisauras:Github 上的一个操作,用于缩小 CSS 和 JS 文件 Minisauras 🎉