16. 保护流
安全性对于任何应用程序来说都是一个重要的概念。 最终用户不应仅通过猜测 URL 来访问网站的任何部分。 站点的敏感区域必须确保仅处理授权请求。 Spring Security 是一个经过验证的安全平台,可以在多个级别与您的应用程序集成。 本节重点介绍如何保护流执行。
16.1. 如何保护流?
保护流的过程分为三个步骤:
-
使用身份验证和授权规则配置 Spring Security。
-
使用安全元素对流定义进行批注以定义安全规则。
-
添加
SecurityFlowExecutionListener
以处理安全规则。
必须完成这些步骤中的每一个,否则不会应用流安全规则。
16.2. 使用secured
元素
这secured
元素指定其包含元素应在完全进入之前应用授权检查。
在受保护的流执行的每个阶段,这种情况可能不会多次发生。
可以保护流的三个阶段:流、状态和转换。
在每种情况下,secured
元素相同。
这secured
元素位于它保护的元素内。
例如,为了保护状态,secured
元素直接发生在该状态内,如下所示:
<view-state id="secured-view">
<secured attributes="ROLE_USER" />
...
</view-state>
16.3. 使用SecurityFlowExecutionListener
在流中定义安全规则本身并不能保护流。
您还必须定义SecurityFlowExecutionListener
在 Webflow 配置中,并将其应用于 Flow Executor,如下所示:
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
<webflow:flow-execution-listeners>
<webflow:listener ref="securityFlowExecutionListener" />
</webflow:flow-execution-listeners>
</webflow:flow-executor>
<bean id="securityFlowExecutionListener"
class="org.springframework.webflow.security.SecurityFlowExecutionListener" />
如果拒绝访问应用程序的某个部分,则AccessDeniedException
被抛出。
此异常稍后由 Spring Security 捕获,并用于提示用户进行身份验证。
允许此异常不受抑制地在执行堆栈中向上移动,这一点很重要。
否则,系统可能不会提示最终用户进行身份验证。
16.3.1. 自定义授权管理器
如果您的应用程序使用不基于角色的权限,则需要配置自定义AuthorizaitonManager
.
您可以覆盖AuthorityAuthorizationManager
默认通过
这authorizationManagerInitializer
属性。例如:
@Bean
SecurityFlowExecutionListener securityFlowExecutionListener() {
SecurityFlowExecutionListener listener = new SecurityFlowExecutionListener();
listener.setAuthorizationManagerInitializer(securityRule -> {
// ...
});
return listener;
}
16.3.2. 自定义访问决策管理器
Spring Security 的AccessDecisionManager
已弃用,并将在未来版本中删除。
因此,建议配置AuthorizationManager
相反。
但是,如果您必须使用AccessDecisionManager
中,您可以设置accessDecisionManager
security 侦听器的属性,
或覆盖createAccessDecisionManager(SecurityRule)
protected 方法。
要了解有关 Spring Security 的AuthorizationManager
API,请参阅 Spring Security 参考文档。
16.4. 配置 Spring Security
Spring Security 具有强大的配置选项。 由于每个应用程序和环境都有自己的安全要求,因此 Spring Security 参考文档是了解可用选项的最佳场所。
这booking-faces
和booking-mvc
示例应用程序配置为使用 Spring Security。
Spring 和web.xml
水平。
16.4.1. Spring 配置
Spring 配置定义了http
具体信息(例如受保护的 URL 和登录/注销机制)和authentication-provider
.
对于示例应用程序,配置了本地身份验证提供程序。
以下示例为 Web 流配置 Spring Security:
<security:http auto-config="true">
<security:form-login login-page="/spring/login"
login-processing-url="/spring/loginProcess"
default-target-url="/spring/main"
authentication-failure-url="/spring/login?login_error=1" />
<security:logout logout-url="/spring/logout" logout-success-url="/spring/logout-success" />
</security:http>
<security:authentication-provider>
<security:password-encoder hash="md5" />
<security:user-service>
<security:user name="keith" password="417c7382b16c395bc25b5da1398cf076"
authorities="ROLE_USER,ROLE_SUPERVISOR" />
<security:user name="erwin" password="12430911a8af075c6f41c6976af22b09"
authorities="ROLE_USER,ROLE_SUPERVISOR" />
<security:user name="jeremy" password="57c6cbff0d421449be820763f03139eb"
authorities="ROLE_USER" />
<security:user name="scott" password="942f2339bf50796de535a384f0d1af3e"
authorities="ROLE_USER" />
</security:user-service>
</security:authentication-provider>
16.4.2.web.xml
配置
在web.xml
文件、filter
被定义为拦截所有请求。
此筛选器侦听登录和注销请求并相应地处理它们。
它还捕获AccesDeniedException
实例,并将用户重定向到登录页面。
以下示例定义了此类筛选器:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>