在使用 Spring Security 的 web 应用程序中总是使用一些关键过滤器,因此我们首先来看看这些关键过滤器及其支持类和接口。我们不会覆盖每一个特性,所以如果你想得到完整的描述,一定要查看 JavaDoc。
在讨论一般访问控制时,我们已经简要地了解了 <bean id="filterSecurityInterceptor" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor"> <property name="authenticationManager" ref="authenticationManager"/> <property name="accessDecisionManager" ref="accessDecisionManager"/> <property name="securityMetadataSource"> <security:filter-security-metadata-source> <security:intercept-url pattern="/secure/super/**" access="ROLE_WE_DONT_HAVE"/> <security:intercept-url pattern="/secure/**" access="ROLE_SUPERVISOR,ROLE_TELLER"/> </security:filter-security-metadata-source> </property> </bean>
可以通过两种方式的配置属性配置
应当注意,
由命名空间语法创建的 <bean id="filterInvocationInterceptor" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor"> <property name="authenticationManager" ref="authenticationManager"/> <property name="accessDecisionManager" ref="accessDecisionManager"/> <property name="runAsManager" ref="runAsManager"/> <property name="securityMetadataSource"> <security:filter-security-metadata-source request-matcher="regex"> <security:intercept-url pattern="\A/secure/super/.*\Z" access="ROLE_WE_DONT_HAVE"/> <security:intercept-url pattern="\A/secure/.*\" access="ROLE_SUPERVISOR,ROLE_TELLER"/> </security:filter-security-metadata-source> </property> </bean>
模式总是按照定义的顺序进行评估。因此,重要的是在列表中定义比在特定的模式中更高的特定模式。这反映在上面的示例中,其中更具体的
<bean id="exceptionTranslationFilter" class="org.springframework.security.web.access.ExceptionTranslationFilter"> <property name="authenticationEntryPoint" ref="authenticationEntryPoint"/> <property name="accessDeniedHandler" ref="accessDeniedHandler"/> </bean> <bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> <property name="loginFormUrl" value="/login.jsp"/> </bean> <bean id="accessDeniedHandler" class="org.springframework.security.web.access.AccessDeniedHandlerImpl"> <property name="errorPage" value="/accessDenied.htm"/> </bean>
如果用户请求一个安全的 HTTP 资源,但他们未被认证,则将调用 如果用户已经被认证并且试图访问受保护的资源,会发生什么?在正常使用中,不应该发生这种情况,因为应用程序工作流程应限于用户能够访问的操作。例如,对管理页面的 HTML 链接可能在没有管理员角色的用户中隐藏。但是,你不能依赖隐藏链接来保证安全性,因为用户总是有可能直接输入 URL 以试图绕过限制。或者他们可以修改一个 RESTful URL 来改变一些参数值。你的应用程序必须保护这些场景,否则肯定是不安全的。通常,你将使用简单的 web 层安全性来对基本 URL 应用约束,并在服务层接口上使用更具体的基于方法的安全性来真正确定允许的内容。
如果抛出
当你使用命名空间来配置应用程序时,也可以提供自定义
在正常情况下,你不需要修改任何此功能,但是保存请求处理是一种尽力而为的方法,并且可能存在默认配置无法处理的情况。这些接口的使用使得它完全可以从 Spring Security 3.0 向前兼容。
我们在技术概览章节中讨论了这个非常重要的过滤器的用途,所以你现在可能需要重新阅读这一节。让我们先看看如何配置它以与 <bean id="securityContextPersistenceFilter" class="org.springframework.security.web.context.SecurityContextPersistenceFilter"/>
正如我们前面看到的,这个过滤器有两个主要任务。它负责在 HTTP 请求之间存储 从 Spring Security 3.0 起,加载和存储安全上下文的任务现在委托给单独的策略接口: public interface SecurityContextRepository { SecurityContext loadContext(HttpRequestResponseHolder requestResponseHolder); void saveContext(SecurityContext context, HttpServletRequest request, HttpServletResponse response); }
默认实现是 <bean id="securityContextPersistenceFilter" class="org.springframework.security.web.context.SecurityContextPersistenceFilter"> <property name='securityContextRepository'> <bean class='org.springframework.security.web.context.HttpSessionSecurityContextRepository'> <property name='allowSessionCreation' value='false' /> </bean> </property> </bean>
或者,可以提供
现在我们已经看到了三个主要的过滤器,它们总是存在于 Spring Security web 配置中。这三者也是由命名空间
登录表单仅包含 <bean id="authenticationFilter" class= "org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> <property name="authenticationManager" ref="authenticationManager"/> </bean>
过滤器调用配置的
如果身份验证成功,则生成的
如果身份验证失败,将调用配置的 [11] 我们使用转发,以便 SecurityContextHolder 仍然包含主体的细节,这对于向用户显示可能是有用的。在老版本的 Spring Security 中,我们依赖于 servlet 容器来处理 403 错误消息,而后者缺少这个有用的上下文信息。
[12] 在 Spring Security 2.0 及更早版本中,这个过滤器被称为
[13] 由于历史原因,在 Spring Security 3.0 之前,这个过滤器被称为 [14] 在 3.0 之前的版本中,此时的应用程序流程已经发展到一个阶段,由这个类和策略插件上的属性混合控制。3.0 的决定是对代码进行重构,使这两种策略完全负起责任。 |