当前位置:   article > 正文

OAuth2AuthenticationProcessingFilter资源认证服务器过滤器

oauth2authenticationprocessingfilter

OAuth2AuthenticationProcessingFilter资源认证服务器过滤器

资源服务器如何认证访问身份?

一般会传入access_token,那资源认证服务器是如何解析令牌以及如何与资源认证服务器的token库进行对比的?
核心代码在org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter#doFilter

Debug验证:

1、先用密码模式生成一个访问的access_token

img

2、然后用postman进行资源请求:

img

3、进入断点

img

图1

(1):根据tokenExtractor的extract方法获取请求携带的token信息

方法:org.springframework.security.oauth2.provider.authentication.BearerTokenExtractor#extractHeaderToken

img

经过:org.springframework.security.oauth2.provider.authentication.BearerTokenExtractor#extract

将token封装成

PreAuthenticatedAuthenticationToken
  • 1

img

这里可以看到PreAuthenticationAuthenticationToken是Authentication的子类,回到图1的第一步,返回authentication对象

(2)、验证authenticaion

图1的第二步,进入断点

img

从服务端的token存储位置取出OAuth2AccessToken对象,这里tokenStore.readAccessToken(accessTokenValue)方法是核心,点进去看-》

img

就是从accessTokenStore中获取的。

此时权限信息已经通过token取到,继续往下验证

img

checkClientDetails(auth)方法

img

验证当前登录用户的scope属性是否符合所请求的资源所需的权限要求,如果不满足,抛出

Invalid token contains disallowed scope (" + scope + ") for this client

通过验证后,将身份认证信息返回,第二步结束

(3):将身份信息绑定到SecurityContextHolder中 
这步没什么好说的,进入断点就是
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

img

将身份信息设置到上下文的authentication属性中。

至此,token的服务端验证逻辑结束

转载于博客园卡卡罗特

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/84020
推荐阅读
相关标签
  

闽ICP备14008679号