【Shiro】shiro在springmvc里面的集成使用
commons-collections commons-collections 3.2.1 net.sf.ehcache ehcache-core 2.6.9 org.apache.shiro shiro-spring 1.2.3 org.apache.shiro shiro-ehcache 1.2.3 org.apache.shiro shiro-quartz 1.2.3
如果项目是hibernate的,以前的时候ehcache可能不是单例的,因为shiro里面也使用到了ehcache做缓存,和hibernate的ehcache缓存配置有冲突,所以需要对hibernate的ehcache部分做些调整,调整如下:
Xml代码
- <bean id=”sessionFactory”
- class=”org.springframework.orm.hibernate4.LocalSessionFactoryBean”>
org.hibernate.dialect.MySQLDialect true update - <!—
org.hibernate.cache.EhCacheRegionFactory - —>
- org.hibernate.cache.SingletonEhCacheRegionFactory
net.sf.ehcache.hibernate.SingletonEhCacheProvider true true true WEB-INF/classes/ehcache.xml org.springframework.orm.hibernate4.SpringSessionContext com.xxx.entity
上面红色的文字部分是需要调整的内容。
既然用到了ehcache,ehcahce.xml文件里面的配置内容如下:
Xml代码
- <?xml version=”1.0” encoding=”UTF-8”?>
- <defaultCache maxElementsInMemory=”10000” eternal=”false”
- timeToIdleSeconds=”120” timeToLiveSeconds=”120” overflowToDisk=”true” />
- <cache name=”org.hibernate.cache.UpdateTimestampsCache”
- maxElementsInMemory=”5000” eternal=”true” overflowToDisk=”true” />
- <cache name=”org.hibernate.cache.StandardQueryCache”
- maxElementsInMemory=”10000” eternal=”false” timeToLiveSeconds=”120”
- overflowToDisk=”true” />
- <cache name=”passwordRetryCache”
- maxEntriesLocalHeap=”2000”
- eternal=”false”
- timeToIdleSeconds=”3600”
- timeToLiveSeconds=”0”
- overflowToDisk=”false”
- statistics=”true”>
- <cache name=”authorizationCache”
- maxEntriesLocalHeap=”2000”
- eternal=”false”
- timeToIdleSeconds=”3600”
- timeToLiveSeconds=”0”
- overflowToDisk=”false”
- statistics=”true”>
- <cache name=”authenticationCache”
- maxEntriesLocalHeap=”2000”
- eternal=”false”
- timeToIdleSeconds=”3600”
- timeToLiveSeconds=”0”
- overflowToDisk=”false”
- statistics=”true”>
- <cache name=”shiro-activeSessionCache”
- maxEntriesLocalHeap=”2000”
- eternal=”false”
- timeToIdleSeconds=”3600”
- timeToLiveSeconds=”0”
- overflowToDisk=”false”
- statistics=”true”>
然后是web.xml文件里面加过滤器,注意要写在springmvc的filter前面
Xml代码
shiroFilter org.springframework.web.filter.DelegatingFilterProxy true targetFilterLifecycle true shiroFilter /*
然后就是shiro相关的spring配置参数文件了
Xml代码
- <?xml version=”1.0” encoding=”UTF-8”?>
- <beans xmlns=”http://www.springframework.org/schema/beans“
- xmlns:util=”http://www.springframework.org/schema/util“
- xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“
- xsi:schemaLocation=”
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
- <bean id=”credentialsMatcher”
- class=”com.shinowit.framework.security.credentials.RetryLimitSimpleCredentialsMatcher”>
- <property name=”cipherKey”
- value=”#{T(org.apache.shiro.codec.Base64).decode(‘4AvVhmFLUs0KTA3Kprsdag==’)}“/>
- <!—下面的loginUrl有两个必要条件,一个登陆校验失败以后会强制客户端redirect到这个url,
- 另外一个是登陆的表单(含有用户名及密码)必须action到这个url—>
- <!— 自定义的能够接收校验码的身份验证过滤器
- 跳转问题太他妈诡异了,不用了,自己写代码控制如何跳转了
- -->
- <!—
- —>
- /index.jsp = anon
- /validcode.jsp = anon
- /login/ = anon
- /static/** = anon
- /js/** = anon
- /img/** = anon
- /unauthorized.jsp = anon
- #/login/checklogin = authc
- /login/checklogin = anon
- /login/logoutlogout = logout
- /** = user
哦,对了,里面那个fuck那个url是用来改密码的,因为数据库里面的密码是加密的,不这么整总也不可能知道对的md5值是多少。
但愿没有忘记什么内容,挺墨迹的,不过能跑起来以后后边关于权限和安全的处理就简单多了,写写注解或者标签就搞定了,很爽
还没有评论,来说两句吧...