开发者学堂课程【Spring Security知识精讲与实战演示(一):Spring Security 常用过滤器介绍】学习笔记与课程紧密联系,让用户快速学习知识
课程地址:https://developer.aliyun.com/learning/course/730/detail/13031
Spring Security常用过滤器介绍
Spring Security 常用过滤器介绍
过滤器是一种典型的 AOP 思想,凡是 web工程都能用过滤
过滤器在 Spring Security 中的作用
在我们做完入门案例后,再也无法访问首页。如果要访问,必须先认证。以下提供了两个虚拟的用户(“user”和“admir”)
(1)如果用“user”得到的角色就是“ROLE_USER”正好是上方所需要的角色。
(2)如果用“admir”得到的则是“ROLE_ADMIR”这样的一个角色,但这个角色是不能用的。因为这里没有对“ROLE_ADMIR”实施授权操作,所以它不能访问所有资源。
因此必须用“user”,密码也是“user”
点击登录后即可访问主页
访问完首页之后,以下是对刚才案例中的过滤器的简单讲解。
1.org.springframework.security.web.context.SecurityContextPersistenceFilter(是以下所有过滤器的基础,充当一个容器的概念,否则下面的过滤器将没有存在的空间)
首当其冲的一个过滤器,作用很重要。 SecurityContextPersistenceFilter主要是使用SecurityContextPepository在session中保存或更新一个SecurityContext,并将SecurityContext给以后的过滤器使用,来为后续filter建立所需的上下文,SecurityContext中储存了当前用户的认证以及权限信息。
2. org.springframework.security.web.context.request.async.WebAsyncManagerlntegrationFilter
此过滤器用来集成SecurityContext到Spring导步执行机制中的WebAsyncManager。如果没有WebAsyncManagerlntegrationFilter,SecurityContext与外部容器及Spring就无法整合。
3.org.springframework.security.web.header.HeaderWriterFilter
向请求的Header中添加相应的信息,可在http标签内部使用security:header来控制(动态标签,仅限于GSP页面)
4.org.springframework.security.web.csrf.CsrfFilter
csrf 又称跨域伪造请求,SpringSecurity会对所有post请求验证是否包含系统生成的 csrf 的 token 信息,如果不包含,则报错,起到防止 csrf 攻击的效果。
例如有以下两个网站,用户在登陆第一个网站的时候,在同一个浏览器有打开了另一个网站,但第二个网站有 csrf相关病毒,即跨站伪造请求的病毒,该病毒可以得到当前浏览器中所有信息,其中包含所有登录信息。会造成账号被盗风险。
5.org.springframework.security.web.authentication.LogoutFilter(退出登录使用的)
匹配URL为/logout的请求,实现用户退出,清除认证信息。
6.org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
认证操作全靠这个过滤器,默认匹配URL为/login且必须为POST请求。
7.org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter(Default默认;Login登录)
如果没有在配置文件中指定认证页面,则由该过滤器生成一个默认认证页面。
8.org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter
由此过滤器可以生产一个默认的退出登录页面,也是由该过滤器生成一个默认认证页面。
9.org.springframework.security.web.authentication.www.BasicAuthenticationFilter
此过滤器会自动解析HTTP请求中头部名字为Authentication,且以Basic开头的头信息。(在头信息验证中非常重要)
10.org.springframework.security.web.savedrequest.RequestCacheAwareFilter
通过HttpSessionRequestCache内部维护了一个RequestCache,用于缓存HttpServletRequest
11.org.springframework.security.web.servletapi.SecurityContextHolderwareRequestFilter
针对 ServletRequest 进行了一次包装,使得 request 具有更加丰富的 API
12.org.springframework.security.web.authentication.AnonymousAuthenticationFilter(Anonymous匿名)
当 SecurityContextHolder 中认证信息为空,则会创建一个匿名用户存入到 SecurityContextHolder 中。Spring security 为了兼容未登陆的访问,也走了一套认证流程,只不过是一个匿名的身份。
13.org.springframework.security.web.session.SessionManagementFilter
SecurityContextRepository 限制同一用户开启多个会话的数量
14.org.springframework.security.web.access.ExceptionTranslationFilter
异常转换过滤器位于整个 springSecurityFilterChain 的后方,用来转换整个链路中出现的异常。
15.org.springframework.security.web.access.intercept.FilterSecuritylnterceptor
获取所配置资源访问的授权信息,根据 SecurityContextHolder 中存储的用户信息来决定其是否有权限。决定哪些过滤器能否使用。
以上就是对过滤器的介绍,但是 1.spring security 中并不是只有这么多过滤器,随着 spring-security.xml 配置的添加,还会出现新的过滤器。
2.spring security 中也不是每次都会增加这些过滤器,随着spring-security.xml 配置的修改,有些过滤器可能会被去掉。