SpringSecurity:授权服务器与客户端应用(入门案例)

news/2025/2/9 5:42:40 标签: oauth2, springsecurity

文章目录

  • 一、需求概述
  • 二、开发授权服务器
    • 1、pom依赖
    • 2、yml配置
    • 3、启动服务端
  • 三、开发客户端应用
    • 1、pom依赖
    • 2、yml配置
    • 3、SecurityConfig
    • 4、接口
    • 5、测试

一、需求概述

在这里插入图片描述在这里插入图片描述在这里插入图片描述
maven需要3.6.0以上版本

二、开发授权服务器

1、pom依赖

在这里插入图片描述

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-authorization-server</artifactId>
        </dependency>

2、yml配置

server:
  port: 9000

logging:
  level:
    org.springframework.security: trace

spring:
  security:
    # 授权服务器的账号密码
    user:
      name: admin
      password: 1111
    oauth2:
      authorizationserver:
        # 客户端配置
        client:
          myclient:
            registration:
              client-id: pzj
              client-secret: "{noop}123456"
              client-authentication-methods:
                - "client_secret_basic"
              authorization-grant-types:
                - "authorization_code"
                - "refresh_token"
              # 客户端的回调地址
              redirect-uris:
                - "http://localhost:8080/login/oauth2/code/myclient"
              # 客户端的应用首页
              post-logout-redirect-uris:
                - "http://localhost:8080/"
              scopes:
                - "openid"
                - "profile"
            require-authorization-consent: true

3、启动服务端

三、开发客户端应用

1、pom依赖

        <!-- spring security 安全认证 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!-- oauth2 客户端 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>

2、yml配置

# Spring配置
spring:
  security:
    oauth2:
      client:
        registration:
          myclient:
            provider: sas
            client-id: pzj
            client-secret: 123456
            # 重定向的url地址,这个地址为默认的
            redirect-uri: http://localhost:8080/login/oauth2/code/myclient
            authorization-grant-type: "authorization_code"
            scope:
              - openid
              - profile
        provider:
          sas:
            # 以下地址是默认配置在 AuthorizationServerSettings builder方法中
            # 授权服务器地址
            authorization-uri: http://oauth2-server:9000/oauth2/authorize
            # 获取token的地址
            token-uri: http://oauth2-server:9000/oauth2/token
            # 用于验证JWT签名的、oauth2授权服务器的公钥集合
            jwk-set-uri: http://oauth2-server:9000/oauth2/jwks

3、SecurityConfig

@EnableWebSecurity
@Configuration
public class SecurityConfig {

	@Bean
	protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception
	{
		http.authorizeHttpRequests(auth -> auth.anyRequest().authenticated());
		http.oauth2Login(Customizer.withDefaults());
		return http.build();
	}
}

4、接口

@RestController
public class HelloController {

	@GetMapping("/hello")
	public String hello(){
		return "<h1>hello,spring authorization server!</h1>";
	}
}

5、测试

浏览器访问:http://localhost:8080/hello
会跳转到
在这里插入图片描述
点击浏览器的×,进入登陆页
在这里插入图片描述
登陆后,进入授权确认页面
在这里插入图片描述
点击submit按钮,就会访问到我们的hello接口
在这里插入图片描述

工程名:authorization-project


http://www.niftyadmin.cn/n/5845600.html

相关文章

leetcode_深度遍历和广度遍历 100. 相同的树

100. 相同的树 给你两棵二叉树的根节点 p 和 q &#xff0c;编写一个函数来检验这两棵树是否相同。 如果两棵树在结构上相同&#xff0c;并且节点具有相同的值&#xff0c;则认为它们是相同的。 思路: (递归法) 返回True的情况: 两棵树都为空两棵树相同 返回False的情况: 两棵…

知识图谱智能应用系统:数据存储架构与流程解析

在当今数字化时代,知识图谱作为一种强大的知识表示和管理工具,正逐渐成为企业、科研机构以及各类智能应用的核心技术。知识图谱通过将数据转化为结构化的知识网络,不仅能够高效地存储和管理海量信息,还能通过复杂的查询和推理,为用户提供深度的知识洞察。然而,构建一个高…

RK3568上使用C++结合V4L2拉流,并RKMPP硬件编解码,并保存为MP4文件

在RK3568平台上使用C结合V4L2捕获视频流&#xff0c;并通过RKMPP进行硬件编码后保存为MP4文件&#xff0c;可以按照以下步骤实现&#xff1a; 1. 环境准备 硬件&#xff1a;RK3568开发板、摄像头模块。软件依赖&#xff1a; Linux内核支持V4L2。Rockchip MPP库&#xff08;RKM…

【RocketMQ 存储】- 同步刷盘和异步刷盘

文章目录 1. 前言2. 概述3. submitFlushRequest 提交刷盘请求4. FlushDiskWatcher 同步刷盘监视器5. 同步刷盘但是不需要等待刷盘结果6. 小结 本文章基于 RocketMQ 4.9.3 1. 前言 RocketMQ 存储部分系列文章&#xff1a; 【RocketMQ 存储】- RocketMQ存储类 MappedFile【Rock…

React 生命周期函数详解

React 组件在其生命周期中有多个阶段&#xff0c;每个阶段都有特定的生命周期函数&#xff08;Lifecycle Methods&#xff09;。这些函数允许你在组件的不同阶段执行特定的操作。以下是 React 组件生命周期的主要阶段及其对应的生命周期函数&#xff0c;并结合了 React 16.3 的…

【ROS2】【2025】Simulate a 6DoF Robotic Arm in Gazebo and ROS2

在本教程中&#xff0c;将学习如何从头开始模拟机械臂。我们将使用 Doosan Robotics 的 6DoF 机械臂。Gazebo 和 ROS2 是执行此模拟的软件。所有代码、URDF 和配置文件都可以在我的 gitee 存储库中找到和下载。 https://gitee.com/kong-yue1/robotic_arm_environment.githttps…

FreeRTOS的事件组

1 创建事件组 xEventGroupCreate EventGroupHandle_t xEventGroupCreate( void ) { EventGroup_t *pxEventBits;/* 分配事件组内存。*/pxEventBits ( EventGroup_t * ) pvPortMalloc( sizeof( EventGroup_t ) );if( pxEventBits ! NULL ){pxEventBits->uxEventBits 0; …

uniapp中使用uCharts折线图X轴数据间隔显示

1、先看官网 https://www.ucharts.cn/ 2、设置代码 "xAxisDemo3":function(val, index, opts){if(index % 2 0){return val}else {return }}, 再在数据中引入设置好样式