首页 > 代码库 > 将 Shiro 作为应用的权限基础 五:SpringMVC+Apache Shiro+JPA(hib
将 Shiro 作为应用的权限基础 五:SpringMVC+Apache Shiro+JPA(hib
点击链接加入群【JavaEE(SSH+IntelliJIDE+Maven)】:http://jq.qq.com/?_wv=1027&k=L2rbHv
将 Shiro 作为应用的权限基础 五:SpringMVC+Apache Shiro+JPA(hibernate)整合配置
配置web.xml,applicationContext.xml, spring-mvc.xml,applicationContext-shiro.xml,而且都有详细的说明。
web.xml是web项目最基本的配置文件,看这个配置,可以快速知道web项目使用什么框架,它就像一个面板,切入我们想用的插件。
applicationContext.xml是spring的基本配置,主要配置数据源、JPA实体管理器工厂、事务;
spring-mvc.xml是SpringMVC的配置;
applicationContext-shiro.xml是shiro的配置,主要配置securityManager、shiroFilter;
web.xml
<?xml version="1.0"encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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> <filter-name>hibernateFilter</filter-name> <filter-class> org.springframework.orm.jpa.support .OpenEntityManagerInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Shiro filter--> <!-- 这里filter-name必须对应applicationContext.xml中定义的 <beanid="shiroFilter"/> --> <filter> <filter-name>shiroFilter</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> <init-param> <!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理, 设置为true则表示由ServletContainer管理 --> <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> <!-- 配置Log4j 把log4j 的默认配置文件(log4j.properties)放在classpath中, 通常是/web-inf/classes目录下.这种方式是log4j的 默认配置方式, 无须其他配置。 缺点就是无法灵活的通过配置文件来指定log4j.properties的文件位置 webAppRootKey是log4j在容器中的唯一标识,缺省为"webapp.root" --> <!-- <context-param> <param-name>webAppRootKey</param-name> <param-value>spring_springmvc_jpa.root</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> --> <!-- SpringMVC核心分发器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!--默认欢迎页 --> <!-- Servlet2.5中可直接在此处执行Servlet应用,如 <welcome-file> servlet/InitSystemParamServlet </welcome-file> --> <!-- 这里使用了SpringMVC提供的<mvc:view-controller>标签, 实现了首页隐藏的目的,详见spring-mvc.xml --> <!-- <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> --> </web-app>
applicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "> <!-- 注解支持 --> <context:annotation-config /> <!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描--> <context:component-scan base-package="org.shiro.demo"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 属性文件位置 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 数据源 --> <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"> <!-- 数据库驱动 --> <property name="driverClass"value=http://www.mamicode.com/"${jdbc.driverClassName}" />>spring-mvc.xml
<?xml version="1.0"encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!--启用SpringMVC的注解功能,它会自动注册 HandlerMapping、HandlerAdapter、ExceptionResolver的相关实例--> <mvc:annotation-driven /> <!-- SpringMVC的扫描范围 --> <context:component-scan base-package="org.shiro.demo.controller" /> <!--默认访问跳转到登录页面,即定义无Controller的path<->view直接映射 --> <mvc:view-controller path="/"view-name="redirect:/login"/> <!-- 静态文件访问 --> <mvc:resources mapping="/resources/**"location="/resources/" /> <!-- 配置SpringMVC的视图解析器 --> <!--其viewClass属性的默认值就是 org.springframework.web.servlet.view.JstlView --> <bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- <property name="viewClass" value=http://www.mamicode.com/"org.springframework.web.servlet.view.JstlView" /> -->>ehcache-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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsd" default-lazy-init="true"> <description>Shiro安全配置</description> <!-- shiro securityManager --> <!--Shiro默认会使用Servlet容器的Session, 可通过sessionMode属性来指定使用Shiro原生Session --> <!--即<property name="sessionMode"value=http://www.mamicode.com/"native"/>,>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。