OAuth 2.0 介绍和使用示例

248 阅读3分钟

        OAuth 2.0 是一种开放标准授权协议,用于安全地让第三方应用访问用户在另一个服务上的资源,而无需暴露用户的凭证。OAuth 2.0 主要用于 Web 应用程序、桌面应用程序、移动应用程序和物联网设备等场景。

OAuth 2.0 的含义

  • 资源所有者(Resource Owner):通常是最终用户。
  • 客户端(Client):请求访问资源的应用程序。
  • 资源服务器(Resource Server):存储资源并响应客户端的请求。
  • 授权服务器(Authorization Server):负责验证资源所有者的身份,并颁发访问令牌(Access Token)给客户端。

OAuth 2.0 常见场景

  1. 社交登录:用户使用第三方平台(如 Google、Facebook)的账号登录到你的网站或应用。
  2. API 访问:开发者希望应用能够访问某些用户授权的 API。
  3. 第三方应用集成:如允许某个应用发布推文到用户的 Twitter 账号上。

OAuth 2.0 授权流程

OAuth 2.0 包括四种授权方式:授权码模式、简化模式、密码模式和客户端凭证模式。以下示例使用授权码模式,这是最常见和最安全的一种方式。

获取授权码:

用户通过浏览器访问授权服务器,登录并授权后,授权服务器将重定向到客户端指定的重定向 URI,并附带授权码。GET /authorize?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&scope=YOUR_SCOPES&state=YOUR_STATE

获取访问令牌:

客户端使用授权码向授权服务器请求访问令牌。

POST /token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=AUTHORIZATION_CODE&redirect_uri=YOUR_REDIRECT_URI&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET

访问受保护资源:

客户端使用访问令牌请求资源服务器上的受保护资源。

GET /resource Authorization: Bearer ACCESS_TOKEN

具体示例

  • 注册一个 GitHub OAuth 应用,获取 client_idclient_secret
  • 设置重定向 URI(如 http://localhost:8080/login/oauth2/code/github)。

创建 Spring Boot 项目

创建一个新的 Spring Boot 项目,并添加以下依赖:

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-oauth2-client org.springframework.boot spring-boot-starter-security

配置应用程序属性

src/main/resources/application.yml 文件中,配置 GitHub 的 OAuth2 客户端信息:

spring:
security:
oauth2:
client:
registration:
github:
client-id: your_client_id
client-secret: your_client_secret
scope: user
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
authorization-grant-type: authorization_code
client-name: GitHub
provider:
github:
authorization-uri: github.com/login/oauth…
token-uri: github.com/login/oauth…
user-info-uri: api.github.com/user
user-name-attribute: login

创建控制器

创建一个控制器来处理主页和用户信息显示:

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

@GetMapping("/")
public String home() {
return "home";
}

@GetMapping("/userinfo")
public String userinfo(Model model, @AuthenticationPrincipal OAuth2AuthenticationToken authentication) {
model.addAttribute("userName", authentication.getName());
model.addAttribute("attributes", authentication.getPrincipal().getAttributes());
return "userinfo";
}
}

配置安全性

创建一个安全配置类来设置 Spring Security:

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.oauth2Login()
.defaultSuccessURL("/userinfo")
.failureURL("/login?error=true");
}
}

创建视图

创建视图模板来显示登录按钮和用户信息。首先在 src/main/resources/templates 文件夹中创建 home.htmluserinfo.html 文件。

home.html

Home

Welcome to OAuth 2.0 Demo

Login with GitHub

userinfo.html

User Info

User Info

User Name:

Attributes:

运行项目

  • 确保你已经配置好了 GitHub OAuth 应用,并将 client_idclient_secret 填写到 application.yml 中。
  • 运行 Spring Boot 应用程序。
  • 在浏览器中访问 http://localhost:8080,点击 "Login with GitHub" 按钮,完成授权流程并查看用户信息。

示例解释

  1. 依赖配置:项目使用 Spring Boot 和 Spring Security OAuth2 客户端。
  2. 配置文件:在 application.yml 中配置 OAuth2 客户端信息,包括 client_idclient_secret 和相关的 URI。
  3. 控制器:处理主页和用户信息页面的请求。
  4. 安全配置:配置 Spring Security 以启用 OAuth2 登录。
  5. 视图模板:提供登录按钮和用户信息显示的 HTML 页面。

通过示例,说明 Spring Boot 和 Spring Security OAuth2 客户端实现 OAuth 2.0 授权码模式的完整流程。