[翻譯-Shiro]-整合Apache Shiro到基於Spring的應用

劉曉日發表於2011-09-28

譯者:劉曉日

本文主要介紹如何將Apache Shiro整合到基於Spring的應用。

Shiro相容javabean使得它能很好的與Spring XML或其他基於Spring的配置方式整合。在基於Shiro的應用程式中的SecurityManager是單例的。不過,SecurityManager不一定是靜態單例,但是不論是否是靜態單例,必須保證一個應用程式中只有一個SecurityManager的例項。

獨立的應用程式

下面是在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應用程式

Shiro對基於Spring的Web應用提供了完美的支援,web應用中,Shiro可控制的web請求必須經過Shiro主過濾器的攔截。Shiro主過濾器本身功能十分強大,其強大之處就在於它支援任何基於URL路徑表示式的、自定義的過濾器的執行。

Shiro1.0版本前,Spring web應用使用混合方式進行配置,在web.xml中定義Shiro的過濾器和它所有的配置,而在Spring XML中定義SecurityManager。這樣有些彆扭,因為第一你沒辦法將所有的配置放到一個位置;第二沒法最大發揮Spring提供配置上的優點,比如PropertyPlaceholderConfigurer或者通過抽象beans來合併配置。

在Shiro1.0或更高版本中,所有的Shiro配置都放到Spring XML中,這樣做進一步發揮了Spring配置機制的優勢。

下面是如何在基於Spring的web應用中配置Shiro:

  • web.xml

除了定義ContextLoaderListener, Log4jConfigListener等Spring元素外,只要在web.xml中增加如下的filter和filter-mapping:

<!-- 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中定義SecurityManager和web.xml中使用的名字叫shiroFilter的bean

<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>
  • 開啟Shiro的註解

不論獨立的應用程式還是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>
  • Spring遠端安全

Shiro的Spring遠端支援有兩部分組成:遠端呼叫的客戶端配置和接收、處理遠端呼叫的伺服器端配置。

Server端配置

當Shiro的Server端接收到一個遠端方法呼叫時,與遠端呼叫相關的Subject必須在接收執行緒執行時繫結到接收執行緒上,這項工作通過在applicationContext.xml中定義SecureRemoteInvocationExecutor bean完成。

<!-- Spring遠端安全確保每個遠端方法呼叫都與一個負責安全驗證的Subject繫結 -->
<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的遠端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端配置

當遠端呼叫發生時,負責鑑別資訊的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>

原文地址:http://shiro.apache.org/spring.html

相關文章