(例子+源碼 http://my.oschina.net/smile622/blog/203459) java
最近整合JFinal和Shiro遇到的問題,但願能給大家提示與幫助。 web
首先,JFinal和Shiro本人都是剛剛接觸,JFinal上手很快,但Shiro上手比較費勁,看了很長時間的文檔。
下面說一下整合JFinal配置這裏就不說了。 apache
按照官方Shiro配置
一、添加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 緩存
二、配置web.xml session
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 app
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;
}
}
|