1. 程式人生 > >maven專案中SSM+Shiro整合配置中,在有SpringMVC+MyBatis配置下整合shiro的配置步驟

maven專案中SSM+Shiro整合配置中,在有SpringMVC+MyBatis配置下整合shiro的配置步驟

      最近在做SSM(SpringMVC+Shiro+MyBatis)整合的一個專案,對於shiro的配置不是特別熟練,於是想要寫下來,加深一下印象。在有SpringMVC+MyBatis配置的基礎上整合shiro:

      首先進入shiro的官方網站(http://shiro.apache.org/),如下圖所示(圖中有提示下一步怎麼操作):

點選Download進入到下面這個頁面:

向下拖動,可以看到shiro的相關依賴,新增四個依賴到(pom.xml)中:分別是shiro-core、shiro-spring、shiro-web、shiro-ehcache,


這裡我把我的依賴給弄下來(pom.xml):

<!-- Shiro -->
			<dependency>
				<groupId>org.apache.shiro</groupId>
				<artifactId>shiro-core</artifactId>
				<version>1.3.2</version>
			</dependency>

			<dependency>
				<groupId>org.apache.shiro</groupId>
				<artifactId>shiro-web</artifactId>
				<version>1.3.2</version>
			</dependency>

			<dependency>
				<groupId>org.apache.shiro</groupId>
				<artifactId>shiro-spring</artifactId>
				<version>1.3.2</version>
			</dependency>

			<dependency>
				<groupId>org.apache.shiro</groupId>
				<artifactId>shiro-ehcache</artifactId>
				<version>1.3.2</version>
			</dependency>

新增完依賴之後,開始配置:


依次如圖所示,找到有配置資訊的頁面:


進入到配置檔案(Web-configuration):

點選上圖指示的地方,進入到下一個頁面:


進入到這裡,可以開始配置了:


如圖中所示,配置檔案:



我現在我把我配置的資訊給複製過來,方便大家直接複製:

這是applicationContext.xml裡的配置,但是我是將有關shiro的配置單獨放在一個xml檔案中(application_shiro.xml),我個人覺得這樣更容易區分:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!--第一步:直接配置一個 securityManager -->

	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		

<!--剛配置時,先把這條註釋掉,等後面寫了MyRealm.java時,再把它的註釋去掉,因為如果沒有去掉就會在
tomcat開啟時報一個錯誤 -->

<!--<property name="realm" ref="myRealm" /> -->

</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- 第三步:把請求路徑攔截之後的處理 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!--shiro整個的處理,都由securityManger指定和決定 -->
<property name="securityManager" ref="securityManager" />
<!-- loginUrl==>如果登入成功,跳轉到哪個頁面,或者執行哪個請求 -->
<property name="loginUrl" value="/login.jsp" />
<!-- 驗證通過執行的請求或者跳轉 -->
<property name="successUrl" value="/home.jsp" />
<!-- 驗證不通過執行的請求或者跳轉 -->
<property name="unauthorizedUrl" value="/unauthorized.jsp" />
<!-- 具體的攔截路徑和攔截的方式和角色和許可權的範圍 -->
<property name="filterChainDefinitions">
<value>
</value>
</property>
</bean>
</beans>

接著配置web.xml:

<!-- 第二步:伺服器啟動時,載入shiro的配置檔案
  -->
  <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>
<!-- 攔截所有的請求路徑 -->
<filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 還有一個地方要注意,因為我是單獨把shiro寫在一個xml檔案中,新加了一個檔案,所以要新增路徑 -->
<context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>
  		classpath:applicationContext.xml
  		classpath:application_shiro.xml
  	</param-value>
  </context-param>

然後再是spring_mvc.xml(添加註解功能):
<!-- 配置啟用Shiro的註解功能 -->
	<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" 
		depends-on="lifecycleBeanPostProcessor">
		<property name="proxyTargetClass" value="true"></property>
	</bean>
		
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
	    <property name="securityManager" ref="securityManager"/>
	</bean>


然後開啟
配置檔案完成,開啟tomcat,成功開啟(出現如圖所示的紅線部分,說明配置shiro成功)


配置成功後,可以開始寫程式碼了。