SpringBoot3集成Spring Authorization Server实现SSO单点登录

news2024/9/19 10:04:32

1. 概述

在之前的文章中介绍过SpringBoot集成OAuth2老版本的方案SpringCloud搭建微服务之OAuth2实现SSO单点登录,随着Spring Authorization Server框架的成熟和SpringBoot版本的更新,新项目必然会采用新的技术和框架,本文将使用最新的SpringBoot版本和JDK17实现SSO单点登录,本文包括三个微服务,一个认证中心,两个客户端

2. 搭建Spring Authorization Server认证中心

认证中心的搭建可以参考SpringBoot3集成Spring Authorization Server搭建服务认证中心,不同的是在application.yml中需要配置两个客户端的信息,具体配置如下:

server:
  port: 9000

spring:
  security:
    oauth2:
      authorizationserver:
        issuer: http://localhost:9000
        client:
          sso-login-client-1:
            registration:
              client-id: sso-login-client-1
              client-secret: '{noop}openid-connect-1'
              client-name: Spring SSO Login Client 1
              client-authentication-methods:
                - client_secret_basic
              authorization-grant-types:
                - authorization_code
                - refresh_token
              redirect-uris:
                - http://127.0.0.1:8081/login/oauth2/code/sso-login-client-1
                - http://127.0.0.1:8081/authorized
              scopes:
                - openid
                - profile
            require-authorization-consent: true
          sso-login-client-2:
            registration:
              client-id: sso-login-client-2
              client-secret: '{noop}openid-connect-2'
              client-name: Spring SSO Login Client 2
              client-authentication-methods:
                - client_secret_basic
              authorization-grant-types:
                - authorization_code
                - refresh_token
              redirect-uris:
                - http://127.0.0.1:8082/login/oauth2/code/sso-login-client-2
                - http://127.0.0.1:8082/authorized
              scopes:
                - openid
                - profile
            require-authorization-consent: true

3. 搭建客户端服务

3.1. 引入核心依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3.2. 编写application.yml

server:
  port: 8081
spring:
  thymeleaf:
    cache: false
  security:
    oauth2:
      client:
        registration:
          sso-login-client-1:
            provider: spring
            client-id: sso-login-client-1
            client-secret: openid-connect-1
            client-authentication-method: client_secret_basic
            authorization-grant-type: authorization_code
            redirect-uri: http://127.0.0.1:8081/login/oauth2/code/sso-login-client-1
            scope:
              - openid
              - profile
            client-name: Spring SSO Login Client 1
        provider:
          spring:
            authorization-uri: http://localhost:9000/oauth2/authorize
            token-uri: http://localhost:9000/oauth2/token
            jwk-set-uri: http://localhost:9000/oauth2/jwks

客户端2的application.yml配置文件如下:

server:
  port: 8082
spring:
  thymeleaf:
    cache: false
  security:
    oauth2:
      client:
        registration:
          sso-login-client-2:
            provider: spring
            client-id: sso-login-client-2
            client-secret: openid-connect-2
            client-authentication-method: client_secret_basic
            authorization-grant-type: authorization_code
            redirect-uri: http://127.0.0.1:8082/login/oauth2/code/sso-login-client-2
            scope:
              - openid
              - profile
            client-name: Spring SSO Login Client 2
        provider:
          spring:
            authorization-uri: http://localhost:9000/oauth2/authorize
            token-uri: http://localhost:9000/oauth2/token
            jwk-set-uri: http://localhost:9000/oauth2/jwks

3.3. 编写Controller

编写一个接口获取用户信息用于登录成功后页面显示,具体代码如下

@Controller
public class LoginClientController {

    @GetMapping(value = "/")
    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";
    }
}

3.4. 编写Filter

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class LoopbackIpRedirectFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        if (request.getServerName().equals("localhost") && request.getHeader("host") != null) {
            HttpRequest httpRequest = new ServletServerHttpRequest(request);
            UriComponents uri = ForwardedHeaderUtils.adaptFromForwardedHeaders(httpRequest.getURI(), httpRequest.getHeaders()).host("127.0.0.1").build();
            response.sendRedirect(uri.toUriString());
            return;
        }
        filterChain.doFilter(request, response);
    }
}

3.5. 编写登录成功后页面

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

