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

Java17 --- SpringSecurity之OAuth2

一、OAuth2

1.1、使用github以授权码方式

1.1.1、注册应用程序

 

 

1.1.2、测试代码

 pom依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-oauth2-client</artifactId></dependency>
spring:security:oauth2:client:registration:github:client-id: Ov23liQnQPka0x1qX6ZIclient-secret: 278f3ad4ecdd8dad4b318d7bd8d33f2bb9428019
@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";}
}
<!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>

 

 

 

http://www.lryc.cn/news/377455.html

相关文章:

  • 服务器上线的一些事
  • OceanBase-docker安装、连接数据库、修改mysql用户密码
  • 浪潮(Inspur)服务器硬件监控指标解读
  • 极简opencv操作xml文件
  • 更换域名流程记录
  • CSS 实现电影信息卡片
  • Skype机器人
  • 海外仓系统能解决海外仓哪些难题?海外仓标准化管理实用指南
  • 从零开始精通Onvif之录像存储
  • vue3面试题八股集合——2024
  • 第2章 Rust初体验5/8:match表达式和模式匹配:更富表达力:猜骰子冷热游戏
  • 1台UG图形工作站实现5-7人共享使用
  • Dubbo 3.x源码(22)—Dubbo服务引用源码(5)服务引用bean的获取以及懒加载原理
  • nodejs——原型链污染
  • 忘记 iPhone 密码:如果忘记密码,如何解锁 iPhone
  • 案例 采用Springboot默认的缓存方案Simple在三层架构中完成一个手机验证码生成校验的程序
  • 第四届人工智能、机器人和通信国际会议(ICAIRC 2024)
  • ctr/cvr预估之FM模型
  • HAL-DMA中断空闲接受不定长数据
  • 【会议征稿,CPS出版】第四届管理科学和软件工程国际学术会议(ICMSSE 2024,7月19-21)
  • 无引擎游戏开发(3):数据结构设计|功能函数完善
  • Laravel 高级:了解$loop
  • 深入理解指针(1)
  • 在无线网中 2.4G、5G、WiFi6、WiFi7 都是什么意思?
  • milvus元数据解析工具milvusmetagui介绍使用
  • LabVIEW电磁超声热态金属在线缺陷检测系统
  • leecode代码模板
  • 可靠性测试及模型计算
  • 【Tools】 深入了解Burp Suite:Web应用抓包利器
  • 技术先进、应用广泛、社区活跃的[项目名称]