JFinal 整合 Shiro

(例子+源碼 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>

三、配置Shiro.ini (該文件Shiro默認讀取路徑爲classpath)   
?
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

這裏本身重寫一個MyShiroRealm,沒有使用默認的ehcache.xml,緣由是怕有緩存名稱衝突,個人echcache-shiro.xml   
?
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>

其實這樣在JFinal就整合好了Shiro,剩下的就是在開發過程當中使用Shiro了。   

下面說一下整合工程中我遇到的問題

在配置過程當中我遇到的問題是緩存異常:
起初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;
}
}


而後在JFinal配置類中添加了   


me.add(new ShiroPlugin());   


就是由於這一段代碼困擾了我很長的時間,起初覺得是本身shiro配置不對,其實在啓動項目的時候shiro已經默認啓動好了,若是JFinal再加載shiro,那麼會出現ehcache異常,提示ehcache已經存在不能建立,雖然再該問題上我話了很長的時間,但對shiro有了更深的認識,分享一下能給你帶來幫助。 
相關文章
相關標籤/搜索