(例子+源码 http://my.oschina.net/smile622/blog/203459)
最近整合JFinal和Shiro遇到的问题,希望能给你们提示与帮助。
首先,JFinal和Shiro本人都是刚刚接触,JFinal上手很快,但Shiro上手比较费劲,看了很长时间的文档。
下面说一下整合JFinal配置这里就不说了。
按照官方Shiro配置
1、添加shiro-all-1.2.1.jar 包括Shiro所依赖的jar包commons-beanutils-1.8.3.jar、commons-logging-1.1.3.jar和ehcache-core-2.6.6.jar
2、配置web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
<filter>
<filter-name>ShiroFilter</filter-name>
<filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ShiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[main]
#realm
myRealm = com.myweb.ext.shiro.MyShiroRealm
securityManager.realm = $myRealm
#cache
shiroCacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
shiroCacheManager.cacheManagerConfigFile = classpath:ehcache-shiro.xml
securityManager.cacheManager = $shiroCacheManager
#session
sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
sessionDAO.activeSessionsCacheName = shiro-activeSessionCache
sessionManager.sessionDAO = $sessionDAO
securityManager.sessionManager = $sessionManager
securityManager.sessionManager.globalSessionTimeout =360000
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<ehcache name="shiro">
<diskStore path="java.io.tmpdir/shiro-ehcache"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<cache name="myRealm.authorizationCache"
maxElementsInMemory="10000"
overflowToDisk="true"
eternal="true"
timeToLiveSeconds="0"
timeToIdleSeconds="0"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600">
</cache>
<cache name="shiro-activeSessionCache"
maxElementsInMemory="10000"
overflowToDisk="true"
eternal="true"
timeToLiveSeconds="0"
timeToIdleSeconds="0"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600"/>
</ehcache>
|
在配置过程中我遇到的问题是缓存异常:
起初Shiro与JFinal整合的时候自己写一个ShiroPlugin.class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
publicclassShiroPluginimplementsIPlugin {
@Override
publicbooleanstart() {
Factory<SecurityManager> factory =newIniSecurityManagerFactory(
"classpath:shiro.ini");
<preclass="brush:java; toolbar: true; auto-links: false;">SecurityManager securityManager<span style="font-size:9pt;line-height:1.5;"> = factory.getInstance();</span></pre>
SecurityUtils.setSecurityManager(securityManager);
returntrue;
}
@Override
publicbooleanstop() {
returnfalse;
}
}
|