获取当前用户
获取当前用户的方法:
java中:UserUtils.getUser()
如:newbean.setFker(UserUtils.getUser());
User user = UserUtils.getUser();
jsp中: <c:if test="${fns:getUser().id==bean.createBy.id}">
/** * 获取当前用户 * @return 取不到返回 new User() */ public static User getUser(){ Principal principal = getPrincipal(); if (principal!=null){ User user = get(principal.getId()); if (user != null){ return user; } return new User(); } // 如果没有登录,则返回实例化空的User对象。 return new User(); }
/** * 获取当前登录者对象 */ public static Principal getPrincipal(){ try{ Subject subject = SecurityUtils.getSubject(); Principal principal = (Principal)subject.getPrincipal(); if (principal != null){ return principal; } }catch (UnavailableSecurityManagerException e) { }catch (InvalidSessionException e){ } return null; }