• 将 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

    1. <span style="font-size:18px"><?xml version="1.0"encoding="UTF-8"?>  
    2. <web-appxmlns:xsiweb-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">  
    3.    
    4. <context-param>  
    5. <param-name>contextConfigLocation</param-name>  
    6. <param-value>classpath*:applicationContext*.xml</param-value>  
    7. </context-param>  
    8.    
    9.  <!--防止发生java.beans.Introspector内存泄露,应将它配置在ContextLoaderListener的前面 -->   
    10.     <!--详细描述见http://blog.csdn.net/jadyer/article/details/11991457 -->   
    11.    <listener>   
    12.        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>   
    13.    </listener>   
    14.      
    15.      <!--实例化Spring容器 -->   
    16.     <!--应用启动时,该监听器被执行,它会读取Spring相关配置文件,其默认会到WEB-INF中查找applicationContext.xml-->   
    17. <listener>  
    18. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    19. </listener>  
    20.    
    21. <!-- 配置编码过滤器 -->  
    22. <filter>  
    23. <filter-name>characterEncodingFilter</filter-name>  
    24. <filter-class>  
    25. org.springframework.web.filter.CharacterEncodingFilter  
    26. </filter-class>  
    27. <init-param>  
    28. <param-name>encoding</param-name>  
    29. <param-value>UTF-8</param-value>  
    30. </init-param>  
    31. <init-param>  
    32. <param-name>forceEncoding</param-name>  
    33. <param-value>true</param-value>  
    34. </init-param>  
    35. </filter>  
    36. <filter-mapping>  
    37. <filter-name>characterEncodingFilter</filter-name>  
    38. <url-pattern>/*</url-pattern>  
    39. </filter-mapping>  
    40.    
    41. <!-- 配置spring管理OpenEntityManagerInViewFilter-->  
    42. <!--OpenEntityManagerInViewFilter会让session一直到view层调用结束后才关闭  
    43. Spring针对Hibernate的非JPA实现用的是OpenSessionInViewFilter,原理与这个大同小异  
    44.  -->  
    45. <filter>  
    46. <filter-name>hibernateFilter</filter-name>  
    47. <filter-class>  
    48. org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter  
    49. </filter-class>  
    50. </filter>  
    51. <filter-mapping>  
    52. <filter-name>hibernateFilter</filter-name>  
    53. <url-pattern>/*</url-pattern>  
    54. </filter-mapping>  
    55.    
    56.    
    57. <!-- Shiro filter-->  
    58.     <!--这里filter-name必须对应applicationContext.xml中定义的<beanid="shiroFilter"/> -->   
    59. <filter>  
    60. <filter-name>shiroFilter</filter-name>  
    61. <filter-class>  
    62. org.springframework.web.filter.DelegatingFilterProxy  
    63. </filter-class>  
    64. <init-param>  
    65. <!--该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理-->   
    66. <param-name>targetFilterLifecycle</param-name>  
    67. <param-value>true</param-value>  
    68. </init-param>  
    69. </filter>  
    70. <filter-mapping>  
    71. <filter-name>shiroFilter</filter-name>  
    72. <url-pattern>/*</url-pattern>  
    73. </filter-mapping>  
    74.    
    75. <!-- 配置Log4j  
    76. 把log4j 的默认配置文件(log4j.properties)放在classpath中,  
    77. 通常是/web-inf/classes目录下.这种方式是log4j的 默认配置方式,  
    78. 无须其他配置。缺点就是无法灵活的通过配置文件来指定log4j.properties的文件位置  
    79.    
    80. webAppRootKey是log4j在容器中的唯一标识,缺省为"webapp.root"  
    81. -->  
    82. <!--        <context-param>  
    83. <param-name>webAppRootKey</param-name>  
    84. <param-value>spring_springmvc_jpa.root</param-value>  
    85. </context-param>  
    86. <context-param>  
    87. <param-name>log4jConfigLocation</param-name>  
    88. <param-value>classpath:log4j.properties</param-value>  
    89. </context-param>  
    90. <listener>  
    91. <listener-class>  
    92. org.springframework.web.util.Log4jConfigListener  
    93. </listener-class>  
    94. </listener> -->  
    95.    
    96.    
    97. <!-- SpringMVC核心分发器 -->  
    98. <servlet>  
    99. <servlet-name>dispatcherServlet</servlet-name>  
    100. <servlet-class>  
    101. org.springframework.web.servlet.DispatcherServlet  
    102. </servlet-class>  
    103. <init-param>  
    104. <param-name>contextConfigLocation</param-name>  
    105. <param-value>classpath:spring-mvc.xml</param-value>  
    106. </init-param>  
    107. <load-on-startup>1</load-on-startup>  
    108. </servlet>  
    109. <servlet-mapping>  
    110. <servlet-name>dispatcherServlet</servlet-name>  
    111. <url-pattern>/</url-pattern>  
    112. </servlet-mapping>  
    113.    
    114. <welcome-file-list>  
    115. <welcome-file>login.jsp</welcome-file>  
    116. </welcome-file-list>  
    117.    
    118.    
    119.     <!--默认欢迎页 -->   
    120.     <!--Servlet2.5中可直接在此处执行Servlet应用,如<welcome-file>servlet/InitSystemParamServlet</welcome-file>-->   
    121.     <!--这里使用了SpringMVC提供的<mvc:view-controller>标签,实现了首页隐藏的目的,详见spring-mvc.xml-->   
    122.     <!--    
    123.    <welcome-file-list>   
    124.        <welcome-file>login.jsp</welcome-file>   
    125.    </welcome-file-list>   
    126.      --></span>  



     

    applicationContext.xml

    1. <span style="font-size:18px"><?xml version="1.0"encoding="UTF-8"?>  
    2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"  
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4. xmlns:aop="http://www.springframework.org/schema/aop"  
    5. xmlns:tx="http://www.springframework.org/schema/tx"  
    6. xmlns:context="http://www.springframework.org/schema/context"  
    7. xmlns:p="http://www.springframework.org/schema/p"  
    8. xmlns:cache="http://www.springframework.org/schema/cache"  
    9. xmlns:jaxws="http://cxf.apache.org/jaxws"  
    10. xsi:schemaLocation="  
    11.                    http://www.springframework.org/schema/beans  
    12.                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
    13.                    http://www.springframework.org/schema/tx  
    14.                    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
    15.                    http://www.springframework.org/schema/aop  
    16.                    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
    17.                    http://www.springframework.org/schema/context       
    18.                    http://www.springframework.org/schema/context/spring-context-3.1.xsd  
    19.                    http://www.springframework.org/schema/cache  
    20.                    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
    21.                    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">  
    22. <!-- 注解支持 -->  
    23. <context:annotation-config />  
    24.    
    25. <!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描-->  
    26. <context:component-scanbase-packagecontext:component-scanbase-package="org.shiro.demo">  
    27. <context:exclude-filter type="annotation"  
    28. expression="org.springframework.stereotype.Controller"/>  
    29. </context:component-scan>  
    30.    
    31. <!-- 属性文件位置 -->  
    32. <context:property-placeholderlocationcontext:property-placeholderlocation="classpath:jdbc.properties" />  
    33.    
    34.    
    35. <!-- 数据源 -->  
    36. <bean id="dataSource"class="com.jolbox.bonecp.BoneCPDataSource"  
    37. destroy-method="close">  
    38. <!-- 数据库驱动 -->  
    39. <property name="driverClass"value="${jdbc.driverClassName}" />  
    40. <!-- 相应驱动的jdbcUrl-->  
    41. <property name="jdbcUrl"value="${jdbc.url}" />  
    42. <!-- 数据库的用户名 -->  
    43. <property name="username"value="${jdbc.username}" />  
    44. <!-- 数据库的密码 -->  
    45. <property name="password"value="${jdbc.password}" />  
    46. </bean>  
    47.    
    48. <!-- JPA实体管理器工厂 -->  
    49. <bean id="entityManagerFactory"  
    50. class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
    51. <property name="dataSource"ref="dataSource" />  
    52. <property name="jpaVendorAdapter"ref="hibernateJpaVendorAdapter" />  
    53. <!-- 加入定制化包路径 -->  
    54. <property name="packagesToScan"value="org.shiro.demo.entity" />  
    55.    
    56. <property name="jpaProperties">  
    57. <props>  
    58. <propkeypropkey="hibernate.current_session_context_class">thread</prop>  
    59. <propkeypropkey="hibernate.hbm2ddl.auto">update</prop><!--validate/update/create -->  
    60. <propkeypropkey="hibernate.show_sql">false</prop>  
    61. <propkeypropkey="hibernate.format_sql">false</prop>  
    62.    
    63. <!-- 建表的命名规则 -->  
    64. <propkeypropkey="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>  
    65.    
    66. </props>  
    67. </property>  
    68. </bean>  
    69.    
    70. <!-- 设置JPA实现厂商的特定属性 -->  
    71. <bean id="hibernateJpaVendorAdapter"  
    72. class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
    73. <property name="databasePlatform"value="${hibernate.dialect}"/>  
    74. </bean>  
    75.    
    76.    
    77.    
    78. <!-- 事务管理器 -->  
    79. <bean id="txManager"  
    80. class="org.springframework.orm.jpa.JpaTransactionManager">  
    81. <property name="entityManagerFactory"ref="entityManagerFactory" />  
    82. </bean>  
    83.    
    84. <!-- 注解式事务 -->  
    85. <tx:annotation-driventransaction-managertx:annotation-driventransaction-manager="txManager" />  
    86. </beans></span>  



     

    spring-mvc.xml

    1. <span style="font-size:18px"><?xml version="1.0"encoding="UTF-8"?>  
    2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"  
    3. xmlns:mvc="http://www.springframework.org/schema/mvc"  
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5. xmlns:context="http://www.springframework.org/schema/context"  
    6. xsi:schemaLocation="  
    7.                 http://www.springframework.org/schema/beans  
    8.                 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
    9.                 http://www.springframework.org/schema/context  
    10.                 http://www.springframework.org/schema/context/spring-context-3.1.xsd  
    11.                 http://www.springframework.org/schema/mvc  
    12.                 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">  
    13.    
    14. <!--启用SpringMVC的注解功能,它会自动注册HandlerMapping、HandlerAdapter、ExceptionResolver的相关实例-->  
    15. <mvc:annotation-driven />  
    16. <!-- SpringMVC的扫描范围 -->  
    17. <context:component-scanbase-packagecontext:component-scanbase-package="org.shiro.demo.controller" />  
    18.    
    19. <!--默认访问跳转到登录页面,即定义无Controller的path<->view直接映射 -->  
    20. <mvc:view-controller path="/"view-name="redirect:/login"/>  
    21.    
    22.    
    23. <!-- 静态文件访问 -->  
    24. <mvc:resources mapping="/resources/**"location="/resources/" />  
    25.    
    26. <!-- 配置SpringMVC的视图解析器 -->  
    27. <!--其viewClass属性的默认值就是org.springframework.web.servlet.view.JstlView -->   
    28. <beanclassbeanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    29. <!-- <property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /> -->  
    30. <property name="prefix"value="/" />  
    31. <property name="suffix"value=".jsp" />  
    32. </bean>  
    33.    
    34. <!-- 配置SpringMVC的异常解析器 -->  
    35. <beanclassbeanclass="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
    36. <property name="exceptionMappings">  
    37. <props>  
    38. <!-- 发生授权异常时,跳到指定页 -->  
    39. <propkeypropkey="org.apache.shiro.authz.UnauthorizedException">/system/error</prop>  
    40.    
    41.  <!--SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException-->   
    42.  <!--遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/error_fileupload.jsp页面-->   
    43.                <!-- <propkey="org.springframework.web.multipart.MaxUploadSizeExceededException">WEB-INF/error_fileupload</prop>-->   
    44. </props>  
    45. </property>  
    46. </bean>     
    47. </beans></span>  



     

    ehcache-shiro.xml

    1. <span style="font-size:18px"><?xml version="1.0"encoding="UTF-8"?>  
    2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3. xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsd"  
    4. default-lazy-init="true">  
    5.    
    6. <description>Shiro安全配置</description>  
    7.    
    8. <!-- shiro securityManager -->  
    9. <!--Shiro默认会使用Servlet容器的Session,可通过sessionMode属性来指定使用Shiro原生Session -->   
    10.     <!--即<property name="sessionMode"value="native"/>,详细说明见官方文档 -->   
    11.     <!--这里主要是设置自定义的单Realm应用,若有多个Realm,可使用'realms'属性代替 -->   
    12. <bean id="securityManager"  
    13. class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
    14. <property name="realm"ref="shiroDbRealm" />  
    15. <!-- <property name="cacheManager"ref="myShiroEhcacheManager" /> -->  
    16. <!-- <property name="sessionMode" value="native"/>  
    17.  <property name="sessionManager" ref="sessionManager"/>  
    18. -->  
    19. </bean>  
    20.    
    21.           <!-- 用户授权信息Cache,采用EhCache -->  
    22. <bean id="myShiroEhcacheManager"class="org.apache.shiro.cache.ehcache.EhCacheManager">  
    23. <property name="cacheManagerConfigFile"value="classpath:ehcache-shiro.xml"/>  
    24. </bean>  
    25.    
    26. <!--继承自AuthorizingRealm的自定义Realm,即指定Shiro验证用户的认证和授权 -->   
    27.     <beanidbeanid="shiroDbRealm"class="org.shiro.demo.service.realm.ShiroDbRealm"depends-on="baseService">  
    28.            <propertynamepropertyname="userService" ref="userService"/>  
    29.    </bean>  
    30.    
    31. <!--Shiro主过滤器本身功能十分强大,其强大之处就在于它支持任何基于URL路径表达式的、自定义的过滤器的执行 -->  
    32. <!-- Shiro Filter -->  
    33. <bean id="shiroFilter"  
    34. class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
    35. <!-- Shiro的核心安全接口,这个属性是必须的 -->   
    36. <property name="securityManager"ref="securityManager" />  
    37. <!--要求登录时的链接,非必须的属性,默认会自动寻找Web工程根目录下的"/login.jsp"页面 -->   
    38. <property name="loginUrl"value="/" />  
    39. <!--登录成功后要跳转的连接(本例中此属性用不到,因为登录成功后的处理逻辑在LoginController里硬编码为main.jsp了) -->   
    40. <property name="successUrl"value="/system/main" />  
    41. <!-- 用户访问未对其授权的资源时,所显示的连接 -->   
    42. <property name="unauthorizedUrl"value="/system/error" />  
    43.  <!-- Shiro过滤链的定义 -->   
    44.         <!--此处可配合这篇文章来理解各个过滤连的作用http://blog.csdn.net/jadyer/article/details/12172839 -->   
    45.         <!--下面value值的第一个'/'代表的路径是相对于HttpServletRequest.getContextPath()的值来的 -->   
    46.         <!--anon:它对应的过滤器里面是空的,什么都没做,这里.do和.jsp后面的*表示参数,比方说login.jsp?main这种 -->   
    47.         <!--authc:该过滤器下的页面必须验证后才能访问,它是Shiro内置的一个拦截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter-->   
    48. <propertynamepropertyname="filterChainDefinitions">  
    49. <value>  
    50. /login = anon  
    51. /validateCode = anon  
    52.                   /** = authc  
    53. </value>  
    54. </property>  
    55. </bean>  
    56.    
    57. <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->  
    58. <bean id="lifecycleBeanPostProcessor"  
    59. class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>  
    60.    
    61.      <!--开启Shiro的注解,实现对Controller的方法级权限检查(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro注解的类,并在必要时进行安全逻辑验证-->   
    62.     <!--配置以下两个bean即可实现此功能 -->   
    63.     <!--Enable Shiro Annotations for Spring-configured beans. Only run after thelifecycleBeanProcessor has run -->   
    64. <bean  
    65. class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"  
    66. depends-on="lifecycleBeanPostProcessor" >  
    67. <property name="proxyTargetClass"value="true" />  
    68. </bean>  
    69. <bean  
    70. class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">  
    71. <property name="securityManager"ref="securityManager" />  
    72. </bean>  
    73.    
    74.    
    75. </beans></span>  


  • 相关阅读:
    1.2基础--对象属性操作检测遍历
    C语言概览
    测试博客园字体
    c语言指针理解,指针的概念和演示指针的简单操作2
    c语言指针理解代码示例_函数返回指针变量_解除运算符获取指针的值
    c语言指针理解,指针的概念和演示指针的简单操作
    c语言指针概念解释_初步
    项目对文件分目录的感想
    visual studio快捷键
    java开源项目RuoYi若依管理系统
  • 原文地址:https://www.cnblogs.com/hanxue112253/p/3850596.html
Copyright © 2020-2023  润新知