OAuth2AuthenticationProcessingFilter资源认证服务器过滤器

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

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

Debug验证:

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

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

 3、进入断点

                              图1 

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

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

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

将token封装成

PreAuthenticatedAuthenticationToken

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

(2)、验证authenticaion

图1的第二步,进入断点

 

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

 就是从accessTokenStore中获取的。

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

checkClientDetails(auth)方法

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

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

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

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

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

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

原文地址:https://www.cnblogs.com/yibao/p/14223230.html