4. 测试验证

依次启动认证中心和两个客户端
在浏览器输入http://127.0.0.1:8081/,在登录界面输入用户名和密码admin/123456
登录
登录后,勾选profile,点击Submit Consent
profile
授权成功后,进入client1页面
client 1
在浏览器输入http://127.0.0.1:8082/,会直接跳转到认证界面
client 2 profile
授权后,直接进入client2页面
client 2

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2092966.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

推理引擎测试-算力共享:test_inference_engine

目录 算力共享:test_inference_engine 关键点解释 实际应用和注意事项 算力共享:test_inference_engine 这段代码设计用于测试一个名为 InferenceEngine 的推理引擎,特别是测试其在处理不同分片(Shards)时的连续性和一致性。在机器学习和深度学习模型中,尤其是当模型非…

jenkins+python+appium 本地(简洁版)

jenkinspythonappium 本地&#xff08;简洁版&#xff09; 等服务器到了配到服务器上去&#xff0c;先在自己电脑上试一下。踩了n个坑&#xff0c;网上找的资料太深奥TAT 直接上操作 先把jenkins安装好&#xff08;肯定安装啦&#xff09;我的版本是2.462 新建任务 输入名称 选…

市占率最高的显示器件,TFT_LCD的驱动系统设计--Part 1

目录 一、简介 二、TFT-LCD驱动系统概述 &#xff08;一&#xff09;系统概述 &#xff08;二&#xff09;设计要点 二、扫描驱动电路设计 &#xff08;一&#xff09;概述 扫描驱动电路的功能 扫描驱动电路的组成部分 设计挑战 驱动模式 &#xff08;二&#xff09…

互联网全景消息(1)之RabbitMq基础入门

一、消息中间件 1.1消息队列回顾 消息队列中间件是分布式系统中重要的组件&#xff0c;主要解决应用解耦&#xff0c;异步消息&#xff0c;流量削锋等问题&#xff0c;实 现高性能&#xff0c;高可用&#xff0c;可伸缩和最终一致性架构。目前使用较多的消息队列有ActiveMQ &a…

js插件-模糊搜索、自动补全下拉框

问题&#xff1a;一个老系统&#xff0c;让把所有jsp页面动态生成的<select>下拉选&#xff0c;选项过多的下拉选全部改为支持模糊搜索的下拉选的功能。系统框架只有 jq 和layui&#xff08;仅用于列表和弹窗&#xff09;&#xff0c; JQurey 首先想到的就是jQuery UI …

构建以数据为核心智慧型工业园区新架构方案

1. 项目背景与目标 智慧型工业园区新架构的构建旨在通过数据驱动实现节能、绿色、高效和安全的目标&#xff0c;以应对当前工业园区在基础数据收集、系统管理和操作复杂性方面的挑战。 2. 现状分析 当前工业园区的发展面临数据收集难题、系统分散、操作复杂以及孤岛效应&…

前端面试——八股文

一、Vue2篇 1. 关于生命周期 1.1 生命周期有哪些&#xff1f;发送请求在created还是mounted&#xff1f; 请求接口测试&#xff1a;https://fcm.52kfw.cn/index.php?_mall_id1&rapi/default/districtVue2.x系统自带有8个 beforeCreate created beforeMount mounted be…

电子签合同区块链存证合约小程序开源版开发

电子签合同区块链存证合约小程序开源版开发 电子合同底层对接的腾讯电子签接口&#xff0c;支持自定义模版发起合同和文件发起合同&#xff0c;支持骑缝章&#xff0c;多方签署&#xff0c;腾讯至信链提供区块链存证&#xff0c;安全高效签署合同文书。 特色功能 自定义合同模…

(计算机论文)基于SpringBoot和Vue的台球赛事服务网站的设计与实现

毕业设计&#xff08;论文&#xff09; 博主可接毕设论文&#xff01;&#xff01;&#xff01; 基于SpringBoot和Vue的台球赛事服务网站的设计与实现 摘 要 在快速发展的信息时代&#xff0c;体育竞赛作为群众文化娱乐的一部分&#xff0c;已日益受到广泛关注。台球&#xff…

Windows—线程基本知识和线程同步

