一、配置开发环境
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"/>