NutzCN Logo
精华 shiro和ehcache集成
发布于 2925天前 作者 qq_8c799e25 2442 次浏览 复制 上一个帖子 下一个帖子
标签: ehcache shiro

按照nutzbook中的讲解,在做17.集成Ehcache及DaoCache 这一部分时,老是报异常:

能指点下如何配置吗?

shiro.ini:

[main]
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
nutzdao_realm = com.zhangwei.test.nutzbook.shiro.realm.NutDaoRealm
nutzdao_realm.credentialsMatcher = $sha256Matcher

authc = org.nutz.integration.shiro.CaptchaFormAuthenticationFilter
authc.loginUrl  = /user2/user3/login
logout.redirectUrl= /user2/user3/login

[urls]
/rs/*        = anon
/user2/user3/logout = logout
/user2/user3/error  = anon
/user2/user3/profile/active/mail = anon
/user2/user3/**     = authc

cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:ehcache.xml
nutzdao_realm.cacheManager = $cacheManager

#如果需要缓存shiro的Session,可以加入这些配置(不推荐)
#sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
#sessionManager.sessionDAO = $sessionDAO
#securityManager.sessionManager = $sessionManager
#
#securityManager.cacheManager = $cacheManager

ehcahe.js:

var ioc = {
        cacheManager : {
            type : "net.sf.ehcache.CacheManager",
            factory : "net.sf.ehcache.CacheManager#getCacheManager",
            args : ["nutz-book"] // 对应shiro.ini中指定的ehcache.xml中定义的name
        }
        /*      
        // 如果不需要shiro初始化的Ehcache, 使用下面的方式配置
        cacheManager : {
            type : "net.sf.ehcache.CacheManager",
            factory : "net.sf.ehcache.CacheManager#create"
        }
         */
};
十一月 18, 2016 5:13:59 下午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.apache.shiro.web.env.EnvironmentLoaderListener
java.lang.IllegalArgumentException: There is no filter with name 'org.apache.shiro.cache.ehcache.EhCacheManager' to apply to chain [cacheManager] in the pool of available Filters.  Ensure a filter with that name/path has first been registered with the addFilter method(s).
	at org.apache.shiro.web.filter.mgt.DefaultFilterChainManager.addToChain(DefaultFilterChainManager.java:265)
	at org.apache.shiro.web.filter.mgt.DefaultFilterChainManager.createChain(DefaultFilterChainManager.java:148)
	at org.apache.shiro.web.config.IniFilterChainResolverFactory.createChains(IniFilterChainResolverFactory.java:192)
	at org.apache.shiro.web.config.IniFilterChainResolverFactory.buildChains(IniFilterChainResolverFactory.java:127)
	at org.apache.shiro.web.config.IniFilterChainResolverFactory.createInstance(IniFilterChainResolverFactory.java:80)
	at org.apache.shiro.web.config.IniFilterChainResolverFactory.createInstance(IniFilterChainResolverFactory.java:43)
	at org.apache.shiro.config.IniFactorySupport.createInstance(IniFactorySupport.java:123)
	at org.apache.shiro.util.AbstractFactory.getInstance(AbstractFactory.java:47)
	at org.apache.shiro.web.env.IniWebEnvironment.createFilterChainResolver(IniWebEnvironment.java:187)
	at org.apache.shiro.web.env.IniWebEnvironment.configure(IniWebEnvironment.java:102)
	at org.apache.shiro.web.env.IniWebEnvironment.init(IniWebEnvironment.java:92)
	at org.apache.shiro.util.LifecycleUtils.init(LifecycleUtils.java:45)
	at org.apache.shiro.util.LifecycleUtils.init(LifecycleUtils.java:40)
	at org.apache.shiro.web.env.EnvironmentLoader.createEnvironment(EnvironmentLoader.java:221)
	at org.apache.shiro.web.env.EnvironmentLoader.initEnvironment(EnvironmentLoader.java:133)
	at org.apache.shiro.web.env.EnvironmentLoaderListener.contextInitialized(EnvironmentLoaderListener.java:58)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

dao.js:

var ioc = {
        conf : {
            type : "org.nutz.ioc.impl.PropertiesProxy",
            fields : {
                paths : ["custom/"]
            }
        },
        dataSource : {
            type : "com.alibaba.druid.pool.DruidDataSource",
            events : {
                create : "init",
                depose : 'close'
            },
            fields : {
                url : {java:"$conf.get('db.url')"},
                username : {java:"$conf.get('db.username')"},
                password : {java:"$conf.get('db.password')"},
                testWhileIdle : true,
                validationQuery : {java:"$conf.get('db.validationQuery')"},
                maxActive : {java:"$conf.get('db.maxActive')"},
                filters : "wall,mergeStat",
                connectionProperties : "druid.stat.slowSqlMillis=2000"
            }
        },
        dao : {
            type : "org.nutz.dao.impl.NutDao",
            args : [{refer:"dataSource"}]
        }
};

