单点登陆之OAuth2(SSO/SingleSignOn)

本文详细介绍了使用OAuth2实现单点登录(SSO)的流程,包括创建认证服务器、配置资源服务、登录获取token、处理网关转发时的token问题以及权限管理。通过实例讲解了如何在Spring Cloud中配置OAuth2,并提供了配置代码和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方案一:session共享(使用redis实现,逐渐被淘汰)
方案二:OAuth2(逐渐成为主流),允许用户授权第三方移动应用访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方移动应用或分享他们数据的所有内容
比如:你授权优酷使用你的微信中的头像和昵称,授权的过程不需要提供微信的账号和密码,微信在用户授权后生成access_token返回给优酷保存,
优酷使用access_token访问微信获取头像和昵称
资料网址:https://www.jianshu.com/p/4f5fcddb4106
oauth2 的四种认证方式:https://blog.csdn.net/wqy248/article/details/95061282#_43

spring cloud中的oauth2:
认证服务器:管理access_token(颁发,刷新,验证和查询等)
资源服务器:需要授权后才能访问的服务

单点登录流程图:

在这里插入图片描述

一、创建 auth 服务

在这里插入图片描述

  1. 引入依赖(auth)
<dependencies>

        <!--单个服务的场景启动器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--oauth2的场景启动器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
    </dependencies>
        auth2 依赖有 3 部分组成:
            oauth2:负责 oauth2 流程
            security:负责认证和授权
            jwt:使用 jwt 作为 token
            
            2. 编写 application 配置文件(auth)
spring:
  application:
    name:  auth   #程序名=服务名
server:
  port:   8904    #端口号
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:8761/eureka  #在注册中心注册自己的请求地址
  1. 编写启动类(auth)
package com.laozhang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @author zhangfan
 * @date 2019/10/18
 */
@SpringBootApplication
@EnableEurekaClient
public class AuthApplication {
   

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

  1. 编写 MyWebSecurityConfiguration 配置文件,配置 security(auth)

     1. 为类增加 @EnableWebSecurity 注解
     2. 重写 configure(AuthenticationManagerBuilder auth) 方法
         设置授权验证条件(账号和密码一致)
     
     3. 重写 authenticationManagerBean() 增加 @Bean 注解
         AuthenticationManager 使用上边指定的条件对授权请求进行验证
    
package com.zhiyou100.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author zhangfan
 * @date 2019/10/18
 */
@Configuration
@EnableWebSecurity      //启用配置
public class MyWebSecurityConfigruation extends WebSecurityConfigurerAdapter {
   

    /**
     * 验证表单参数是否正确,账号密码是否一致
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
   
        //spring  security 5 中默认密码必须加密,加密后的密码表示方式为:{加密方式}加密结果
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        //加密后的密码表示方式为:{加密方式}加密结果
        String password =  "{bcrypt}" + bCryptPasswordEncoder.encode("123456");
        //设置两个默认用户
        auth.inMemoryAuthentication().passwordEncoder(passwordEncoder())
                //用户名密码角色
                .withUser("zhangsan").password(password).roles("ADMIN")
                .and()
                .withUser("lisi").password(password).roles("EMPLOYEE");
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
   
        //告诉框架使用哪种加密格式验证密码
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    //返回对象,在MyAuthorizationServerConfiguration,中使用
    @Bean       //把方法返
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值