8.shiro集成spring,web

1.web集成地址:http://shiro.apache.org/web.html
講述了怎麼配置web.xml filter等 已經jsp引入shiro標籤的使用

2.Apache Shiro集成到spring http://shiro.apache.org/spring.html

本頁面介紹瞭如何將Shiro集成到基於spring的應用程序中。
Shiro的JavaBeans兼容性使其非常適合通過Spring XML或其他基於Spring的配置機制進行配置。Shiro應用程序需要一個單例SecurityManager實例。注意,這並不一定是一個靜態單例,但是應用程序應該只使用一個實例,不管它是不是一個靜態單例。
Standalone Applications 獨立的項目
下面是在Spring應用中啓用單例SecurityManager的最簡單方法:

<!-- 配置你自己的Realm路徑.Define the realm you want to use to connect to your back-end security datasource: -->
<bean id="myRealm" class="...">
    ...
</bean>

<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">
    <!-- 配置一個或者多個realm屬性.Single realm app.  If you have multiple realms, use the 'realms' property instead. -->
    <property name="realm" ref="myRealm"/>
</bean>

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- For simplest integration, so that all SecurityUtils.* methods work in all cases, -->
<!-- make the securityManager bean a static singleton.  DO NOT do this in web web項目不要定義單例        -->
<!-- applications - see the 'Web Applications' section below instead.                 -->
<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 web項目
Shiro對Spring web應用程序有一流的支持。在web應用程序中,所有可訪問Shiro的web請求都必須經過一個主Shiro過濾器。這個過濾器本身非常強大,允許基於任何URL路徑表達式執行自定義過濾器鏈。
在Shiro 1.0之前,必須在Spring web應用程序中使用混合方法定義Shiro過濾器 它的所有配置屬性都在web.xml中,但是在Spring XML中定義了SecurityManager。這有點令人沮喪,因爲您不能1)將您的配置合併到一個地方,2)利用更高級的Spring特性(如PropertyPlaceholderConfigurer或抽象bean)的配置功能來合併通用配置
現在在Shiro 1.0以及之後的版本中,所有Shiro配置都是在Spring XML中完成的,提供了對更健壯的Spring配置機制的訪問。
下面是如何在一個基於spring的web應用程序中配置Shiro:
web.xml
除了您的其他Spring web.xml元素(ContextLoaderListener、Log4jConfigListener等)之外,還定義了以下過濾器和過濾器映射:

<!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.xml -->
<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>

...

<!-- Make sure any request you want accessible to Shiro is filtered. /* catches all -->
<!-- requests.  Usually this filter mapping is defined first (before all others) to -->
<!-- ensure that Shiro works in subsequent filters in the filter chain:             -->
<filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

applicationContext.xml
在您的applicationContext.xml文件中,定義web啓用的SecurityManager和將從web.xml引用的“shiroFilter”bean。

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <!-- override these for application-specific URLs if you like:
    <property name="loginUrl" value="/login.jsp"/> 登陸頁面
    <property name="successUrl" value="/home.jsp"/> 登陸成功後的頁面>首頁
    <property name="unauthorizedUrl" value="/unauthorized.jsp"/> 登陸驗證失敗的頁面       -->
    <!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean  -->
    <!-- defined will be automatically acquired and available via its beanName in chain        -->
    <!-- definitions, but you can perform instance overrides or name aliases here if you like: -->
    <!-- <property name="filters">
        <util:map>
            <entry key="anAlias" value-ref="someFilter"/>  #下面定義的bean
        </util:map>
    </property> -->
    <property name="filterChainDefinitions">
        <value>
            # some example chain definitions: 權限和角色攔截設置
            /admin/** = authc, roles[admin]     攔截url /admin/**,只要角色admin可以訪問
            /docs/** = authc, perms[document:read]  權限時document:read可訪問
            /** = authc                             登陸後纔可以訪問
            # more URL-to-FilterChain definitions here
        </value>
    </property>
</bean>

<!-- Define any javax.servlet.Filter beans you want anywhere in this application context.   -->
<!-- They will automatically be acquired by the 'shiroFilter' bean above and made available -->
<!-- to the 'filterChainDefinitions' property.  Or you can manually/explicitly add them     -->
<!-- to the shiroFilter's 'filters' Map if desired. See its JavaDoc for more details.       -->
<bean id="someFilter" class="..."/>      #定義的bean它們將被上面的“shiroFilter”bean自動獲取到filterChainDefinitions
<bean id="anotherFilter" class="..."> ... </bean>
...

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->
    <property name="realm" ref="myRealm"/>
    <!-- By default the servlet container sessions will be used.  Uncomment this line
         to use shiro's native sessions (see the JavaDoc for more): -->
    <!-- <property name="sessionMode" value="native"/> -->
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- Define the Shiro Realm implementation you want to use to connect to your back-end -->
<!-- security datasource: -->  #自定義的realm路徑
<bean id="myRealm" class="...">
    ...
</bean>

Enabling Shiro Annotations shiro註解開啓
在獨立應用程序和web應用程序中,您可能希望將Shiro的註釋用於安全檢查(例如,@RequiresRoles、@ requirespermission等)。這需要Shiro的Spring AOP集成來掃描適當的帶註釋的類,並根據需要執行安全邏輯。
下面是如何啓用這些註釋。只需將這兩個bean定義添加到applicationContext.xml:

<!-- Enable Shiro Annotations for Spring-configured beans.  Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<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 remoting支持包括兩部分:客戶端進行遠程調用的配置和服務器接收和處理遠程調用的配置。
Server-side Configuration
當遠程方法調用進入啓用shiro的服務器時,必須將與該RPC調用關聯的主題綁定到接收線程,以便在線程執行期間進行訪問。這是通過定義Shiro的SecureRemoteInvocationExecutor bean在applicationContext.xml:

<!-- Secure Spring remoting:  Ensure any Spring Remoting method invocations -->
<!-- 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>

一旦定義了此bean,就必須將其插入到用於導出/公開服務的任何remoting導出器。導出器實現是根據使用的remoting機制/協議定義的。參見Spring的Remoting一章關於定義出口商bean。
例如,如果使用基於http的remoting(注意secureRemoteInvocationExecutor bean的屬性引用):

<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客戶端配置
當執行遠程調用時,必須將主題標識信息附加到remoting有效負載,以便讓服務器知道是誰在進行調用。如果客戶端是基於spring的客戶端,則通過Shiro的SecureRemoteInvocationFactory進行關聯

<bean id="secureRemoteInvocationFactory" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationFactory"/>:

定義了這個bean之後,需要將它插入到正在使用的特定於協議的Spring remoting ProxyFactoryBean。
例如,如果您使用基於http的remoting(注意上面定義的secureRemoteInvocationFactory bean的屬性引用):

<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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章