spring security oauth2整合SSO(单点登录)

 1.流程

  1. 用户在访问应用程序时,将被重定向到身份认证服务器进行身份验证。
  2. 用户输入他们的凭据(通常是用户名和密码),身份认证服务器对其进行验证。
  3. 身份认证服务器向用户颁发一个令牌,该令牌表示用户已经通过身份验证。
  4. 当用户尝试访问其他应用程序时,该应用程序将重定向到身份认证服务器。
  5. ( 身份认证服务器检查它以前颁发的令牌是否有效,并且如果有效,则会生成一个新的令牌,该令牌针对该特定的应用程序进行授权 )。
  6. 应用程序使用授权令牌访问受保护的资源,并向身份认证服务器验证令牌是否有效。
  7. 用户可以在多个应用程序中使用同一个令牌,而无需再次进行身份验证。

(55条消息) spring security oauth2学习 -- 快速入门_本郡主是喵的博客-CSDN博客

(55条消息) spring-security -oauth2 整合 JWT_本郡主是喵的博客-CSDN博客

以上述博客代码为基础,以一个客户端和授权服务器模拟单点登录。

下面Demo,没有完全演示出oauth2协议的流程,但是配置出来,可以自行尝试。

客户端包结构

新建一个项目或者模块。

 导入依赖

	<properties>
		<spring-boot.version>2.6.13</spring-boot.version>
		<spring-cloud.version>2021.0.7</spring-cloud.version>
	</properties>

    <dependencies>
		<!--springboot -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<!--test组件-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<!--oauth2-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-oauth2</artifactId>
			<version>2.2.2.RELEASE</version>
		</dependency>
		<!--security-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-security</artifactId>
			<version>2.2.2.RELEASE</version>
		</dependency>
		<!--jjwt-->
		<dependency>
			<groupId>io.jsonwebtoken</groupId>
			<artifactId>jjwt</artifactId>
			<version>0.9.1</version>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>${spring-boot.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

yml配置文件

application.yml

server:
  port: 8081
#  防止cookie冲突.冲突会导致登录验证不通过
  servlet:
    session:
      cookie:
        name: OAUTH2-CLIENT-SESSIONID01

# 授权服务器地址
oauth2-server-url: http://localhost:8888
# 与授权服务器相应的配置
security:
  oauth2:
    client:
      client-id: admin
      client-secret: 112233
      user-authorization-uri: ${oauth2-server-url}/oauth/authorize
      access-token-uri: ${oauth2-server-url}/oauth/token

    resource:
      jwt:
        key-uri: ${oauth2-server-url}/oauth/token_key

启动类

@EnableOAuth2Sso  // 开启单点登录
public class Ouath2ClientDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(Ouath2ClientDemoApplication.class, args);
    }

}

UserController.java

还是用这个来模拟资源。

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/getCurrentUser")
    public Object getCurrentUser(Authentication authentication, HttpServletRequest request){


        Object principal = authentication.getPrincipal();
      return principal;
    }
}

 AuthenticationServer.java

这里面将客户端重定向的地址,改成客户端的登录地址(之前不是)。

  public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                // id 和 secret 也是oauth处理的
                .withClient("admin") // 配置 client - id
                .secret(passwordEncoder.encode("112233")) // 配置client- secret 简单理解秘钥
                .accessTokenValiditySeconds(3600)// 配置access-token有效时间
                .refreshTokenValiditySeconds(86400) // 配置refresh_token有效期
                .autoApprove(true)// 自动授权(省略我们登录之后,不是拿到授权码?有一个定向页面)
                .redirectUris("http://localhost:8081/login")// 配置配置拿到令牌后重定向的网址(客户端)
                .scopes("all")// 配置申请的权限范围
                .authorizedGrantTypes("password","refresh_token","authorization_code"); // 配置授权类型
        // authorization_code
    }

结果

1.访问 http://localhost:8081/user/getCurrentUser

2.自动重定向到http://localhost:8888/login页面 页面,登录自动授权之后,重定向到我们的http://localhost:8081/login

3.但由于我们相当于登录,因此放行到我们之前访问localhost:8888/user/getCurrentUser

3.总结

授权服务器和资源服务器在一个服务器上,客户端与授权服务交互,拿到授权码,重定向到客户端。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

本郡主是喵

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

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

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

打赏作者

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

抵扣说明:

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

余额充值