1、SpringSecurity过滤器链总概
如下图
2、自我理解
此处理解皆为自己的观点,若有错误,还请路过的大佬批评与指正:
2.1、http请求在到达RESTful服务器之前,会经过SpringSecurity的一系列过滤器(当然项目要使用该鉴权框架)鉴权认证,该请求是否有资格访问到服务器。
2.2、过滤器链中最后一个就是来判断该请求是否有资格访问到服务的,其内部维护着投票器来决定的。
以下通过伪代码说明:
FilterSecurityInterceptor.doFilter->
invoke->
AbstractSecurityInterceptor.beforeInvocation->
DefaultFilterInvocationSecurityMetadataSource.getAttributes->
accessDecisionManager(spring默认采用的实现类AffirmativeBased).decide(投票器不通过抛出AccessDeniedException异常给AbstractSecurityInterceptor)->
if (鉴权不通过) {
AbstractSecurityIntercepto捕获AccessDeniedException异常并调用AbstractSecurityInterceptor.publishEvent发布事件,且向上抛出该异常给ExceptionTranslationFilter->
throwableAnalyzer.getFirstThrowableOfType(检查此异常的根是否为AuthenticationException\AccessDeniedException->
ExceptionTranslationFilter.handleSpringSecurityException(处理异常))->
if (authenticationTrustResolver.isAnonymous(authentication)) {
sendStartAuthentication
} else {
(accessDeniedHandler.handle(request, response,(AccessDeniedException) exception))
}
} else {
鉴权通过直接访问服务
}
2.3、第一个过滤器SecurityContextPersistenceFilter
作用一:
当请求进来时,检查session中是否有SecurityContext,若有,将其取出放入线程(ThreadLocal)中,没有进入下一个过滤器
作用二:
当请求结束返回时,检查线程(ThreadLocal)中是否有SecirityContext,如果有,将其取出放入session中
2.4、在概括图中绿色的过滤器皆为进行身份认证的过滤器。