# 一、/oauth/token
password模式获取token源码主要操作
1、定义请求参数
![在这里插入图片描述](https://img-blog.csdnimg.cn/25ff96f3239f49c093510013f904c943.png)
增加请求头 Authorization 否则 在请求参数使用 client_id (注意! 此头为base64拼接格式为 client_id:client_secret)
![在这里插入图片描述](https://img-blog.csdnimg.cn/50ba7e5dc9d947fdb6ed4ba23a7b1fe5.png)
2、下下为重点执行代码处
若定义Authorization头则进入
1、org.springframework.security.web.authentication.www.BasicAuthenticationFilter#doFilterInternal
![在这里插入图片描述](https://img-blog.csdnimg.cn/071e38c67eef4a24ae6ad57ada499a9c.png)
使用client_id,client_secret 请求param 则进入
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter#doFilter
![在这里插入图片描述](https://img-blog.csdnimg.cn/7bc72567a82f4ac5870a12068488dc1f.png)
2、org.springframework.security.web.access.intercept.FilterSecurityInterceptor#invoke
![在这里插入图片描述](https://img-blog.csdnimg.cn/bb16d91efb5b4954a12b76327b32b4c1.png)
3、org.springframework.web.method.support.InvocableHandlerMethod#invokeForRequest
![在这里插入图片描述](https://img-blog.csdnimg.cn/caeeb3adf9fb4cc7aa429bcc79b7d5c4.png)
4、org.springframework.security.oauth2.provider.endpoint.TokenEndpoint#postAccessToken
![在这里插入图片描述](https://img-blog.csdnimg.cn/761337d39f9644fba3d3cef9c4b128b6.png)
5、org.springframework.security.oauth2.provider.token.AbstractTokenGranter#getAccessToken
![在这里插入图片描述](https://img-blog.csdnimg.cn/418ab5db75074eee846c21c5dcc46acc.png)
6、org.springframework.security.oauth2.provider.password.ResourceOwnerPasswordTokenGranter#getOAuth2Authentication
![在这里插入图片描述](https://img-blog.csdnimg.cn/5dd1b5893c2345ca852dd9614611e8ee.png)
org.springframework.security.authentication.ProviderManager#authenticate
![在这里插入图片描述](https://img-blog.csdnimg.cn/2886aa2b81fc470cb1965d721ab7a2bd.png)
此处选择 provider方式 根据 传入的Authentication而决定 可自定义 实现 AbstractAuthenticationToken(此抽象类实现Authentication copy password模式Ganter 切记自定义token构造器加上 super.setAuthenticated(true);)
![在这里插入图片描述](https://img-blog.csdnimg.cn/0427780164e84df99690b53fbab11b28.png)
7、org.springframework.security.oauth2.provider.token.DefaultTokenServices#createAccessToken(org.springframework.security.oauth2.provider.OAuth2Authentication, org.springframework.security.oauth2.common.OAuth2RefreshToken)
![在这里插入图片描述](https://img-blog.csdnimg.cn/a7731711b8eb458a9c2962ac00c5a3e2.png)
有增强 TokenEnhancer 就进入增强器
自此最后返回org.springframework.security.oauth2.provider.endpoint.TokenEndpoint#getResponse
.oauth2.provider.endpoint.TokenEndpoint#getResponse
![在这里插入图片描述](https://img-blog.csdnimg.cn/5967c68572af4b1d97d25b5eb46b77a3.png)
最后补充AuthorizationServerTokenServices,SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> ,AuthenticationProvider,AbstractTokenGranter,AbstractAuthenticationProcessingFilter 还有好多就不一一列举了都可以自己扩展实现自己的逻辑加油!!!