1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  9. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
  11.  
  12. <!-- 自动扫描的目录 -->
  13. <context:component-scan base-package="com.ang.elearning"></context:component-scan>
  14.  
  15. <!-- 引入配置文件 -->
  16. <bean id="propertyConfigurer"
  17. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  18. <property name="location" value="classpath:db.properties"></property>
  19. </bean>
  20.  
  21. <!-- 配置dbcp数据源 -->
  22. <!-- destroy-method="close"保证当spring不在Web Container或是EJB Container中的情况下,在tomcat退出时,
  23. 调用AbstractApplicationContext的close方法,其实就是调用context中beanFactory的destroySingletons()方法 -->
  24. <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"
  25. destroy-method="close">
  26. <property name="driverClassName" value="${driver}"></property>
  27. <property name="url" value="${url}"></property>
  28. <property name="username" value="${username}"></property>
  29. <property name="password" value="${password}"></property>
  30. <!-- 初始化连接大小 -->
  31. <property name="initialSize" value="${initialSize}"></property>
  32. <!-- 连接池最大数量 -->
  33. <property name="maxActive" value="${maxActive}"></property>
  34. <!-- 连接池最大空闲 -->
  35. <property name="maxIdle" value="${maxIdle}"></property>
  36. <!-- 连接池最小空闲 -->
  37. <property name="minIdle" value="${minIdle}"></property>
  38. <!-- 获取连接最大等待时间
  39. <property name="maxWait" value="${maxWait}"></property> -->
  40. </bean>
  41.  
  42. <!-- 配置SqlSessionFactoryBean -->
  43. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  44. <property name="dataSource" ref="datasource"></property>
  45.  
  46. <!-- 指定扫描mybatis映射文件的目录 -->
  47. <property name="mapperLocations" value="classpath:com/ang/elearning/mapping/*.xml"></property>
  48. </bean>
  49.  
  50. <!-- 为每一个mapper接口手动配置MapperFactoryBean,不推荐,推荐使用下面的方法 -->
  51. <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  52. <property name="mapperInterface" value="com.ang.elearning.dao.UserMapper"></property>
  53. <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> -->
  54.  
  55. <!-- 注册MapperScannerConfigurer,自动创建MapperFactoryBean -->
  56. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  57.  
  58. <!-- basePackage指定mapper接口所在的包名,MapperScannerConfigurer会查找该类路径下的接口并自动为其创建MapperFactoryBean -->
  59. <property name="basePackage" value="com.ang.elearning.dao"></property>
  60.  
  61. <!-- 当只有一个datasource时,不用指定,因为MapperScannerConfigurer会自动装配MapperFactoryBean
  62. 但是,当有多个datasource时,自动装配可能失效。另外,这里配置的是bean的名称,所以不能用ref -->
  63. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  64. </bean>
  65.  
  66. <!-- 配置事务管理器 -->
  67. <bean id="transactionManager"
  68. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  69. <property name="dataSource" ref="datasource"></property>
  70. </bean>
  71.  
  72. <!-- 配置事务属性 -->
  73. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  74. <tx:attributes>
  75. <tx:method name="add*" propagation="REQUIRED" />
  76. <tx:method name="insert*" propagation="REQUIRED" />
  77. <tx:method name="delete*" propagation="REQUIRED" />
  78. <tx:method name="update*" propagation="REQUIRED" />
  79. <tx:method name="get*" read-only="true" />
  80. <tx:method name="select*" read-only="true" />
  81. <tx:method name="*" propagation="REQUIRED" />
  82. </tx:attributes>
  83. </tx:advice>
  84.  
  85. <!-- 配置事务切入点,以及把事务切入点和事务属性关联起来 -->
  86. <aop:config>
  87. <!-- 对com.ang.elearning.service.impl包下的所有类的所有方法执行事务 -->
  88. <aop:pointcut expression="execution(* com.ang.elearning.service.impl.*.*(..))"
  89. id="pointcut" />
  90. <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
  91. </aop:config>
  92.  
  93. <!---------------------------------------------------------------------------------------------------------------->
  94.  
  95. <!-- shiro start -->
  96. <!-- 1. 配置SecurityManager -->
  97. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  98. <property name="cacheManager" ref="cacheManager" />
  99. <property name="authenticator" ref="authenticator"></property>
  100. <!-- 可以配置多个Realm,其实会把realms属性赋值给ModularRealmAuthenticator的realms属性 -->
  101. <property name="realms">
  102. <list>
  103. <ref bean="userRealm" />
  104. <ref bean="adminRealm"/>
  105. <ref bean="teacherRealm"/>
  106. </list>
  107. </property>
  108. </bean>
  109.  
  110. <!-- 2. 配置CacheManager -->
  111. <!-- 2.1 需要加入ehcache的jar包及配置文件 -->
  112. <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
  113. <property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />
  114. </bean>
  115.  
  116. <!-- 3. 配置Realm -->
  117. <!-- 3.1 直接配置继承了 org.apache.shiro.realm.AuthorizingRealm的bean -->
  118. <bean id="userRealm" class="com.ang.elearning.shiro.UserRealm">
  119. <!-- 配置密码匹配器 -->
  120. <property name="credentialsMatcher">
  121. <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
  122. <!-- 加密算法为MD5 -->
  123. <property name="hashAlgorithmName" value="MD5"></property>
  124. <!-- 加密次数 -->
  125. <property name="hashIterations" value="1024"></property>
  126. </bean>
  127. </property>
  128. </bean>
  129.  
  130. <bean id="adminRealm" class="com.ang.elearning.shiro.AdminRealm">
  131. <!-- 配置密码匹配器 -->
  132. <property name="credentialsMatcher">
  133. <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
  134. <!-- 加密算法为MD5 -->
  135. <property name="hashAlgorithmName" value="MD5"></property>
  136. <!-- 加密次数 -->
  137. <property name="hashIterations" value="1024"></property>
  138. </bean>
  139. </property>
  140. </bean>
  141.  
  142. <bean id="teacherRealm" class="com.ang.elearning.shiro.TeacherRealm">
  143. <!-- 配置密码匹配器 -->
  144. <property name="credentialsMatcher">
  145. <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
  146. <!-- 加密算法为MD5 -->
  147. <property name="hashAlgorithmName" value="MD5"></property>
  148. <!-- 加密次数 -->
  149. <property name="hashIterations" value="1024"></property>
  150. </bean>
  151. </property>
  152. </bean>
  153.  
  154. <!-- 4. 配置LifecycleBeanPostProcessor,可以自定义地来调用配置在Spring IOC容器中shiro bean的生命周期方法 -->
  155. <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
  156.  
  157. <!-- 5. 使能够在IOC容器中使用shiro的注解,但必须在配置了LifecycleBeanPostProcessor之后才可以使用 -->
  158. <bean
  159. class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
  160. depends-on="lifecycleBeanPostProcessor" />
  161. <bean
  162. class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
  163. <property name="securityManager" ref="securityManager" />
  164. </bean>
  165.  
  166. <!-- 6. 配置ShiroFilter -->
  167. <!-- 6.1 id必须和web.xml中配置的DelegatingFilterProxy的<filter-name>一致。 如果不一致,会抛出NoSuchBeanDefinitionException异常,因为shiro会在IOC容器中查找名称和<filter-name>
  168. 值一致的filter bean -->
  169. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  170. <property name="securityManager" ref="securityManager" />
  171. <property name="loginUrl" value="/login.jsp" />
  172. <property name="unauthorizedUrl" value="/login.jsp" />
  173. <!-- 配置哪些页面需要受保护,以及访问这些页面需要的权限 -->
  174. <property name="filterChainDefinitions">
  175. <value>
  176. <!-- 第一次匹配优先的原则 -->
  177. /** = anon
  178. <!-- /login.jsp = anon
  179. /user/login = anon
  180. /admin/login = anon
  181. /teacher/login = anon
  182.  
  183. 测试:只有admin角色可以访问test.jsp
  184. /test.jsp = roles[admin]
  185.  
  186. /logout = logout
  187.  
  188. /** = authc -->
  189. </value>
  190. </property>
  191. </bean>
  192.  
  193. <!-- 7. 配置使用自定义认证器,可以实现多Realm认证,并且可以根据登录类型指定使用特定的Realm -->
  194. <bean id="authenticator" class="com.ang.elearning.shiro.CustomizedModularRealmAuthenticator">
  195. <!-- 配置认证策略,只要有一个Realm认证成功即可,并且返回所有认证成功信息 -->
  196. <property name="authenticationStrategy">
  197. <bean class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"></bean>
  198. </property>
  199. </bean>
  200. <!-- shiro end -->
  201. </beans>

shiro配置参考(二)可以和mybatis的配置放在一个文件中(不建议这样,可以拆分开来,注意相关配置即可)的更多相关文章

  1. 使用 PySide2 开发 Maya 插件系列二:继承 uic 转换出来的 py 文件中的类 Ui_Form

    使用 PySide2 开发 Maya 插件系列二:继承 uic 转换出来的 py 文件中的类 Ui_Form 开发环境: Wing IDE 6.1 步骤1: 打开 Wing IDE,创建一个新的 pr ...

  2. spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置

    spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...

  3. SpringBoot集成mybatis,同时读取一个数据库中多个数据表

    SpringBoot集成mybatis,同时读取一个数据库中多个数据表: application.properties: mybatis.config-location=classpath:mybat ...

  4. C++ vector 实现二维数组时, 在类的头文件中定义时遇到"应输入类型符"的问题?

    见下,当我在类的声明文件中定义二维vector时,提示我应输入类型说明符; 但是相同的格式定义,在类中将二维vector修改为在源文件中定义就可以顺利通过,并顺利执行打印 打印结果如下: 望大神来解惑 ...

  5. 【Mybatis架构】Mapper映射文件中的#{}与${}

    前言 还记得当初从北京回来的时候,跟着倪文杰师姐做JavaITOO的一卡通模块,我亲姐贾梦洁带着我一块做,期间,我遇到了一个特别奇葩的问题,就死我要实现Mybatis的模糊查询,根据当时亲姐教给我方法 ...

  6. Mybatis实体类的映射文件中select,insert语句使用

    id:在命名空间中唯一的标识符,可以被用来引用这条语句. parameterType:设置传入这条语句的参数的数据类型,如int,String...... resultType:设置从这条语句中返回数 ...

  7. python 网络爬虫(二) BFS不断抓URL并放到文件中

    上一篇的python 网络爬虫(一) 简单demo 还不能叫爬虫,只能说基础吧,因为它没有自动化抓链接的功能. 本篇追加如下功能: [1]广度优先搜索不断抓URL,直到队列为空 [2]把所有的URL写 ...

  8. mybatis在Mapper的xml文件中的转义字符的处理

    XML转义字符 < < 小于号 > > 大于号 & & 和 &apos; ’ 单引号 " " 双引号 用转义字符进行替换 例如 SE ...

  9. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

随机推荐

  1. 大话卷积神经网络(CNN)

      这几年深度学习快速发展,在图像识别.语音识别.物体识别等各种场景上取得了巨大的成功,例如AlphaGo击败世界围棋冠军,iPhone X内置了人脸识别解锁功能等等,很多AI产品在世界上引起了很大的 ...

  2. JavaSE——javac、javap、jad

    一.javac 用法:javac <选项> <源文件> 其中,可能的选项包括: -help                            帮助信息   -g       ...

  3. 1096: [ZJOI2007]仓库建设

    1096: [ZJOI2007]仓库建设 思路 斜率优化. 代码 #include<cstdio> #include<iostream> using namespace std ...

  4. springboot遇见问题总结

    今天开始学习创建springboot项目 问题1: 产生异常: 创建项目目录: demo代码: 代码Controller import org.springframework.web.bind.ann ...

  5. 转载: keepalived工作原理和配置说明

    转自:http://outofmemory.cn/wiki/keepalived-configuration keepalived是什么 keepalived是集群管理中保证集群高可用的一个服务软件, ...

  6. 《Cracking the Coding Interview》——第7章:数学和概率论——题目2

    2014-03-20 01:59 题目:有n只蚂蚁在正n边形的n个顶点,同时以同速率开始沿着边走.每只蚂蚁走的方向是随机的,那么这些蚂蚁至少有两只发生碰撞的概率是多少. 解法:只有所有蚂蚁都往一个方向 ...

  7. 《Cracking the Coding Interview》——第7章:数学和概率论——题目1

    2014-03-20 01:57 题目:玩篮球投篮,有两种玩法:要么1投1中,要么3投两中.你单次投篮中的概率是p,那么对于不同的p,哪种玩法胜率更高? 解法:第一种总是胜率更高,可以列不等式算算,结 ...

  8. USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】

    nocows解题报告------------------------------------------------------------------------------------------ ...

  9. python学习笔记二:流程控制

    一.if else: #!/usr/bin/python x = int(raw_input('please input:')) if x >= 90: if x >= 95: print ...

  10. 【APUE】Chapter11 Threads

    看完了APUE第三版的Chapter11 Threads,跟着书上的demo走了一遍,并且参考了这个blog(http://www.cnblogs.com/chuyuhuashi/p/4447817. ...