四川做网站设计的公司,宣城网站开发网络公司,公司微信网站开发,wordpress预览文章目录 一、项目概述二、环境准备三、创建GitHub OAuth应用四、项目依赖配置五、配置OAuth 2.0六、创建控制器七、创建视图八、运行应用九、用户界面展示十、总结 在现代的Web应用中#xff0c;安全性是一个不可忽视的因素。OAuth 2.0作为一种流行的授权框架#xff0c;提供… 文章目录 一、项目概述二、环境准备三、创建GitHub OAuth应用四、项目依赖配置五、配置OAuth 2.0六、创建控制器七、创建视图八、运行应用九、用户界面展示十、总结 在现代的Web应用中安全性是一个不可忽视的因素。OAuth 2.0作为一种流行的授权框架提供了一种便捷的方式让用户通过社交平台如GitHub登录你的应用。本文将详细介绍如何使用Spring Security实现OAuth 2.0登录并在页面上展示用户信息。 一、项目概述
本项目旨在实现一个简单的Web应用允许用户通过GitHub进行登录。我们将使用Spring Boot和Spring Security进行快速开发。
二、环境准备
确保你已经安装了以下环境
JDK 8或更高版本Maven或GradleIDE如IntelliJ IDEA或Eclipse
三、创建GitHub OAuth应用
在开始编码之前你需要在GitHub上创建一个OAuth应用。以下是创建过程的步骤 登录GitHub访问GitHub官网并登录你的账号。 进入设置点击右上角的个人头像选择“Settings”。 找到OAuth应用在左侧菜单中点击“Developer settings”然后选择“OAuth Apps”。 注册新应用点击“New OAuth App”按钮。 填写应用信息填写应用名称、主页 URL 和回调 URL通常是 http://localhost:8080/login/oauth2/code/github然后点击“Register application”。 获取Client ID和Client Secret注册完成后你会看到Client ID和Client Secret保存这两个值。
四、项目依赖配置
在你的pom.xml中添加以下依赖如果你使用的是Maven dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-oauth2-client/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId
/dependency五、配置OAuth 2.0
在application.yml中配置GitHub的OAuth 2.0客户端信息填入你在上面步骤中获取的client-id和client-secret。
spring:security:oauth2:client:registration:github:client-id: 填入你的数据client-secret: 填入你的数据六、创建控制器
接下来我们需要创建一个控制器来处理登录后的请求。以下是IndexController的代码
package com.takumilove.oauth2logindemo.controller;import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;/*** author RaoPengFei* create 2024/10/12*/
Controller
public class IndexController {GetMapping(/)public String index(Model model, RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,AuthenticationPrincipal OAuth2User oauth2User) {model.addAttribute(userName, oauth2User.getName());model.addAttribute(clientName, authorizedClient.getClientRegistration().getClientName());model.addAttribute(userAttributes, oauth2User.getAttributes());return index;}
}七、创建视图
使用Thymeleaf创建登录后的视图以下是index.html的代码
!DOCTYPE html
html xmlnshttp://www.w3.org/1999/xhtml xmlns:thhttps://www.thymeleaf.org xmlns:sechttps://www.thymeleaf.org/thymeleaf-extras-springsecurity5
headtitleSpring Security - OAuth 2.0 Login/titlemeta charsetutf-8 /
/head
body
div stylefloat: right th:fragmentlogout sec:authorizeisAuthenticated()div stylefloat:leftspan stylefont-weight:boldUser: /spanspan sec:authenticationname/span/divdiv stylefloat:nonenbsp;/divdiv stylefloat:rightform action# th:action{/logout} methodpostinput typesubmit valueLogout //form/div
/div
h1OAuth 2.0 Login with Spring Security/h1
divYou are successfully logged in span stylefont-weight:bold th:text${userName}/spanvia the OAuth 2.0 Client span stylefont-weight:bold th:text${clientName}/span
/div
divnbsp;/div
divspan stylefont-weight:boldUser Attributes:/spanulli th:eachuserAttribute : ${userAttributes}span stylefont-weight:bold th:text${userAttribute.key}/span: span th:text${userAttribute.value}/span/li/ul
/div
/body
/html八、运行应用
确保所有配置无误后运行你的Spring Boot应用。访问http://localhost:8080你将看到一个通过GitHub登录的选项。
九、用户界面展示
在用户成功登录后界面将显示用户信息如下图所示 十、总结
本文详细介绍了如何使用Spring Security实现OAuth 2.0登录并展示了用户信息。希望通过这些步骤你能够顺利地实现GitHub登录功能提升你的应用安全性和用户体验。