【Shiro】shiro在springmvc里面的集成使用

傷城~ 2022-06-18 09:15 339阅读 0赞
  1. commons-collections
  2. commons-collections
  3. 3.2.1
  4. net.sf.ehcache
  5. ehcache-core
  6. 2.6.9
  7. org.apache.shiro
  8. shiro-spring
  9. 1.2.3
  10. org.apache.shiro
  11. shiro-ehcache
  12. 1.2.3
  13. org.apache.shiro
  14. shiro-quartz
  15. 1.2.3

如果项目是hibernate的,以前的时候ehcache可能不是单例的,因为shiro里面也使用到了ehcache做缓存,和hibernate的ehcache缓存配置有冲突,所以需要对hibernate的ehcache部分做些调整,调整如下:

Xml代码

  1. <bean id=”sessionFactory”
  2. class=”org.springframework.orm.hibernate4.LocalSessionFactoryBean”>
  3. org.hibernate.dialect.MySQLDialect
  4. true
  5. update
  6. <!—
  7. org.hibernate.cache.EhCacheRegionFactory
  8. —>
  9. org.hibernate.cache.SingletonEhCacheRegionFactory
  10. net.sf.ehcache.hibernate.SingletonEhCacheProvider
  11. true
  12. true
  13. true
  14. WEB-INF/classes/ehcache.xml
  15. org.springframework.orm.hibernate4.SpringSessionContext
  16. com.xxx.entity

上面红色的文字部分是需要调整的内容。

既然用到了ehcache,ehcahce.xml文件里面的配置内容如下:

Xml代码

  1. <?xml version=”1.0” encoding=”UTF-8”?>
  2. <defaultCache maxElementsInMemory=”10000” eternal=”false”
  3. timeToIdleSeconds=”120” timeToLiveSeconds=”120” overflowToDisk=”true” />
  4. <cache name=”org.hibernate.cache.UpdateTimestampsCache”
  5. maxElementsInMemory=”5000” eternal=”true” overflowToDisk=”true” />
  6. <cache name=”org.hibernate.cache.StandardQueryCache”
  7. maxElementsInMemory=”10000” eternal=”false” timeToLiveSeconds=”120”
  8. overflowToDisk=”true” />
  9. <cache name=”passwordRetryCache”
  10. maxEntriesLocalHeap=”2000”
  11. eternal=”false”
  12. timeToIdleSeconds=”3600”
  13. timeToLiveSeconds=”0”
  14. overflowToDisk=”false”
  15. statistics=”true”>
  16. <cache name=”authorizationCache”
  17. maxEntriesLocalHeap=”2000”
  18. eternal=”false”
  19. timeToIdleSeconds=”3600”
  20. timeToLiveSeconds=”0”
  21. overflowToDisk=”false”
  22. statistics=”true”>
  23. <cache name=”authenticationCache”
  24. maxEntriesLocalHeap=”2000”
  25. eternal=”false”
  26. timeToIdleSeconds=”3600”
  27. timeToLiveSeconds=”0”
  28. overflowToDisk=”false”
  29. statistics=”true”>
  30. <cache name=”shiro-activeSessionCache”
  31. maxEntriesLocalHeap=”2000”
  32. eternal=”false”
  33. timeToIdleSeconds=”3600”
  34. timeToLiveSeconds=”0”
  35. overflowToDisk=”false”
  36. statistics=”true”>

然后是web.xml文件里面加过滤器,注意要写在springmvc的filter前面

Xml代码

  1. shiroFilter
  2. org.springframework.web.filter.DelegatingFilterProxy
  3. true
  4. targetFilterLifecycle
  5. true
  6. shiroFilter
  7. /*

然后就是shiro相关的spring配置参数文件了

Xml代码

  1. <?xml version=”1.0” encoding=”UTF-8”?>
  2. <beans xmlns=”http://www.springframework.org/schema/beans“
  3. xmlns:util=”http://www.springframework.org/schema/util“
  4. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“
  5. xsi:schemaLocation=”
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
  8. <bean id=”credentialsMatcher”
  9. class=”com.shinowit.framework.security.credentials.RetryLimitSimpleCredentialsMatcher”>
  10. <property name=”cipherKey”
  11. value=”#{T(org.apache.shiro.codec.Base64).decode(‘4AvVhmFLUs0KTA3Kprsdag==’)}“/>
  12. <!—下面的loginUrl有两个必要条件,一个登陆校验失败以后会强制客户端redirect到这个url,
  13. 另外一个是登陆的表单(含有用户名及密码)必须action到这个url—>
  14. <!— 自定义的能够接收校验码的身份验证过滤器
  15. 跳转问题太他妈诡异了,不用了,自己写代码控制如何跳转了
  16. -->
  17. <!—
  18. —>
  19. /index.jsp = anon
  20. /validcode.jsp = anon
  21. /login/ = anon
  22. /static/** = anon
  23. /js/** = anon
  24. /img/** = anon
  25. /unauthorized.jsp = anon
  26. #/login/checklogin = authc
  27. /login/checklogin = anon
  28. /login/logoutlogout = logout
  29. /** = user
  30. 哦,对了,里面那个fuck那个url是用来改密码的,因为数据库里面的密码是加密的,不这么整总也不可能知道对的md5值是多少。

但愿没有忘记什么内容,挺墨迹的,不过能跑起来以后后边关于权限和安全的处理就简单多了,写写注解或者标签就搞定了,很爽

发表评论

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

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

相关阅读