Spring Security Oauth2 请求/oauth/

2023-03-15  本文已影响0人  任未然

一. 流程

二. 源码执行流程

2.1 发起获取token请求/oauth/token,必须带上客户端凭证Authorization

客户端凭证值的格式为Basic空格 + client_id:client_secret经过Base64加密后的值, 例如:
Authorization: Basic YWstaGx3eXk6YWtobQQQQ==

image.png

2.2 请求经过拦截器BasicAuthenticationFilter进行拦截授权处理

  1. 请求先经过拦截器org.springframework.security.web.authentication.www.BasicAuthenticationFilter#doFilterInternal, 调用方法org.springframework.security.web.authentication.www.BasicAuthenticationConverter#convert解析客户凭证,获取到client_idclient_secret,然后返回UsernamePasswordAuthenticationToken

    image.png
    image.png
  2. 跟据UsernamePasswordAuthenticationToken找到对应的AuthenticationProvider进行下一步处理

    image.png
  3. 调用方法org.springframework.security.authentication.ProviderManager#authenticate遍历所有的AuthenticationProvider找到对应的Provider处理, UsernamePasswordAuthenticationToken对应的Providerorg.springframework.security.authentication.dao.DaoAuthenticationProvider

image.png
  1. 调用方法org.springframework.security.authentication.dao.DaoAuthenticationProvider#retrieveUser根据client_idUsernamePasswordAuthenticationToken获取UserDetails

    image.png
    image.png
  2. UserDetails是调用方法org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService#loadUserByUsername获取的

    image.png
  3. 调用方法org.springframework.security.authentication.dao.DaoAuthenticationProvider#additionalAuthenticationChecks根据UserDetailsAuthentication校验客户凭证的client_secret是否正确

    image.png

以上步骤全部成功,就会执行到下步骤

2.3 正式执行/oauth/token的业务方法

org.springframework.security.oauth2.provider.endpoint.TokenEndpoint#postAccessToken

image.png
  1. 调用方法org.springframework.security.oauth2.provider.OAuth2RequestValidator#validateScope(TokenRequest, ClientDetails)验证scope

    image.png
  2. 调用org.springframework.security.oauth2.provider.CompositeTokenGranter#grant根据grantType遍历所有的TokenGranter, 找到对应的TokenGranter进行处理

    image.png
  3. 调用org.springframework.security.oauth2.provider.token.DefaultTokenServices#createAccessToken(org.springframework.security.oauth2.provider.OAuth2Authentication)生成token信息

image.png
image.png
上一篇 下一篇

猜你喜欢

热点阅读