【SpringSecurity】十五、完成系统支持Github三方登录


SpringSecurity OAuth2.0文档: https://docs.spring.io/spring-security/reference/servlet/oauth2/index.html

1、需求

对接Github,在自己系统实现支持Github登录。

2、在对接系统中完成客户端注册

头像 ⇒ Settings ⇒ Developer settings ⇒ OAuth Apps ⇒ 注册一个新的应用

在这里插入图片描述

在这里插入图片描述

注册成功,拿到客户端id和密钥。请求Github的授权服务器时,携带clinet_id和secret,Github就知道你是一个合法的客户端应用。

在这里插入图片描述

3、创建客户端应用

//官方示例
https://github.com/spring-projects/spring-security-samples/blob/main/servlet/spring-boot/java/oauth2/login/README.adoc

新建模块,搜索勾选springweb、thymeleaf、spring security、oauth2 client依赖:

在这里插入图片描述

上一步完成客户端注册后,这里写application配置:

spring:
  security:
    oauth2:
      client:
        registration:
          github:
            client-id: 33ddf22a5738dcdeeca4
            client-secret: 79519586acf22ef10597e5d94ee0e67d39ebd64

写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;

@Controller
public class OAuth2LoginController {

	@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";
	}

}

写index视图:

<!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>

启动程序,localhost:8080/login

在这里插入图片描述

点击跳转github的登录页面:

在这里插入图片描述

在github页面完成登录,在授权页面点击Authoize授权

在这里插入图片描述

客户端成功获取到github上的用户信息:

在这里插入图片描述

退出登录后,再次授权直接授权成功,因为token还没过期:

在这里插入图片描述

以上的实际流程为(绝大多数步骤已被框架spring security oauth2 client实现了):

  • A网站(客户端)让用户跳转到 GitHub,并携带参数client_id 以及 redirection url
  • GitHub 要求用户登录,然后询问用户"A网站要求获取用户信息的权限,你是否同意?"
  • 用户同意,GitHub 就会重定向回A网站,同时发回一个授权码
  • A网站使用授权码,向 GitHub 请求令牌token
  • GitHub 返回令牌token
  • A网站使用令牌,向 GitHub资源服务器请求用户数据
  • GitHub返回用户数据
  • A网站使用 GitHub用户数据登录

4、CommonOAuth2Provider

CommonOAuth2Provider是一个预定义的通用OAuth2Provider,为一些知名资源服务API提供商如Google、GitHub、Facebook,预定义了一组默认的属性。包括授权码URI、换取令牌URI和请求用户信息URI,因为它们不经常变化。因此,提供默认值以减少所需的配置。所以配置GitHub客户端时,只需要提供client-id和client-secret就行。

在这里插入图片描述

  • 33
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
智能排班系统是一个应用了SpringSpring MVC、MyBatis、PageHelper等技术的系统Spring是一个开源的Java开发框架,提供了依赖注入和面向切面编程等功能,可以简化应用程序开发的复杂性。Spring框架的主要特点是易于扩展和集成其他框架。 Spring MVC是Spring框架中的模块,用于开发基于Model-View-Controller模式的Web应用程序。它通过请求映射和视图解析等功能,将用户请求和响应进行有效地处理和分发。 MyBatis是一种Java持久层框架,通过XML或注解对数据库操作进行配置,提供了对SQL语句的执行和结果的映射功能。它简化了数据库访问的复杂性,提供了更好的SQL控制和性能优化。 PageHelper是一个开源的MyBatis物理分页插件,可以自动地对查询结果进行分页处理。它提供了简单的配置和使用方式,能够有效地减轻数据库的查询压力,提高系统性能。 GitHub是一个基于Git版本控制系统的代码托管平台,开发人员可以在上面创建和管理项目的代码仓库。它提供了多人协同开发、版本控制、代码审查、问题追踪等功能,能够有效地提高开发效率和代码质量。 综上所述,智能排班系统应用了SpringSpring MVC、MyBatis和PageHelper等技术,通过依赖注入、面向切面编程、模型-视图-控制器模式和物理分页等功能,实现了对数据库操作的简化、Web应用程序的高效处理和查询结果的分页处理。同时,利用GitHub进行代码托管,实现了多人协同开发和版本控制,提高了系统的可维护性和代码的质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-代号9527

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值