一、配置開發環境
1、導入要使用的spring開發包
2、導入shiro和shiro-spring整合包
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.1</version>
</dependency>
3、配置mybatis.cfg.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 進行Mybatis的相應的環境的屬性定義 -->
<settings> <!-- 在本項目之中開啓二級緩存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
<typeAliases>
<package name="vo"/>
</typeAliases>
</configuration>
4、配置applicationContext.xml文件:.
//...忽略spring的配置
<!-- 定義Spring與MyBatis整合的操作控制,此時數據庫的連接對象取得由Spring負責 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 配置所有資源文件的保存路徑的資源匹配符 -->
<property name="mapperLocations" value="classpath:cn/mldn/vo/mapping/*.xml"/>
<property name="configLocation" value="classpath:mybatis.cfg.xml"/>
</bean>
//...忽略spring的配置
5、在web.xml文件裏面除配置Spring容器以及Spring MVC的操作外還需要配置shiro要使用的過濾器:
<!-- 進行shiro的過濾器的配置 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<!-- 該參數表示shiro的生命週期將交由Spring容器進行管理(默認情況下,取值爲false) -->
<!-- 如果將其內容設置爲true,則表示由Servlet容器進行管理 -->
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
6、隨後還需要在applicationContext.xml定義shiro的相關配置:
· 如果現在你的shiro是交由Spring來進行管理,則應該出現有如下的配置信息:
<!-- 配置SecurityManager的管理 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 配置你需要使用的Realms -->
<property name="realm" ref="memberRealm"/>
</bean>
<!-- 配置shiro過濾器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 表示現在要配置的是一個安全管理器 -->
<property name="securityManager" ref="securityManager"/>
<!-- 出現錯誤之後的跳轉路徑的配置 -->
<property name="loginUrl" value="/loginUrl.action"/>
<!-- 認證失敗之後的跳轉路徑頁面 -->
<property name="unauthorizedUrl" value="/unauthUrl.action"/>
<!-- 登錄成功之後的跳轉訪問路徑 -->
<property name="successUrl" value="/successUrl.action"/>
<!-- shiro裏面需要針對於所有的路徑進行配置,所有的配置需要通過文本的形式設置 -->
<property name="filterChainDefinitions">
<value>
/*=anon
/shiroLogin.action=anon
/pages/**=authc
/admin*=authc
/pages/welcome.jsp=authc,perms[member:add],perms[dept:add]
</value>
</property>
</bean>
如果此時shiro是交由Spring管理進行配置,那麼還需要定義以下的一個bean:
<!-- 配置Shiro在Spring中的生命週期的控制操作 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>