package net.wendal.nutzbook.module;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.integration.shiro.SimpleShiroToken;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.Scope;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.filter.CheckSession;
import net.wendal.nutzbook.bean.User;
import net.wendal.nutzbook.bean.UserProfile;
import net.wendal.nutzbook.service.UserService;
import net.wendal.nutzbook.util.Toolkit;
@IocBean
@At("/user")
@Ok("json:{locked:'password|salt',ignoreNull:true}")
@Fail("http:500")
//@Filters(@By(type=CheckSession.class, args={"me", "/"}))
public class UserModule extends BaseModule {
@Inject
protected UserService userService;
@At
public int count() {
return dao.count(User.class);
}
@At("/")
@Ok("jsp:jsp.user.list") // 真实路径是 /WEB-INF/jsp/user/list.jsp
@RequiresUser
public void index() {
}
@At
// @Filters()
public Object login(@Param("username")String username, @Param("password")String password, @Param("captcha")String captcha,
@Attr(scope=Scope.SESSION, value="nutz_captcha")String _captcha,HttpSession session) {
NutMap re = new NutMap();
if (!Toolkit.checkCaptcha(_captcha, captcha)) {
return re.setv("ok", false).setv("msg", "验证码错误");
}
int userId = userService.fetch(username, password);
if (userId < 0) {
return re.setv("ok", false).setv("msg", "用户名或密码错误");
} else {
session.setAttribute("me", userId);
// 完成nutdao_realm后启用.
SecurityUtils.getSubject().login(new SimpleShiroToken(userId));
return re.setv("ok", true);
}
}
@GET
@At("/login")
// @Filters
@Ok("jsp:jsp.user.login") // 降内部重定向到登录jsp
public void loginPage() {}
@At("/shouquan")
@Ok("jsp:jsp.user.simple_role") // 降内部重定向到登录jsp
public void shouquan() {}
@At
@Ok(">>:/")
public void logout(HttpSession session) {
session.invalidate();
}
protected String checkUser(User user, boolean create) {
if (user == null) {
return "空对象";
}
if (create) {
if (Strings.isBlank(user.getName()) || Strings.isBlank(user.getPassword()))
return "用户名/密码不能为空";
} else {
if (Strings.isBlank(user.getPassword()))
return "密码不能为空";
}
String passwd = user.getPassword().trim();
if (6 > passwd.length() || passwd.length() > 12) {
return "密码长度错误";
}
user.setPassword(passwd);
if (create) {
int count = dao.count(User.class, Cnd.where("name", "=", user.getName()));
if (count != 0) {
return "用户名已经存在";
}
} else {
if (user.getId() < 1) {
return "用户Id非法";
}
}
if (user.getName() != null)
user.setName(user.getName().trim());
return null;
}
@At
// @RequiresUser
@RequiresPermissions("user:add")
public Object add(@Param("..")User user) {
NutMap re = new NutMap();
String msg = checkUser(user, true);
if (msg != null){
return re.setv("ok", false).setv("msg", msg);
}
user = userService.add(user.getName(), user.getPassword());
return re.setv("ok", true).setv("data", user);
}
@At
// @RequiresUser
@RequiresPermissions("user:update")
public Object update(@Param("password")String password, @Attr("me")int me) {
if (Strings.isBlank(password) || password.length() < 6)
return new NutMap().setv("ok", false).setv("msg", "密码不符合要求");
userService.updatePassword(me, password);
return new NutMap().setv("ok", true);
}
@At
@Aop(TransAop.READ_COMMITTED)
// @RequiresUser
@RequiresPermissions("user:delete")
public Object delete(@Param("id")int id, @Attr("me")int me) {
if (me == id) {
return new NutMap().setv("ok", false).setv("msg", "不能删除当前用户!!");
}
dao.delete(User.class, id); // 再严谨一些的话,需要判断是否为>0
dao.clear(UserProfile.class, Cnd.where("userId", "=", me));
return new NutMap().setv("ok", true);
}
@At
// @RequiresUser
@RequiresPermissions("user:query")
public Object query(@Param("name")String name, @Param("..")Pager pager) {
Cnd cnd = Strings.isBlank(name)? null : Cnd.where("name", "like", "%"+name+"%");
QueryResult qr = new QueryResult();
qr.setList(dao.query(User.class, cnd, pager));
pager.setRecordCount(dao.count(User.class, cnd));
qr.setPager(pager);
return qr; //默认分页是第1页,每页20条
}
@At
public Object getUserById(@Param("id")String id) {
NutMap re = new NutMap();
Cnd cnd = Cnd.where("name", "=", id);
List<User> users = dao.query(User.class, cnd);
if(users != null && users.size() > 0){
return re.setv("ok", true).setv("data", users.get(0));
}else{
return re.setv("ok", false).setv("data", null);
}
}
}
2018-04-04 10:09:49,287 org.nutz.mvc.impl.NutLoading.createChainMaker(NutLoading.java:266) DEBUG - @ChainBy(org.nutz.mvc.impl.NutActionChainMaker)
2018-04-04 10:09:49,296 org.nutz.mvc.impl.Loadings.scanModules(Loadings.java:154) DEBUG - module class location 'file:/F:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/nutzbook/WEB-INF/classes/'
2018-04-04 10:09:49,297 org.nutz.mvc.impl.Loadings.scanModuleInPackage(Loadings.java:184) DEBUG - > scan 'net.wendal.nutzbook'
2018-04-04 10:09:49,309 org.nutz.resource.Scans.scan(Scans.java:274) DEBUG - Found 15 resource by src( net/wendal/nutzbook/ ) , regex( ^.+[.]class$ )
2018-04-04 10:09:49,310 org.nutz.mvc.impl.Loadings.checkModule(Loadings.java:198) DEBUG - >> add 'net.wendal.nutzbook.module.CaptchaModule'
2018-04-04 10:09:49,310 org.nutz.mvc.impl.Loadings.checkModule(Loadings.java:198) DEBUG - >> add 'net.wendal.nutzbook.module.UserModule'
2018-04-04 10:09:49,311 org.nutz.mvc.impl.Loadings.checkModule(Loadings.java:198) DEBUG - >> add 'net.wendal.nutzbook.module.UserProfileModule'
2018-04-04 10:09:49,343 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/profile/' >> (UserProfileModule.java:45).index : UserProfile | @Ok(jsp:jsp.user.profile) @Fail(jsp:jsp.500) | by 1 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,345 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/profile/get' >> (UserProfileModule.java:50).get : UserProfile | @Ok(json:full) @Fail(jsp:jsp.500) | by 1 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,350 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/profile/update' >> (UserProfileModule.java:65).update : void | @Ok(void ) @Fail(jsp:jsp.500) | by 1 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,357 org.nutz.filepool.NutFilePool.(NutFilePool.java:23) INFO - Init file-pool by: F:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/nutzbook/WEB-INF/tmp/user_avatar [20000]
2018-04-04 10:09:49,361 org.nutz.filepool.NutFilePool.(NutFilePool.java:37) DEBUG - file-pool.home: 'F:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\nutzbook\WEB-INF\tmp\user_avatar'
2018-04-04 10:09:49,474 org.nutz.filepool.NutFilePool.(NutFilePool.java:66) INFO - file-pool.cursor: 1
2018-04-04 10:09:49,483 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/profile/avatar' >> (UserProfileModule.java:96).uploadAvatar : void | @Ok(>>:/user/profile) @Fail(jsp:jsp.500) | by 1 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,491 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/profile/avatar' >> (UserProfileModule.java:126).readAvatar : Object | @Ok(raw:jpg) @Fail(jsp:jsp.500) | by 1 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,496 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/captcha/next' >> (CaptchaModule.java:24).next : BufferedImage | @Ok(raw:png) @Fail(jsp:jsp.500) | by 0 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,504 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/' >> (UserModule.java:49).index : void | @Ok(jsp:jsp.user.list) @Fail(http:500) | by 1 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,526 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/add' >> (UserModule.java:109).add : Object | @Ok(json:{locked:'password|salt',ignoreNull:true}) @Fail(http:500) | by 1 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,527 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/count' >> (UserModule.java:43).count : int | @Ok(json:{locked:'password|salt',ignoreNull:true}) @Fail(http:500) | by 1 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,529 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/update' >> (UserModule.java:120).update : Object | @Ok(json:{locked:'password|salt',ignoreNull:true}) @Fail(http:500) | by 1 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,531 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/delete' >> (UserModule.java:129).delete : Object | @Ok(json:{locked:'password|salt',ignoreNull:true}) @Fail(http:500) | by 1 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,533 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/query' >> (UserModule.java:139).query : Object | @Ok(json:{locked:'password|salt',ignoreNull:true}) @Fail(http:500) | by 1 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,534 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/login' >> (UserModule.java:55).login : Object | @Ok(json:{locked:'password|salt',ignoreNull:true}) @Fail(http:500) | by 0 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,535 org.nutz.mvc.impl.UrlMappingImpl.print(UrlMappingImpl.java:146) DEBUG - '/user/logout' >> (UserModule.java:73).logout : void | @Ok(>>:/ ) @Fail(http:500) | by 1 Filters | (I:UTF-8/O:UTF-8)
2018-04-04 10:09:49,535 org.nutz.mvc.impl.NutLoading.evalUrlMapping(NutLoading.java:220) INFO - Found 14 module methods
2018-04-04 10:09:49,536 org.nutz.mvc.impl.NutLoading.evalLocalization(NutLoading.java:306) DEBUG - Localization: org.nutz.mvc.impl.NutMessageLoader('msg/') dft
2018-04-04 10:09:49,541 org.nutz.resource.Scans.scan(Scans.java:274) DEBUG - Found 1 resource by src( msg/ ) , regex( ^.+[.]properties$ )
2018-04-04 10:09:49,542 org.nutz.mvc.impl.NutMessageLoader.load(NutMessageLoader.java:27) DEBUG - Load Messages in 1 resource : [[NutResource[zh-CN/user.properties]]]
2018-04-04 10:09:49,544 org.nutz.mvc.impl.NutMessageLoader.load(NutMessageLoader.java:95) DEBUG - Message Loaded, size = 2
2018-04-04 10:09:49,545 org.nutz.mvc.impl.NutLoading.createSessionProvider(NutLoading.java:413) INFO - SessionBy --> org.nutz.integration.shiro.ShiroSessionProvider@1a572a33
2018-04-04 10:09:49,545 org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:274) INFO - Setup application...
2018-04-04 10:09:49,546 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'dao'<interface org.nutz.dao.Dao>
按照入门的nutzbook新加的getUserById方法和一个页面的跳转方法shouquan全部404
2018-04-04 10:17:53,434 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:110) DEBUG - Search mapping for [GET] path=/user/getUserById : NOT Action match