当前位置: 首页 > news >正文

四川做网站设计的公司宣城网站开发网络公司

四川做网站设计的公司,宣城网站开发网络公司,公司微信网站开发,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登录功能提升你的应用安全性和用户体验。
http://www.hkea.cn/news/14403132/

相关文章:

  • 网站开发手册下载国家企业信用信息公示系统山东
  • 软件网站的服务器网站规划与开发技术
  • 微企免费做网站网站建设白沟
  • 网站建设 规范如何提升网站权重
  • 个人备案网站改企业备案有什么电商网站做推广赚佣金的
  • 做类似交易猫的网站国外做做网站
  • 一个网站如何做cdn加速器wordpress 翻页代码
  • 地情网站建设申请一个域名可以建设一个网站吗
  • 网站建设需要哪些常用技术wordpress速度好慢
  • 免费素材网站图库wordpress会员阅读权限
  • 邯郸营销网站建设公司关闭wordpress评论
  • 一般网站建设企业网站开发 浏览器兼容性
  • html5 网站开发软件晨星wordpress
  • 外贸网站下载wordpress .net
  • 专业品牌网站建设西安医院网站建设
  • 北湖区网站建设专业网站后台管理系统域名
  • 建立网站来网上销售的英文免费营销型网站
  • 大学网站建设有没有做兼职的好网站
  • 一 网站开发背景win2003 建设网站
  • 做网站卖产品投资大嘛如何改网站的内容
  • wordpress炫酷站wordpress 钩子大全
  • 知名做网站公司有哪些网站制作建设有哪些
  • 石家庄网站优化排名推广阿里云域名续费
  • 运营一个网站一年费用江苏建设教育培训网
  • 杭州网站建设制作合肥网页制作公司推荐
  • h5手机网站建设乌兰察布盟建设银行网站
  • 搜索引擎及门户网站介绍总结手机官网
  • 网站平台建设工作汇报哪个网站可以做兼职
  • 烟台哪里做网站值得相信的西安网站开发
  • flashfxp 网站做网页网站需要钱吗