spring security经常在项目中用到,但是平常只是简单的使用肯定是不够的,我们需要了解深层次的东西,才能在使用的过程中不畏惧,本次打算从demo入手,跟踪源码,剖析security内在
本文在上一篇的基础上进行,继续追踪看下不同类型模式的grant方法到底做了什么
一、ResourceOwnerPasswordTokenGranter
1、从继承关系可以看到,父类AbstractTokenGranter
2、在getOAuth2Authentication方法中进入子类自己的方法
注意,此时认证对象包含了userAuth对象
二、ClientCredentialsTokenGranter
1、从继承关系可以看到,父类AbstractTokenGranter
重写了grant方法
2、没有重写getOAuth2Authentication
进入父类getOAuth2Authentication方法
三、携带token请求资源流程
1、OAuth2AuthenticationProcessingFilter
资源服务配置时创建的filter
此时的认证管理器为OAuth2AuthenticationManager
四、参数解析器AuthenticationPrincipalArgumentResolver简单分析
1、实现接口HandlerMethodArgumentResolver
参数解析时会调用 authentication.getPrincipal();
authentication为之前创建token时创建的oauth2Authentication
由上面分析可知,当方法参数中有@AuthenticationPrincipal注解时,会调用getPrincipal方法获取,所以当客户端模式获取的token请求资源时,principal为clientId
2、此参数解析器是在@EnableWebSecurity注解的作用下加入的