applicationContext-shiro.xml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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:util="http://www.springframework.org/schema/util"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  7. <description>zheng-upms</description>
  8. <context:property-placeholder location="classpath*:zheng-upms-shiro-*.properties"/>
  9. <!-- Shiro的Web过滤器 -->
  10. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  11. <property name="securityManager" ref="securityManager"/>
  12. <property name="loginUrl" value="${upms.loginUrl}"/>
  13. <property name="successUrl" value="${upms.successUrl}"/>
  14. <property name="unauthorizedUrl" value="${upms.unauthorizedUrl}"/>
  15. <property name="filters">
  16. <util:map>
  17. <entry key="authc" value-ref="upmsAuthenticationFilter"/>
  18. </util:map>
  19. </property>
  20. <property name="filterChainDefinitions">
  21. <value>
  22. /manage/** = upmsSessionForceLogout,authc
  23. /manage/index = user
  24. /druid/** = user
  25. /swagger-ui.html = user
  26. /resources/** = anon
  27. /** = anon
  28. </value>
  29. </property>
  30. </bean>
  31. <!-- 重写authc过滤器 -->
  32. <bean id="upmsAuthenticationFilter" class="com.zheng.upms.client.shiro.filter.UpmsAuthenticationFilter"/>
  33. <!-- 强制退出会话过滤器 -->
  34. <bean id="upmsSessionForceLogout" class="com.zheng.upms.client.shiro.filter.UpmsSessionForceLogoutFilter"/>
  35. <!-- 安全管理器 -->
  36. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  37. <property name="realms">
  38. <list><ref bean="upmsRealm"/></list>
  39. </property>
  40. <property name="sessionManager" ref="sessionManager"/>
  41. <property name="rememberMeManager" ref="rememberMeManager"/>
  42. </bean>
  43. <!-- realm实现,继承自AuthorizingRealm -->
  44. <bean id="upmsRealm" class="com.zheng.upms.client.shiro.realm.UpmsRealm"></bean>
  45. <!-- 会话管理器 -->
  46. <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
  47. <!-- 全局session超时时间 -->
  48. <property name="globalSessionTimeout" value="${upms.session.timeout}"/>
  49. <!-- sessionDAO -->
  50. <property name="sessionDAO" ref="sessionDAO"/>
  51. <property name="sessionIdCookieEnabled" value="true"/>
  52. <property name="sessionIdCookie" ref="sessionIdCookie"/>
  53. <property name="sessionValidationSchedulerEnabled" value="false"/>
  54. <property name="sessionListeners">
  55. <list><ref bean="sessionListener"/></list>
  56. </property>
  57. <property name="sessionFactory" ref="sessionFactory"/>
  58. </bean>
  59. <!-- 会话DAO,可重写,持久化session -->
  60. <bean id="sessionDAO" class="com.zheng.upms.client.shiro.session.UpmsSessionDao"/>
  61. <!-- 会话Cookie模板 -->
  62. <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
  63. <!-- 不会暴露给客户端 -->
  64. <property name="httpOnly" value="true"/>
  65. <!-- 设置Cookie的过期时间,秒为单位,默认-1表示关闭浏览器时过期Cookie -->
  66. <property name="maxAge" value="-1"/>
  67. <!-- Cookie名称 -->
  68. <property name="name" value="${upms.session.id}"/>
  69. </bean>
  70. <!-- 会话监听器 -->
  71. <bean id="sessionListener" class="com.zheng.upms.client.shiro.listener.UpmsSessionListener"/>
  72. <!-- session工厂 -->
  73. <bean id="sessionFactory" class="com.zheng.upms.client.shiro.session.UpmsSessionFactory"/>
  74. <!-- rememberMe管理器 -->
  75. <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
  76. <!-- rememberMe cookie加密的密钥 建议每个项目都不一样 默认AES算法 密钥长度(128 256 512 位)-->
  77. <property name="cipherKey" value="#{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}"/>
  78. <property name="cookie" ref="rememberMeCookie"/>
  79. </bean>
  80. <!-- rememberMe缓存cookie -->
  81. <bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
  82. <constructor-arg value="rememberMe"/>
  83. <!-- 不会暴露给客户端 -->
  84. <property name="httpOnly" value="true"/>
  85. <!-- 记住我cookie生效时间 -->
  86. <property name="maxAge" value="${upms.rememberMe.timeout}"/>
  87. </bean>
  88. <!-- 设置SecurityUtils,相当于调用SecurityUtils.setSecurityManager(securityManager) -->
  89. <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  90. <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
  91. <property name="arguments" ref="securityManager"/>
  92. </bean>
  93. <!-- 开启Shiro Spring AOP权限注解@RequiresPermissions的支持 -->
  94. <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
  95. <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
  96. <property name="securityManager" ref="securityManager"/>
  97. </bean>
  98. <!-- Shiro生命周期处理器-->
  99. <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
  100. </beans>