使用shiro和ehcache整合时候,登陆出现异常,求救

猴子 发布于 2012/02/27 18:38
阅读 31K+
收藏 1
项目情况struts2 

然后使用shiro安全框架

shiro.ini:

[main]
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
#realm
myRealm = com.haiersoft.shiro.realm.MyShiroRealm
myRealm.credentialsMatcher = $sha256Matcher

sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
securityManager.sessionManager = $sessionManager
securityManager.sessionManager.globalSessionTimeout = 360000

sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
securityManager.sessionManager.sessionDAO = $sessionDAO
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
securityManager.cacheManager = $cacheManager
然后缓存配置:

ehcache.xml

<ehcache>
<diskStore path="java.shm.tmpdir" />
<cache name="shiro-activeSessionCache" maxElementsInMemory="10000"
overflowToDisk="true" eternal="true" timeToLiveSeconds="0"
timeToIdleSeconds="0" diskPersistent="true"
diskExpiryThreadIntervalSeconds="600" />
</ehcache>

出现异常为:

HTTP Status 500 -

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.shiro.cache.CacheException: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: InputStreamConfigurationSource [stream=java.io.ByteArrayInputStream@13acb3d]

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.1 logs.

GlassFish Server Open Source Edition 3.1.1

请问有人知道如何解决吗?或者问题产生的原因吗?

以下是问题补充:

@猴子:今天出现一个情况,我之前在ehcache.xml配置过一个shirocache的缓存。但是我现在的缓存名字"shiro-activeSessionCache"。。为什么启动的时候加载缓存的时候他打的日志是这样的。 INFO] - [2012-02-28 09:11:11,495] - [Cache with name 'shiroCache' does not yet exist. Creating now.] - org.slf4j.impl.JCLLoggerAdapter.info(JCLLoggerAdapter.java:284) 信息: <2012-02-28 09:11:11,495> <INFO > <org.apache.shiro.cache.ehcache.EhCacheManager> - Cache with name 'shiroCache' does not yet exist. Creating now.(JCLLoggerAdapter.java:info:284) [INFO] - [2012-02-28 09:11:11,496] - [Added EhCache named [shiroCache]] - org.slf4j.impl.JCLLoggerAdapter.info(JCLLoggerAdapter.java:263) 信息: <2012-02-28 09:11:11,496> <INFO > <org.apache.shiro.cache.ehcache.EhCacheManager> - Added EhCache named [shiroCache](JCLLoggerAdapter.java:info:263) (2012/02/28 09:14)
加载中
0
纯天然原味酱
纯天然原味酱

你配置 cacheManager 的时候如果没有显示指定 cacheManagerConfigFile,那么shiro将使用“classpath:org/apache/shiro/cache/ehcache/ehcache.xml”;

而 hibernate 将会加载你上面配置的那个ehcache.xml。

要解决同时有两个未命名的cacheManager

重新指定shiro cacheManager.cacheManagerConfigFile 的值为你自己配置的ehcache.xml

或者

在你自己配置的ehcache.xml 的 <ehcache/> 配置节指定一个name属性即<ehcache name=“xxx”/>

0
PaulWong
PaulWong
shiroCacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
securityManager.cacheManager = $shiroCacheManager
0
猴子
猴子
这个写法和我写的一样啊,没什么区别。。我按照你写的改了,也是出现这个错误。。
0
PaulWong
PaulWong
将这个ehcache.xml 换成另外的名字:ehcache-shiro.xml 再看看哪报错。
0
lixuenong
lixuenong

是不是你配置了hibernate ehcache有二级缓存?

i十十
i十十
我是同时用了spring cache 和hibernate二级缓存,也报Another unnamed CacheManager already exists in the same VM 的错。请问你知道怎么解决吗?
0
lixuenong
lixuenong

实际上,错误的信息就提示你了CacheManager这个实例,在jvm中只能存在一个,spring cache及hibernate及shiro的都的缓存中,都用这个实例就行了

0
leh
leh

嚓,我也碰到这个问题了,也是shiro和hibernate3.x的缓存都用了,后来把shiro的缓存改成

<bean id="memoryCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />

让shiro用MemoryConstrainedCacheManager内存缓存就ok了,总之变了个缓存,但还没解决冲突问题,求正解。

0
leh
leh

我靠,终于解决了,原来是ehcache版本的问题,ehcache-core2.5.0之前的版本不会出问题,2.5.0及之后会报这个异常,我选了个ehcache-core2.4.8的好使。

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
 <version>2.4.8</version>
</dependency>

看官方的文档说明:

Versions of Ehcache before version 2.5 allowed any number of CacheManagers with the same name (same configuration resource) to exist in a JVM.

Ehcache 2.5 and higher does not allow multiple CacheManagers with the same name to exist in the same JVM. CacheManager() constructors creating non-Singleton CacheManagers can violate this rule

0
谭建纳
谭建纳

引用来自“leh”的答案

我靠,终于解决了,原来是ehcache版本的问题,ehcache-core2.5.0之前的版本不会出问题,2.5.0及之后会报这个异常,我选了个ehcache-core2.4.8的好使。

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
 <version>2.4.8</version>
</dependency>

看官方的文档说明:

Versions of Ehcache before version 2.5 allowed any number of CacheManagers with the same name (same configuration resource) to exist in a JVM.

Ehcache 2.5 and higher does not allow multiple CacheManagers with the same name to exist in the same JVM. CacheManager() constructors creating non-Singleton CacheManagers can violate this rule

此為正確的答案!非常感謝!
0
Zemo
Zemo
现在 net.sf.ehcache改为 net.sf.ehcache.internal了吗?我看到search.maven.org里面 net.sf.ehcache更新到2.6.9而 net.sf.ehcache.internal是从2.7.0开始更新的。 但是两个的用法好像不太一样。 
返回顶部
顶部