nutzbook-mvc-chain.js:

var chain={
    "default" : {
        "ps" : [
              "com.zhangwei.test.nutzbook.mvc.LogTimeProcessor",
              "org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor",
              "org.nutz.mvc.impl.processor.EncodingProcessor",
              "org.nutz.mvc.impl.processor.ModuleProcessor",
              "org.nutz.integration.shiro.NutShiroProcessor",
              "org.nutz.mvc.impl.processor.ActionFiltersProcessor",
              "org.nutz.mvc.impl.processor.AdaptorProcessor",
              "org.nutz.mvc.impl.processor.MethodInvokeProcessor",
              "org.nutz.mvc.impl.processor.ViewProcessor"
              ],
        "error" : 'org.nutz.mvc.impl.processor.FailProcessor'
    }
};
4 回复

ehcahe.xml:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
    monitoring="autodetect" dynamicConfig="true" name="nutz-book">
    <!-- <diskStore path="java.io.tmpdir/shiro-ehcache"/> -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            />
    <cache name="shiro-activeSessionCache"
           maxElementsInMemory="10000"
           overflowToDisk="true"
           eternal="true"
           timeToLiveSeconds="0"
           timeToIdleSeconds="0"
           diskPersistent="true"
           diskExpiryThreadIntervalSeconds="600"/>
</ehcache>

加错地方了,是在[main]下面加

改后的:

[main]
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:ehcache.xml
nutzdao_realm.cacheManager = $cacheManager
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
nutzdao_realm = com.zhangwei.test.nutzbook.shiro.realm.NutDaoRealm
nutzdao_realm.credentialsMatcher = $sha256Matcher
authc = org.nutz.integration.shiro.CaptchaFormAuthenticationFilter
authc.loginUrl  = /user2/user3/login
logout.redirectUrl= /user2/user3/login

[urls]
/rs/*        = anon
/user2/user3/logout = logout
/user2/user3/error  = anon
/user2/user3/profile/active/mail = anon
/user2/user3/**     = authc

上面那个异常不报了,但是又报这个异常:

2016-11-18 17:44:52,875 org.apache.shiro.web.env.EnvironmentLoader.initEnvironment(EnvironmentLoader.java:146) ERROR - Shiro environment initialization failed
java.lang.IllegalArgumentException: Line argument must contain a key and a value.  Only one string token was found.
	at org.apache.shiro.config.Ini$Section.splitKeyValue(Ini.java:542)
	at org.apache.shiro.config.Ini$Section.toMapProps(Ini.java:567)
	at org.apache.shiro.config.Ini$Section.<init>(Ini.java:464)
	at org.apache.shiro.config.Ini$Section.<init>(Ini.java:445)
	at org.apache.shiro.config.Ini.addSection(Ini.java:302)
	at org.apache.shiro.config.Ini.load(Ini.java:334)
	at org.apache.shiro.config.Ini.load(Ini.java:287)
	at org.apache.shiro.config.Ini.load(Ini.java:275)
	at org.apache.shiro.web.env.IniWebEnvironment.convertPathToIni(IniWebEnvironment.java:265)
	at org.apache.shiro.web.env.IniWebEnvironment.createIni(IniWebEnvironment.java:163)
	at org.apache.shiro.web.env.IniWebEnvironment.getDefaultIni(IniWebEnvironment.java:134)
	at org.apache.shiro.web.env.IniWebEnvironment.init(IniWebEnvironment.java:82)
	at org.apache.shiro.util.LifecycleUtils.init(LifecycleUtils.java:45)
	at org.apache.shiro.util.LifecycleUtils.init(LifecycleUtils.java:40)
	at org.apache.shiro.web.env.EnvironmentLoader.createEnvironment(EnvironmentLoader.java:221)
	at org.apache.shiro.web.env.EnvironmentLoader.initEnvironment(EnvironmentLoader.java:133)
	at org.apache.shiro.web.env.EnvironmentLoaderListener.contextInitialized(EnvironmentLoaderListener.java:58)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

格式还是有问题?

[main] 之前怎样有空格

添加回复
请先登陆
回到顶部