springboot 集成Oauth2 实现单点登录 原理

四种角色

  客户端应用、Resource Owner、Authorization Server、Resource Server

四种授权模式

  • 授权码模式
  • 密码模式
  • 简化模式
  • 客户端模式

协议流程

下面是授权码模式的官方文档中的流程截图

在这里插入图片描述
详细步骤如下:
(A) The client requests authorization from the resource owner. The
authorization request can be made directly to the resource owner
(as shown), or preferably indirectly via the authorization
server as an intermediary.
(B) The client receives an authorization grant, which is a
credential representing the resource owner’s authorization,
expressed using one of four grant types defined in this
specification or using an extension grant type. The
authorization grant type depends on the method used by the
client to request authorization and the types supported by the
authorization server.
© The client requests an access token by authenticating with the
authorization server and presenting the authorization grant.
(D) The authorization server authenticates the client and validates
the authorization grant, and if valid, issues an access token.
(E) The client requests the protected resource from the resource
server and authenticates by presenting the access token.
(F) The resource server validates the access token, and if valid,
serves the request.

大概翻译一下 :
你相当于 resource owner 第三方应用相当于 client 应用 手机相册相当于 Resource Server
比如一个你在一个第三方应用上操作,想要访问资源服务器的资源,然后authorization server会申请 Resource owner(也就是操作人)的意见是否允许访问, 在你点击允许后 会从授权中心拿到一个授权码,并返回给客户端应用,然后客户端应用再那这这个授权码去授权服务器申请token,
授权服务器办法token给客户端应用,然后客户端应用拿着token访问resource server的资源, resource server 会请求authorization server 去验证token是否有效,有效的话 允许访问资源.。

相关问题

下文中的小demo是基于springboot + oauth2实现的 sso方案,核心注解是@EnableOauthSso,本文给出源码流程中的核心类和方法,用于梳理整个请求跳转过程。文末给出项目gitee地址。

开始前带着几个小问题

  • @EnableOauthSso 注解,在访问客户端应用方法的时候 是如何重定向到授权服务的?
    这个在下面,有点长

  • 授权服务器拦截到客户端应用转发的 /oauth/authorize?refirect_uri=client_login_uri请求后 是如何再次转发请求到客户端应用的
    授权服务器配置了/oauth/authorize方法会被拦截, 在ExceptionTranslationFilter这个过滤器中 会捕捉到下个过滤器中尝试获取授权的过滤器中抛出的异常,
    在这里插入图片描述
    在这里插入图片描述
    然后来到这个方法,层层查看 ,最终来到 LoginUrlAuthenticationEntryPoint这个类的commence方法

  • 转发到客户端应用后 ,又做了什么处理 最终转发到 授权服务器的 /oauth/token方法
    OAuth2ClientAuthenticationProcessingFilter -》attemptAuthentication()这里面会去调用授权服务器的生成token的方法 /oauth/token方法

  • 跳转到登录页后,点击登录 为什么可以跳转到登陆之前要访问的资源

    SavedRequestAwareAuthenticationSuccessHandler 登陆成功过之后会从onAuthenticationSuccess()方法中取出之前真正要访问的接口

  • 访问其他客户端应用的时候 为什么可以不需要再次登录 就可以访问
    这个暂时还不是很清楚

问题解答

1、资源服务器和授权服务器的配置没有什么太多值得说的,分别添加 @EnableResourceServer 和 @EnableAuthorizationServer就好,然后资源服务器配置下 RemoteTokenService 授权服务器配置下 授权的相关配置
关键是客户端应用那里 添加了@EnableOauth2Sso注解 其他的配置都没了,让人很费解,中间到底发生了什么,为什么能够在未登录的情况下 跳转到 登录中心,已登录的情况下 访问其他的应用为什么能够直接访问 不用再次登录, 注意各个客户端应用的配置一致。
点开@EnableOauth2Sso注解进去 可以看到
在这里插入图片描述

会导入OAuth2SsoDefaultConfiguration、OAuth2SsoCustomConfiguration、ResourceServerTokenServicesConfiguration这三个配置类,分别点进去看前两个类的注释
在这里插入图片描述
在这里插入图片描述
在客户端应用未配置WebSecurityConfigurerAdapter的时候 ,因为导入了OAuth2SsoDefaultConfiguration,会提供一个默认的SsoSecurityConfigurer的配置类,拦截所有的请求
在这里插入图片描述
重点是上面那两行代码,大致意思是添加了oauth2sso的过滤器,并且设置了authorization的 端点,其实也就是 统一认证的登录页地址。重点分析下 那个oauth2的过滤器链方法oauth2SsoFilter()。
进去看该方法,找到关键的过滤器类

在这里插入图片描述
该类继承AbstractAuthenticationProcessingFilter,看该类的doFIlter方法 会去获取 token ,由于没有登录过,会报错
在这里插入图片描述
在Oauth2ClientContextFilter中会进行重定向到 授权服务器的 /oauth/authorize方法 ,这个方法由于在授权服务器 里做了登录拦截,会被重定向到授权服务器的登录方法
这个重定向的uri 是如下格式 http://oauthserver/oauth/authorize?redirect_uri= client_login_uri&state=xxx&type=code

代码地址

sso-oauth2-master

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值