线程 线程的组成 线程的内核对象&#xff0c;操作系统用它来对线程实施管理。内核对象也是系统用来存放线程统计信息的地方。线程堆栈&#xff0c;它用于维护线程在执行代码时需要的所有函数参数和局部变量 线程的进入点 每个线程必须拥有一个进入点函数&#xff0c;线程从…

备战2024年全国大学生数学建模竞赛:多波束测线问题的解题与优化

目录 一、引言 二、问题分析 三、解题思路与模型建立 问题1&#xff1a;覆盖宽度及重叠率计算 问题2&#xff1a;不同测线方向的覆盖宽度 问题3&#xff1a;最短测线的设计 问题4&#xff1a;基于单波束数据的测线设计 四、知识点解析 五、结果讨论与总结 六、模型的评…

X86架构(六)——硬盘访问与控制

在前面几节中&#xff0c;我们总是通过ROM-BIOS从硬盘的主引导扇区读取一段程序并加载到内存运行&#xff0c;但是处理器是如何访问硬盘呢&#xff1f;这是一个值得我们思考的问题 OK&#xff0c;我们先看一张图 所有这些和计算机主机连接的设备&#xff0c;叫做外围设备,也叫…

240831-Qwen2-VL-7B/2B部署测试

A. 运行效果 B. 配置部署 如果可以执行下面就执行下面&#xff1a; pip install githttps://github.com/huggingface/transformers accelerate否则分开执行 git clone https://github.com/huggingface/transformers cd transformers pip install . accelerate随后&#xff0…

8.27FLEX,BISON

RC ParseStage::handle_request(SQLStageEvent *sql_event) 这个意思是返回类型是RC&#xff0c;然后用到的函数来自 ParseStage&#xff0c;&#xff1a;&#xff1a;就是用来标识作用域的&#xff0c;函数名是handle_request&#xff0c;是ParseStage里的函数 FLEX BISON

vue.js项目实战案例详细源码讲解

​ 大家好&#xff0c;我是程序员小羊&#xff01; 前言&#xff1a; 为帮助大家更好地掌握Vue.js项目的开发流程&#xff0c;我将为你讲解一个完整的Vue.js实战案例&#xff0c;并提供详细的源码解析。这个案例将涵盖从项目创建到实现各种功能模块的全过程&#xff0c;适合用于…

组织培训如何分组?

在组织培训活动时&#xff0c;合理分组是提高效率和参与度的关键。云分组小程序提供了一个简单而有效的解决方案&#xff0c;帮助组织者快速、公平地将参与者分配到不同的小组中。以下是使用云分组小程序进行培训分组的详细步骤&#xff1a;一、创建分组 1. 打开云分组小程序。…

入坑大模型18个月的反思与贩私

前几天开完一个有高层参加的会议&#xff0c;会后组里的技术大佬直接就开喷“要规划没规划&#xff0c;整天只知道对着几个糊弄老板的榜使劲刷”。我下意识地赶紧去拉住他&#xff0c;低声对他讲“你声音太小了&#xff0c;老板听不到的&#xff0c;回头我领你去大厦的保安室&a…

Docker容器技术(下)超多好上手的实验,保姆级教程

文章目录 Docker数据卷管理及优化为什么要使用数据卷bind mount数据卷docker managed数据卷Data Volume Container&#xff08;数据卷容器&#xff09;bind mount数据卷 VS docker managed数据卷备份与迁移数据卷 Docker的安全优化Docker的资源限制限制CPU的使用限制CPU的使用量…

RAG重磅升级:DSF带来特定领域精准提升的全新方案!

检索增强生成&#xff08;Retrieval-Augmented Generation, RAG&#xff09;是一种结合了检索&#xff08;Retrieval&#xff09;和生成&#xff08;Generation&#xff09;能力的框架&#xff0c;通过从背景数据中检索相关信息来增强模型的生成输出。在当前的大型语言模型&…

Linux 安装mysql 数据库通用教程(rpm傻瓜安装)

通用教程&#xff1a;Centos7.9安装mysql8.0.39&#xff08;使用rpm 安装&#xff09; 目录 前言 下载镜像源 删除或查看旧版本 安装mysql 启动mysql mysql授权远程登录 前言 在本篇博客中&#xff0c;我将向您展示如何在CentOS 7.9系统上通过RPM包安装特定版本的MySQL…