ehcache.js:
var ioc = {
cacheManager : {
type : "net.sf.ehcache.CacheManager",
factory : "net.sf.ehcache.CacheManager#getCacheManager",
args : ["nutzbook"] // 对应shiro.ini中指定的ehcache.xml中定义的name
}
/*
// 如果不需要shiro初始化的Ehcache, 使用下面的方式配置
cacheManager : {
type : "net.sf.ehcache.CacheManager",
factory : "net.sf.ehcache.CacheManager#create"
}
*/
};
nutzbook-mvc-chain.js:
var chain={
"default" : {
"ps" : [
"net.wendal.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'
}
};
ehcache.xml:(ehcache.xsd 这个文件没有)
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
monitoring="autodetect" dynamicConfig="true" name="nutzbook">
<cache name="shiro-activeSessionCache"
maxElementsInMemory="10000"
overflowToDisk="false"
eternal="true"
timeToLiveSeconds="0"
timeToIdleSeconds="0"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="600"/>
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="86400" timeToLiveSeconds="86400" overflowToDisk="false"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
statistics="false" />
<cache name="markdown" maxEntriesLocalHeap="1000"
maxEntriesLocalDisk="1000000" overflowToDisk="true" diskPersistent="true"
diskSpoolBufferSizeMB="20" timeToIdleSeconds="0" timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LFU"
eternal="true">
</cache>
shiro.ini:
[main]
nutzdao_realm = net.wendal.nutzbook.shiro.realm.SimpleAuthorizingRealm
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:ehcache.xml
nutzdao_realm.cacheManager = $cacheManager
authc = org.nutz.integration.shiro.SimpleAuthenticationFilter
authc.loginUrl = /user/login
logout.redirectUrl= /user/login
securityManager.cacheManager = $cacheManager
[urls]
/rs/* = anon
/user/logout = logout
/user/error = anon
/user/login = anon
/user/profile/active/mail = anon
BaseModule.java:
package net.wendal.nutzbook.module;
import net.wendal.nutzbook.service.EmailService;
import org.apache.shiro.cache.CacheManager;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.lang.random.R;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import java.util.List;
/**
* @author mrj
* @date 2017/5/9
*/
public abstract class BaseModule {
private static final Log log = Logs.get();
@Inject protected EmailService emailService;
protected byte[] emailKEY = R.sg(24).next().getBytes();
/**
* 注入同名的一个ioc对象
*/
@Inject
protected Dao dao;
@Inject
protected CacheManager cacheManager;
public BaseModule() {
}
protected QueryResult query(Class<?> klass, Condition cnd, Pager pager, String regex) {
if (pager != null && pager.getPageNumber() < 1) {
pager.setPageNumber(1);
}
List<?> roles = dao.query(klass, cnd, pager);
dao.fetchLinks(roles, null);
pager.setRecordCount(dao.count(klass, cnd));
return new QueryResult(roles, pager);
}
protected NutMap ajaxOk(Object data) {
return new NutMap().setv("ok", true).setv("data", data);
}
protected NutMap ajaxFail(String msg) {
return new NutMap().setv("ok", false).setv("msg", msg);
}
}
MainSetup.java:
package net.wendal.nutzbook;
import net.wendal.nutzbook.bean.User;
import net.wendal.nutzbook.service.AuthorityService;
import net.wendal.nutzbook.service.UserService;
import org.apache.shiro.cache.CacheManager;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.integration.quartz.NutQuartzCronJobFactory;
import org.nutz.ioc.Ioc;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;
import static org.nutz.dao.util.Pojos.log;
/**
* mrj
*/
public class MainSetup implements Setup {
// 注意是init方法,不是destroy方法
public void init(NutConfig nc) {
Ioc ioc = nc.getIoc();
Dao dao = ioc.get(Dao.class);
//Daos.migration(dao, User.class, true, false, false);
// 如果提示没有createTablesInPackage方法,请确认用了最新版的nutz,且老版本的nutz已经删除干净
Daos.createTablesInPackage(dao, "net.wendal.nutzbook", false);
// 初始化默认根用户
if (dao.count(User.class) == 0) {
UserService us = ioc.get(UserService.class);
us.add("admin", "123456");
}
/* if (dao.count(User.class) == 0) {
User user = new User();
user.setName("admin");
user.setPassword("123456");
user.setCreateTime(new Date());
user.setUpdateTime(new Date());
dao.insert(user);
}*/
// 获取NutQuartzCronJobFactory从而触发计划任务的初始化与启动
// ioc.get(NutQuartzCronJobFactory.class);
System.out.print("-----hhhh--------");
// 测试发送邮件
/* try {
HtmlEmail email = ioc.get(HtmlEmail.class);
email.setSubject("测试NutzBook");
email.setMsg("This is a test mail ... :-)" + System.currentTimeMillis());
email.addTo("3125189441@qq.com");//请务必改成您自己的邮箱啊!!!
email.buildMimeMessage();
email.sendMimeMessage();
} catch (Exception e) {
e.printStackTrace();
}*/
// 获取NutQuartzCronJobFactory从而触发计划任务的初始化与启动
//ioc.get(Scheduler.class);
ioc.get(NutQuartzCronJobFactory.class);
AuthorityService as = ioc.get(AuthorityService.class);
as.initFormPackage("net.wendal.nutzbook");
as.checkBasicRoles(dao.fetch(User.class, "admin"));
CacheManager cacheManager = ioc.get(CacheManager.class);
log.debug("Ehcache CacheManager = " + cacheManager);
}
public void destroy(NutConfig nc) {
// webapp销毁之前执行的逻辑
// 这个时候依然可以从nc取出ioc, 然后取出需要的ioc 对象进行操作
}
}