文章目录
- 前言
- 一、关键:请求头 Basic + xxx:xxx
- 二、源码分析
- BasicAuthenticationFilter 类
- extractAndDecodeHeader 方法
- authenticate方法
- loadUserByUsername 方法
- 总结
前言
这段时间在学习 oauth2, 发现我组用的框架,登录请求参数中并没有 client_id ,但是后台逻辑确实关联到了 client 信息,在此记录过程。
一、关键:请求头 Basic + xxx:xxx
请求参数中没有 client_id 相关的信息,但是请求头却有了
二、源码分析
BasicAuthenticationFilter 类
org.springframework.security.web.authentication.www.BasicAuthenticationFilter
入口:
doFilterInternal 方法
1.获取请求头 Authorization 的 basic 信息。
2.如果不为空, 调用 extractAndDecodeHeader 方法, 该方法就是解析得到 tokens=[“webApp”, “webApp”]
3.构建 UsernamePasswordAuthenticationToken 然后调用 authenticationManager.authenticate 方法
extractAndDecodeHeader 方法
没什么复杂,就是base64 解码,构建 String[] return
authenticate方法
1.org.springframework.security.authentication.ProviderManager
此时 authenticate的参数 已经存有从上述带来的 Authorization Basic 的信息 webApp:webApp。
2.org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider
3.org.springframework.security.authentication.dao.DaoAuthenticationProvider
username = webApp
后面就是比较关键的地方 loadUserByUsername
loadUserByUsername 方法
org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService
该方法就是 client 信息和上述 webApp 关联的地方。
从方法名称 loadClientByClientId 和参数名称 就知道 client_id 和 username 现在关联起来了
这里 clientDetailsService 就是在配置类经常见到的东西
后续就不再继续分析,各位老铁自行去研究了。
总结
到此也就明了。
一直困扰我的问题也就渐渐水落石出了。