Shiro-整合spring

Love The Way You Lie 2022-04-05 14:54 339阅读 0赞

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. version="2.5">
  6. <!-- needed for ContextLoaderListener -->
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:applicationContext.xml</param-value>
  10. </context-param>
  11. <!-- Bootstraps the root web application context before servlet initialization -->
  12. <listener>
  13. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  14. </listener>
  15. <!-- 配置shiro的过滤器 -->
  16. <filter>
  17. <filter-name>shiroFilter</filter-name>
  18. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  19. <init-param>
  20. <param-name>targetFilterLifecycle</param-name>
  21. <param-value>true</param-value>
  22. </init-param>
  23. </filter>
  24. <filter-mapping>
  25. <filter-name>shiroFilter</filter-name>
  26. <url-pattern>/*</url-pattern>
  27. </filter-mapping>
  28. <!-- The front controller of this Spring Web application, responsible for
  29. handling all application requests -->
  30. <servlet>
  31. <servlet-name>spring</servlet-name>
  32. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  33. <load-on-startup>1</load-on-startup>
  34. </servlet>
  35. <!-- Map all requests to the DispatcherServlet for handling -->
  36. <servlet-mapping>
  37. <servlet-name>spring</servlet-name>
  38. <url-pattern>/</url-pattern>
  39. </servlet-mapping>
  40. </web-app>

spring的配置文件

  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"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!-- 配置SecurityManager -->
  6. <bean id="securityManager"
  7. class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  8. <property name="cacheManager" ref="cacheManager" />
  9. <!-- Single realm app. If you have multiple realms, use the 'realms' property
  10. instead. -->
  11. <property name="sessionMode" value="native" />
  12. <property name="realm" ref="jdbcRealm" />
  13. </bean>
  14. <!-- 配置cacheManager
  15. 需要加入ehcache的配置文件和jar包
  16. -->
  17. <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
  18. <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
  19. </bean>
  20. <!-- Used by the SecurityManager to access security data (users, roles,
  21. etc). Many other realm implementations can be used too (PropertiesRealm,
  22. LdapRealm, etc. -->
  23. <!-- 直接使用实现了Realm接口的bean -->
  24. <bean id="jdbcRealm" class="com.atguigu.shiro.realms.ShiroRealm"></bean>
  25. <!-- ========================================================= Shiro Spring-specific integration ========================================================= -->
  26. <!-- Post processor that automatically invokes init() and destroy() methods
  27. for Spring-configured Shiro objects so you don't have to 1) specify an init-method
  28. and destroy-method attributes for every bean definition and 2) even know
  29. which Shiro objects require these methods to be called. -->
  30. <!-- 配置生命周期的后置处理器,自动调用spirng的IOC容器中shiro的方法 -->
  31. <bean id="lifecycleBeanPostProcessor"
  32. class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
  33. <!-- Enable Shiro Annotations for Spring-configured beans. Only run after
  34. the lifecycleBeanProcessor has run:
  35. 启用shiro的生命周期注解,其依赖lifecycleBeanPostProcessor,所以在配置了lifecycleBeanPostProcessor之后才能生效
  36. -->
  37. <bean
  38. class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
  39. depends-on="lifecycleBeanPostProcessor" />
  40. <bean
  41. class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
  42. <property name="securityManager" ref="securityManager" />
  43. </bean>
  44. <!--
  45. 这里的id必须于web.xml文件中配置的DelegatingFilterProxy的filter-name一样
  46. 若不一样则抛出NoSuchBeanDefinitionException
  47. 因为shiro会到spring的IOC容器中找shiroFilter对应的bean
  48. 若不一致的话也可以在fileter的初始化参数中配置targetBeanName,将这里的shiroFilter换成targetBeanName的值就可以了
  49. -->
  50. <bean id="shiroFilter"
  51. class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  52. <property name="securityManager" ref="securityManager" />
  53. <property name="loginUrl" value="/login.jsp" />
  54. <property name="successUrl" value="/list.jsp" />
  55. <property name="unauthorizedUrl" value="/unauthorized.jsp" />
  56. <!--
  57. 配置哪些页面需要保护
  58. 以及访问页面的权限
  59. 拦截器:(这里的url支持Ant风格模式)
  60. 1).anon 可以被匿名访问
  61. 2) .authc 需要认证才能访问
  62. 这里的url优先匹配
  63. -->
  64. <property name="filterChainDefinitions">
  65. <value>
  66. /login.jsp = anon
  67. # everything else requires authentication:
  68. /** = authc
  69. /list.jsp = anon
  70. </value>
  71. </property>
  72. </bean>
  73. </beans>

ehcache.xml 这里的ehcache配置文件是在下载的hibernete中的

  1. <ehcache>
  2. <!-- Sets the path to the directory where cache .data files are created.
  3. If the path is a Java System Property it is replaced by
  4. its value in the running VM.
  5. The following properties are translated:
  6. user.home - User's home directory
  7. user.dir - User's current working directory
  8. java.io.tmpdir - Default temp file path -->
  9. <diskStore path="java.io.tmpdir"/>
  10. <!--Default Cache configuration. These will applied to caches programmatically created through
  11. the CacheManager.
  12. The following attributes are required for defaultCache:
  13. maxInMemory - Sets the maximum number of objects that will be created in memory
  14. eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
  15. is never expired.
  16. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
  17. if the element is not eternal. Idle time is now - last accessed time
  18. timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
  19. if the element is not eternal. TTL is now - creation time
  20. overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
  21. has reached the maxInMemory limit.
  22. -->
  23. <defaultCache
  24. maxElementsInMemory="10000"
  25. eternal="false"
  26. timeToIdleSeconds="120"
  27. timeToLiveSeconds="120"
  28. overflowToDisk="true"
  29. />
  30. <!--Predefined caches. Add your cache configuration settings here.
  31. If you do not have a configuration for your cache a WARNING will be issued when the
  32. CacheManager starts
  33. The following attributes are required for defaultCache:
  34. name - Sets the name of the cache. This is used to identify the cache. It must be unique.
  35. maxInMemory - Sets the maximum number of objects that will be created in memory
  36. eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
  37. is never expired.
  38. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
  39. if the element is not eternal. Idle time is now - last accessed time
  40. timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
  41. if the element is not eternal. TTL is now - creation time
  42. overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
  43. has reached the maxInMemory limit.
  44. -->
  45. <!-- Sample cache named sampleCache1
  46. This cache contains a maximum in memory of 10000 elements, and will expire
  47. an element if it is idle for more than 5 minutes and lives for more than
  48. 10 minutes.
  49. If there are more than 10000 elements it will overflow to the
  50. disk cache, which in this configuration will go to wherever java.io.tmp is
  51. defined on your system. On a standard Linux system this will be /tmp"
  52. -->
  53. <cache name="sampleCache1"
  54. maxElementsInMemory="10000"
  55. eternal="false"
  56. timeToIdleSeconds="300"
  57. timeToLiveSeconds="600"
  58. overflowToDisk="true"
  59. />
  60. <!-- Sample cache named sampleCache2
  61. This cache contains 1000 elements. Elements will always be held in memory.
  62. They are not expired. -->
  63. <cache name="sampleCache2"
  64. maxElementsInMemory="1000"
  65. eternal="true"
  66. timeToIdleSeconds="0"
  67. timeToLiveSeconds="0"
  68. overflowToDisk="false"
  69. /> -->
  70. <!-- Place configuration for your caches following -->
  71. </ehcache>

发表评论

表情:
评论列表 (有 0 条评论,339人围观)

还没有评论,来说两句吧...

相关阅读