日期:2014-05-16  浏览次数:20750 次

将 Shiro 作为应用的权限基础 五:SpringMVC+Apache Shiro+JPA(hibernate)整合配置

配置web.xml,applicationContext.xml, spring-mvc.xml,applicationContext-shiro.xml,而且都有详细的说明。

 

Web.xmlweb项目最基本的配置文件,看这个配置,可以快速知道web项目使用什么框架,它就像一个面板,切入我们想用的插件。

applicationContext.xmlspring的基本配置,主要配置数据源、JPA实体管理器工厂、事务

spring-mvc.xmlSpringMVC的配置,

applicationContext-shiro.xml是shiro的配置,主要配置securityManager、shiroFilter

 

 

Web.xml

<?xml version="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
 
 <!--防止发生java.beans.Introspector内存泄露,应将它配置在ContextLoaderListener的前面 --> 
    <!--详细描述见http://blog.csdn.net/jadyer/article/details/11991457 --> 
   <listener> 
       <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 
   </listener> 
   
     <!--实例化Spring容器 --> 
    <!--应用启动时,该监听器被执行,它会读取Spring相关配置文件,其默认会到WEB-INF中查找applicationContext.xml--> 
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
<!-- 配置编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
 
<!-- 配置spring管理OpenEntityManagerInViewFilter-->
<!--OpenEntityManagerInViewFilter会让session一直到view层调用结束后才关闭
Spring针对Hibernate的非JPA实现用的是OpenSessionInViewFilter,原理与这个大同小异
 -->
<filter>
&