Spring 應(yīng)用中集成 Apache Shiro的方法
這一篇文章涵蓋了將 Shiro 集成到基于 Spring 的應(yīng)用程序的方法。
Shiro 的 Java Bean兼容性使它非常適合通過(guò) Spring XML 或其他基于 Spring 的配置機(jī)制進(jìn)行配置。Shiro 的應(yīng)用程序需要一個(gè)應(yīng)用程序單例安全管理器 ( SecuriyManager) 實(shí)例。注意,這并不一定是靜態(tài)的單例,但是應(yīng)用程序應(yīng)該只使用一個(gè)實(shí)例,不管它是否是靜態(tài)的單例。
1.獨(dú)立的應(yīng)用程序
以下是在 Spring 應(yīng)用程序中啟用應(yīng)用程序單例安全管理器的最簡(jiǎn)單方法:
<!-- 定義連接到后端安全數(shù)據(jù)源的 Realm : --> <bean id="myRealm" class="..."> ... </bean> <bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager"> <!-- 單一 Realm 應(yīng)用這樣寫(xiě)。如果有多個(gè) Realm ,可以使用 "realms" 屬性 --> <property name="realm" ref="myRealm"/> </bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- 對(duì)于最簡(jiǎn)單的集成方式,就像所有的 SecurityUtils 中的靜態(tài) 方法一樣,在所有情況下都適用,將 securityManager bean 聲明 為一個(gè)靜態(tài)的單例對(duì)象。但不要在 web 應(yīng)用程序中這樣做。參見(jiàn) 下面的 “web 應(yīng)用程序” 部分。 --> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/> <property name="arguments" ref="securityManager"/> </bean>
2.Web 應(yīng)用程序
Shiro 對(duì) Spring web 應(yīng)用程序有很棒的支持。在一個(gè) web 應(yīng)用程序中,所有的可用的 web 請(qǐng)求都必須經(jīng)過(guò) Shiro Filter。這個(gè)過(guò)濾器非常強(qiáng)大,允許基于 URL 路徑表達(dá)式執(zhí)行的特殊自定義任何過(guò)濾器鏈。
在 Shiro 1.0之前,你必須在 Spring web 應(yīng)用程序中使用一種混合的方法,定義 Shiro 的過(guò)濾器所有的配置屬性都在 web.xml 中。但是在 spring.xml中定義 securityManager,這有點(diǎn)不友好。
現(xiàn)在,在 Shiro 1.0 以上的版本中,所有的 Shiro 配置都是在Spring XML 中完成的,它提供了更健壯的 Spring 配置機(jī)制。
以下是如何在基于 spring 的 web 應(yīng)用程序中配置 Shiro:
web.xml
除了其他的 spring 的一些標(biāo)簽 ( ContextLoaderListener、Log4jConfigListener 等),還定義了以下過(guò)濾器和過(guò)濾器的映射:
<!-- 在 applicationContext.xml 中,過(guò)濾器名稱 “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>
...
<!-- 確保你想要的任何請(qǐng)求都可以被過(guò)濾。/ * 捕獲所有
請(qǐng)求。通常,這個(gè)過(guò)濾器映射首先 (在所有其他的之前)定義,
確保 Shiro 在過(guò)濾器鏈的后續(xù)過(guò)濾器中工作:-->
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
applicationContext.xml
在 applicationContext.xml 文件,定義 web 適用的SecurityManager 和 “shiroFilter” bean,這個(gè)bean 在 web.xml 中會(huì)被引用。
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!-- 根據(jù)具體情況定義以下幾個(gè)屬性:
<property name="loginUrl" value="/login.jsp"/>
<property name="successUrl" value="/home.jsp"/>
<property name="unauthorizedUrl" value="/unauthorized.jsp"/> -->
<!-- 如果聲明過(guò)任何的 javax.servlet,“filters” 屬性就是不必要的了-->
<!-- <property name="filters">
<util:map>
<entry key="anAlias" value-ref="someFilter"/>
</util:map>
</property> -->
<property name="filterChainDefinitions">
<value>
# 定義需要過(guò)濾的 url :
/admin/** = authc, roles[admin]
/docs/** = authc, perms[document:read]
/** = authc
</value>
</property>
</bean>
<!-- 可以在上下文中定義的任何 javax.servlet.Filter bean,它們會(huì)自動(dòng)被上面的 “shiroFilter” bean 所捕獲,并為“filterChainDefinitions” 屬性所用。如果需要的話,可以手動(dòng)添加/顯式添加到 shiroFilter 的 “filters” Map 上。-->
<bean id="someFilter" class="..."/>
<bean id="anotherFilter" class="..."> ... </bean>
...
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 單一 Realm 應(yīng)用這樣寫(xiě)。如果有多個(gè) Realm ,可以使用 "realms" 屬性. -->
<property name="realm" ref="myRealm"/>
<!-- 認(rèn)情況下,適用 servlet 容器的 session 。取消對(duì)這一行的注釋后則使用 shiro的原生 session -->
<!-- <property name="sessionMode" value="native"/> -->
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 通過(guò)自定義 Shiro Realm 的子類來(lái)使用后臺(tái)的數(shù)據(jù)源 -->
<bean id="myRealm" class="...">
...
</bean>
啟用 Shiro 的注解
在應(yīng)用程序中,可能需要使用 Shiro 的注釋來(lái)進(jìn)行安全檢查(例如,@RequiresRole、@requiresPermission 等等。這需要 Shiro的 Spring AOP 集成,以掃描適當(dāng)?shù)膸ё⑨尩念?,并在必要時(shí)執(zhí)行安全邏輯。下面是如何啟用這些注釋,將這兩個(gè) bean 定義添加到 applicationContext.xml 中:
<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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring boot 1.5.4 集成shiro+cas,實(shí)現(xiàn)單點(diǎn)登錄和權(quán)限控制
- SpringBoot集成Shiro進(jìn)行權(quán)限控制和管理的示例
- springmvc集成shiro登錄權(quán)限示例代碼
- spring boot集成shiro詳細(xì)教程(小結(jié))
- 詳解Spring Boot 集成Shiro和CAS
- spring boot 集成shiro的配置方法
- Spring Boot集成Shiro并利用MongoDB做Session存儲(chǔ)的方法詳解
- 詳解spring與shiro集成
- shiro無(wú)狀態(tài)web集成的示例代碼
- Shiro集成Spring之注解示例詳解
相關(guān)文章
Java開(kāi)發(fā)必備知識(shí)之?dāng)?shù)組詳解
數(shù)組對(duì)于每一門(mén)編程語(yǔ)言來(lái)說(shuō)都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當(dāng)然不同語(yǔ)言對(duì)數(shù)組的實(shí)現(xiàn)及處理也不盡相同.本篇文章為大家整理了Java最全關(guān)于數(shù)組的知識(shí)點(diǎn),并給出其對(duì)應(yīng)的代碼,需要的朋友可以參考下2021-06-06
調(diào)用Process.waitfor導(dǎo)致的進(jìn)程掛起問(wèn)題及解決
這篇文章主要介紹了調(diào)用Process.waitfor導(dǎo)致的進(jìn)程掛起問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
JAVA實(shí)現(xiàn)心跳檢測(cè)(長(zhǎng)連接)
本文主要介紹了JAVA實(shí)現(xiàn)心跳檢測(cè)(長(zhǎng)連接),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
springboot 自定義權(quán)限標(biāo)簽(tld),在freemarker引用操作
這篇文章主要介紹了springboot 自定義權(quán)限標(biāo)簽(tld),在freemarker引用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09

