# 15. Spring Framework
# 15. Spring Framework
本頁涵蓋了將 Shiro 集成到基于 [Spring](http://www.springframework.org/) 的應用程序的方法。
Shiro 的 JavaBean 兼容性使得它非常適合通過 Spring XML 或其他基于 Spring 的配置機制。 Shiro 應用程序需要一個具有單例SecurityManager 實例的應用程序。請注意,這不會是一個靜態的單例,但應該只有一個應用程序能夠使用的實例,無論它是否是靜態單例的。
## Standalone Applications
這里是在 Spring 應用程序中啟用應用程序單例 SecurityManager 的最簡單的方法:
```
<!-- 定義連接后臺安全數據源的realm -->
<bean id="myRealm" class="...">
...
</bean>
<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">
<!-- 單realm應用。如果有多個realm,使用‘realms’屬性代替 -->
<property name="realm" ref="myRealm"/>
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 最簡單的集成,是將securityManager bean配置成一個靜態單例,也就是讓 SecurityUtils.*
下的所有方法在任何情況下都起作用。在web應用中不要將securityManager bean配置為靜態單例,
具體方式請參閱下文中的‘Web Application’部分 -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
<property name="arguments" ref="securityManager"/>
</bean>
```
## Web Applications
Shiro 擁有對 Spring Web 應用程序的一流支持。在 Web 應用程序中,所有 Shiro 可訪問的 web 請求必須通過一個主要的 Shiro 過濾器。該過濾器本身是極為強大的,允許臨時的自定義過濾器鏈基于任何 URL 路徑表達式執行。
在Shiro 1.0 之前,你不得不在 Spring web 應用程序中使用一個混合的方式,來定義 Shiro 過濾器及所有它在 web.xml 中的配置屬性,但在Spring XML 中定義 SecurityManager。這有些令人沮喪,由于你不能把你的配置固定在一個地方,以及利用更為先進的 Spring 功能的配置能力,如 PropertyPlaceholderConfigurer 或抽象 bean 來固定通用配置。
現在在 Shiro 1.0 及以后版本中,所有 Shiro 配置都是在 Spring XML 中完成的,用來提供更為強健的 Spring 配置機制。
以下是如何在基于 Spring web 應用程序中配置 Shiro:
### web.xml
除了其他 Spring web.xml 中的元素( ContextLoaderListener,Log4jConfigListener 等等),定義下面的過濾器及過濾器映射:
```
<!-- filter-name對應applicationContext.xml中定義的名字為“shiroFilter”的bean -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
...
<!-- 使用“/*”匹配所有請求,保證所有的可控請求都經過Shiro的過濾。通常這個filter-mapping
放置到最前面(其他filter-mapping前面),保證它是過濾器鏈中第一個起作用的 -->
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
```
### applicationContext.xml
在你的 applicationContext.xml 文件中,定義 web 支持的SecurityManager 和 'shiroFilter' bean 將會被 web.xml 引用。
```
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!-- 可根據項目的URL進行替換 -->
<property name="loginUrl" value="/login.jsp"/>
<property name="successUrl" value="/home.jsp"/>
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<!-- 因為每個已經定義的javax.servlet.Filter類型的bean都可以在鏈的定義中通過bean名
稱獲取,所以filters屬性不是必須出現的。但是可以根據需要通過filters屬性替換filter
實例或者為filter起別名 -->
<property name="filters">
<util:map>
<entry key="anAlias" value-ref="someFilter"/>
</util:map>
</property>
<property name="filterChainDefinitions">
<value>
# some example chain definitions:
/admin/** = authc, roles[admin]
/docs/** = authc, perms[document:read]
/** = authc
# more URL-to-FilterChain definitions here
</value>
</property>
</bean>
<!-- 定義應用上下文的 javax.servlet.Filter beans。這些beans 會被上面定義的shiroFilter自
動感知,并提供給“filterChainDefinitions”屬性使用。或者也可根據需要手動的將他們添加在
shiroFilter bean的“filters”屬性下的Map標簽中。 -->
<bean id="someFilter" class="..."/>
<bean id="anotherFilter" class="..."> ... </bean>
...
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 單realm應用。如果需要配置多個realm,使用“realms”屬性 -->
<property name="realm" ref="myRealm"/>
<!-- 默認使用servlet容器session。下面是使用shiro 原生session的例子(細節請參考幫助文檔)-->
<!-- <property name="sessionMode" value="native"/> -->
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 定義連接后臺安全數據源的realm -->
<bean id="myRealm" class="...">
...
</bean>
```
## Enabling Shiro Annotations 啟用注解
不論獨立的應用程序還是 web 應用程序,都可以使用 Shiro 提供的注解進行安全檢查。比如 @RequiresRoles, @RequiresPermissions 等。這些注解需要借助 Spring 的 AOP掃描使用 Shiro 注解的類,并在必要時進行安全邏輯驗證。
下面讓我們看下如何開啟注解,其實很簡單,只要在applicationContext.xml 中定義兩個 bean 即可。
```
<!-- 開啟Shiro注解的Spring配置方式的beans。在lifecycleBeanPostProcessor之后運行 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
```
## Secure Spring Remoting 遠程安全
Shiro 的 Spring 遠程支持有兩部分組成:遠程調用的客戶端配置和接收、處理遠程調用的服務器端配置。
### Server-side Configuration 服務端配置
當 Shiro 的 Server 端接收到一個遠程方法調用時,與遠程調用相關的Subject 必須在接收線程執行時綁定到接收線程上,這項工作通過在applicationContext.xml 中定義 SecureRemoteInvocationExecutor bean 完成。
```
<!-- Spring遠程安全確保每個遠程方法調用都與一個負責安全驗證的Subject綁定 -->
<!-- can be associated with a Subject for security checks. -->
<bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
<property name="securityManager" ref="securityManager"/>
</bean>
```
SecureRemoteInvocationExecutor 定義完成后,需要將它加入到Exporter 中,這個 Exporter 用于暴露向外提供的服務,而且 Exporter 的實現類由具體使用的遠程處理機制和協議決定。定義 Exporter beans 請參照 Spring 的 [Remoting](http://static.springsource.org/spring/docs/2.5.x/reference/remoting.html) 章節。
以基于 HTTP 的遠程 SecureRemoteInvocationExecutor 為例。( remoteInvocationExecutor 屬性引用自secureRemoteInvocationExecutor)
```
<bean name="/someService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="someService"/>
<property name="serviceInterface" value="com.pkg.service.SomeService"/>
<property name="remoteInvocationExecutor" ref="secureRemoteInvocationExecutor"/>
</bean>
```
### Client-side Configuration 客戶端配置
當遠程調用發生時,負責鑒別信息的Subject需要告知server遠程方法是誰發起的 。如果客戶端是基于Spring的,那么這種關聯可以通過Shiro的SecureRemoteInvocationFactory 完成。
```
<bean id="secureRemoteInvocationFactory" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationFactory"/>
```
然后將SecureRemoteInvocationFactory 添加到與協議相關的Spring遠程ProxyFactoryBean 中。
以基于HTTP協議的遠程ProxyFactoryBean為例。(remoteInvocationExecutor 屬性引用自secureRemoteInvocationExecutor)
```
<bean id="someService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://host:port/remoting/someService"/>
<property name="serviceInterface" value="com.pkg.service.SomeService"/>
<property name="remoteInvocationFactory" ref="secureRemoteInvocationFactory"/>
</bean>
```
## 為文檔加把手
我們希望這篇文檔可以幫助你使用 Apache Shiro 進行工作,社區一直在不斷地完善和擴展文檔,如果你希望幫助 Shiro 項目,請在你認為需要的地方考慮更正、擴展或添加文檔,你提供的任何點滴幫助都將擴充社區并且提升 Shiro。
提供你的文檔的最簡單的途徑是將它發送到用戶[論壇](http://shiro-user.582556.n2.nabble.com/)或[郵件列表](http://shiro.apache.org/mailing-lists.html)
*譯者注:*如果對本中文翻譯有疑議的或發現勘誤歡迎指正,[點此](https://github.com/waylau/apache-shiro-1.2.x-reference/issues)提問。
- Introduction
- 1. Introduction 介紹
- 2. Tutorial 教程
- 3. Architecture 架構
- 4. Configuration 配置
- 5. Authentication 認證
- 6. Authorization 授權
- 6.1. Permissions 權限
- 7. Realms
- 8. Session Management
- 9. Cryptography 密碼
- 10. Web
- 10.1. Configuration 配置
- 10.2. 基于路徑的 url 安全
- 10.3. Default Filters 默認過濾器
- 10.4. Session Management
- 10.5. JSP Tag Library
- 11. Caching 緩存
- 12. Concurrency & Multithreading 并發與多線程
- 13. Testing 測試
- 14. Custom Subjects 自定義 Subject
- 15. Spring Framework
- 16. Guice
- 17. CAS
- 18. Command Line Hasher
- 19. Terminology 術語
- 20. 10 Minute Tutorial 十分鐘教程
- 21. Beginner's Webapp Tutorial 初學者web應用教程
- 22. Application Security With Apache Shiro 用Shiro保護你的應用安全
- 23. CacheManager 緩存管理
- 24. Apache Shiro Cryptography Features 加密功能