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

iis7.5 网站配置电子商务网站建设调查分析

iis7.5 网站配置,电子商务网站建设调查分析,网站开发api中文手册chm,网络营销课程感悟基于SpringBoot和OAuth2,实现通过Github授权登录应用 文章目录 基于SpringBoot和OAuth2,实现通过Github授权登录应用0. 引言1. 创建Github应用2. 创建SpringBoot测试项目2.1 初始化项目2.2 设置配置文件信息2.3 创建Controller层2.4 创建Html页面 3. 启动…

基于SpringBoot和OAuth2,实现通过Github授权登录应用

文章目录

  • 基于SpringBoot和OAuth2,实现通过Github授权登录应用
    • 0. 引言
    • 1. 创建Github应用
    • 2. 创建SpringBoot测试项目
      • 2.1 初始化项目
      • 2.2 设置配置文件信息
      • 2.3 创建Controller层
      • 2.4 创建Html页面
    • 3. 启动应用
    • 4. 其他

0. 引言

在注册登录网站或者应用时,通常会有社交方式登录,例如在登录CSDN时,会提供多种登陆方式,如下图。

在这里插入图片描述
本文介绍通过SpringBoot和OAuth2,开发自己的应用,并实现通过Github授权登录。

1. 创建Github应用

  • 首先登录Github,进入到Settings-Developer Settings,点击OAuth Apps,新建New OAuth App
    在这里插入图片描述
  • 填写相关信息
    在这里插入图片描述

点击注册应用

  • 注册完成后打开,可以获得Client IDClient secrets

注意!
Client secrets要注意复制下来保存,不然在进入这个页面,也获取不到原来完整的Client secrets了,只能重新生成!

在这里插入图片描述

2. 创建SpringBoot测试项目

2.1 初始化项目

初始化项目,同时应包含以下依赖

Spring Web
Thymeleaf
Spring Security
OAuth2 Client

在这里插入图片描述
创建完成后,创建Controller文件和index文件。最终项目结构目录如下:
在这里插入图片描述

2.2 设置配置文件信息

application.yml:

spring:security:oauth2:client:registration:github:client-id: xxxclient-secret: xxx

将上面生成的client-id和client-secret写入配置文件

2.3 创建Controller层

IndexController.java

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 SaoE* @date 2024/12/29 21:29*/
@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";}
}

2.4 创建Html页面

resources/templates/index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head><title>Spring Security - OAuth 2.0 Login</title><meta charset="utf-8" />
</head>
<body>
<div style="float: right" th:fragment="logout" sec:authorize="isAuthenticated()"><div style="float:left"><span style="font-weight:bold">User: </span><span sec:authentication="name"></span></div><div style="float:none">&nbsp;</div><div style="float:right"><form action="#" th:action="@{/logout}" method="post"><input type="submit" value="Logout" /></form></div>
</div>
<h1>OAuth 2.0 Login with Spring Security</h1>
<div>You are successfully logged in <span style="font-weight:bold" th:text="${userName}"></span>via the OAuth 2.0 Client <span style="font-weight:bold" th:text="${clientName}"></span>
</div>
<div>&nbsp;</div>
<div><span style="font-weight:bold">User Attributes:</span><ul><li th:each="userAttribute : ${userAttributes}"><span style="font-weight:bold" th:text="${userAttribute.key}"></span>: <span th:text="${userAttribute.value}"></span></li></ul>
</div>
</body>
</html>

3. 启动应用

  • 在浏览器输入并访问http://localhost:8080/,此时浏览器将被重定向到默认的自动生成的登录页面,该页面显示了一个用于GitHub登录的链接。
    在这里插入图片描述

点击授权

  • 此时,OAuth客户端访问GitHub的获取用户信息的接口获取基本个人资料信息,并建立一个已认证的会话。
    在这里插入图片描述

4. 其他

SpringBoot源码的CommonOAuth2Provider类中,默认配置了GOOGLEFACEBOOKGITHUBOKTA的授权登录配置
在这里插入图片描述
以Github为例,默认配置如下:

    GITHUB {public Builder getBuilder(String registrationId) {Builder builder = this.getBuilder(registrationId, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, "{baseUrl}/{action}/oauth2/code/{registrationId}");builder.scope(new String[]{"read:user"});builder.authorizationUri("https://github.com/login/oauth/authorize");builder.tokenUri("https://github.com/login/oauth/access_token");builder.userInfoUri("https://api.github.com/user");builder.userNameAttributeName("id");builder.clientName("GitHub");return builder;}},
http://www.yayakq.cn/news/653861/

相关文章:

  • 工商银行与建设银行网站对比电脑大型网络游戏排行
  • 武进网站建设怎么样网站建设入固定资产
  • 排名好的郑州网站建设公司建设网站时的注意事项
  • 个人做的网站能备案吗推动品牌建设的网站
  • 在农村做相亲网站怎么样如何查询一个网站是那家公司做的
  • 图片站手机网站怎么做的wordpress电商支付宝微信
  • 在谷歌上网站推广搭建漏洞网站
  • 河南省百城建设提质网站推广优化公司网站
  • 网站建设方案情况汇报企业网站备案资料
  • 闽侯做网站上海中学门户网站登陆
  • 广州公司建站目前国际电商平台有哪些
  • 前端代码做招新网站优购物官方网站地址
  • 12306网站开发时间房屋 哪个网站做的最好
  • 乐陵网络推广seo优化苏州seo公司
  • 如何解决网站兼容站长之家官网查询
  • 什么网站上公司的评价最客观wordpress 没关插件
  • 制作公司网站多少钱网站内容建设 内容审核流程
  • 什么网站有项目做网站运营条件
  • 网站建设步骤的论文互联网最好的公司
  • 网站后台代码如何做织梦网站怎么做404页面模板
  • 找考卷做要去哪个网站汕头建站模板厂家
  • 黑龙江建设网网站长春网站制作都找源晟27
  • 盘锦门户网站制作wordpress下拉式菜单
  • 在线购物商城网站建设网站游戏网站开发设计菲律宾
  • 《电子商务网站建设》精品课天津智能网站建设多少钱
  • 手机自助建站平台dw怎么做网站轮播图
  • 电商网站价格监控梅州市城乡建设部网站首页
  • 3d渲染网站建设wordpress 前端构建
  • 哪些做调查问卷的网站怎么屏蔽优酷网站的广告
  • 东莞广告网站建设合肥网站建设久飞