赞
踩
基于javaweb+mysql的springboot人脸识别登录系统(java+springboot+maven+mysql)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb的SpringBoot人脸识别登录系统(java+springboot+maven+mysql)
实现的功能有:
管理员后台录入人脸信息、前台用户登录时选择人脸识别方式登录。
PS:用到了百度人脸识别的API接口,包括人脸检测、人脸识别对比等。
*本项目需要使用电脑摄像头,运行前先测试下摄像头是否正常:https://www.bchrt.com/tools/webcam-test/
* 获取指定分钟前的时间 * * @param date * @param beforeMinites * @return */ public static Date getBeforeDate(Date date, int beforeMinites) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MINUTE, -beforeMinites); return calendar.getTime(); } /** * 获取制定天数前的日期 * * @param date * @param beforeDays * @return */ public static Date getBeforeDaysDate(Date date, int beforeDays) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, -beforeDays); return calendar.getTime(); } /** * 生成唯一字符串 * * @return */ public static String generateSn() { return UUID.randomUUID().toString().toUpperCase().replace("-", ""); } public static String getMac() { String mac = ""; try { InetAddress localHost = InetAddress.getLocalHost(); byte[] hardwareAddress = NetworkInterface.getByInetAddress(localHost).getHardwareAddress(); StringBuffer sb = new StringBuffer(""); for (int i = 0; i < hardwareAddress.length; i++) {
* @param formatter * @return */ public static String getFormatterDate(Date date, String formatter) { SimpleDateFormat sdf = new SimpleDateFormat(formatter); return sdf.format(date); } /** * 判断请求是否是ajax * * @param request * @return */ public static boolean isAjax(HttpServletRequest request) { String header = request.getHeader("X-Requested-With"); if ("XMLHttpRequest".equals(header)) return true; return false; } /** * 从流读取字符串 * * @param inputStream * @return */ public static String getStringFromInputStream(InputStream inputStream) { String string = ""; try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "GB2312")); String buf = null; try { while ((buf = bufferedReader.readLine()) != null) { string += buf; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { bufferedReader.close();
return "admin/role/list"; } /** * 角色添加页面 * @param model * @return */ @RequestMapping(value="/add",method=RequestMethod.GET) public String add(Model model){ List<Menu> findAll = menuService.findAll(); model.addAttribute("topMenus",MenuUtil.getTopMenus(findAll)); model.addAttribute("secondMenus",MenuUtil.getSecondMenus(findAll)); model.addAttribute("thirdMenus",MenuUtil.getThirdMenus(findAll)); return "admin/role/add"; } /** * 角色添加表单提交处理 * @param role * @return */ @RequestMapping(value="/add",method=RequestMethod.POST) @ResponseBody public Result<Boolean> add(Role role){ //用统一验证实体方法验证是否合法 CodeMsg validate = ValidateEntityUtil.validate(role); if(validate.getCode() != CodeMsg.SUCCESS.getCode()){ return Result.error(validate); } if(roleService.save(role) == null){ return Result.error(CodeMsg.ADMIN_ROLE_ADD_ERROR); } log.info("添加角色【"+role+"】"); operaterLogService.add("添加角色【"+role.getName()+"】"); return Result.success(true); } /** * 角色编辑页面 * @param id * @param model * @return */ @RequestMapping(value="/edit",method=RequestMethod.GET) public String edit(@RequestParam(name="id",required=true)Long id,Model model){ List<Menu> findAll = menuService.findAll(); model.addAttribute("topMenus",MenuUtil.getTopMenus(findAll)); model.addAttribute("secondMenus",MenuUtil.getSecondMenus(findAll));
response.sendRedirect("/system/no_right"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } log.info("该请求符合权限要求,放行" + requestURI); return true; } } /** * session统一操作工具类 * */ public class SessionUtil { /** * 获取请求request * @return */ public static HttpServletRequest getRequest(){ ServletRequestAttributes attributes =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); return attributes == null ? null : attributes.getRequest(); } /** * 获取session * @return */ public static HttpSession getSession(){ HttpServletRequest request = getRequest(); if(request != null){ return request.getSession(); } return null; } /** * 获取指定键的值
* @return */ public static boolean isAjax(HttpServletRequest request) { String header = request.getHeader("X-Requested-With"); if ("XMLHttpRequest".equals(header)) return true; return false; } /** * 从流读取字符串 * * @param inputStream * @return */ public static String getStringFromInputStream(InputStream inputStream) { String string = ""; try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "GB2312")); String buf = null; try { while ((buf = bufferedReader.readLine()) != null) { string += buf; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (Exception e) { // TODO: handle exception } return string; } /** * 获取指定格式时间再指定分钟后的时间字符串 * * @param date * @param formatter * @param minites * @return */ public static String getFormatterDate(String date, String formatter, int minites) { SimpleDateFormat sdf = new SimpleDateFormat(formatter); String ret = null; try {
/** * 系统控制器 * */ @RequestMapping("/system") @Controller public class SystemController { @Autowired private OperaterLogService operaterLogService; @Autowired private UserService userService; @Autowired private OrderAuthService orderAuthService;
/** * 后台用户管理控制器 * */ @RequestMapping("/user") @Controller public class UserController { @Autowired private UserService userService; @Autowired private RoleService roleService; @Autowired private OperaterLogService operaterLogService; /** * 用户列表页面 * @param model * @param user * @param pageBean * @return */ @RequestMapping(value="/list") public String list(Model model,User user,PageBean<User> pageBean){ model.addAttribute("title", "用户列表"); model.addAttribute("username", user.getUsername()); model.addAttribute("pageBean", userService.findList(user, pageBean)); return "admin/user/list"; }
Result.error(CodeMsg.ADMIN_MENU_ADD_ERROR); } //数据库添加操作成功,记录日志 operaterLogService.add("编辑菜单信息【" + existMenu + "】"); return Result.success(true); } /** * 删除菜单信息 * @param request * @param id * @return */ @RequestMapping(value="/delete",method=RequestMethod.POST) @ResponseBody public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){ try { menuService.delete(id); } catch (Exception e) { return Result.error(CodeMsg.ADMIN_MENU_DELETE_ERROR); } //数据库添加操作成功,记录日志 operaterLogService.add("删除菜单信息,菜单ID【" + id + "】"); return Result.success(true); } }
if(validate.getCode() != CodeMsg.SUCCESS.getCode()){ return Result.error(validate); } if(menu.getParent() != null){ if(menu.getParent().getId() == null){ menu.setParent(null); } } Menu existMenu = menuService.find(menu.getId()); if(existMenu == null){ Result.error(CodeMsg.ADMIN_MENU_ID_ERROR); } //表示验证都通过,开始添加数据库 existMenu.setIcon(menu.getIcon()); existMenu.setName(menu.getName()); existMenu.setParent(menu.getParent()); existMenu.setSort(menu.getSort()); existMenu.setUrl(menu.getUrl()); existMenu.setButton(menu.isButton()); existMenu.setShow(menu.isShow()); if(menuService.save(existMenu) == null){ Result.error(CodeMsg.ADMIN_MENU_ADD_ERROR); } //数据库添加操作成功,记录日志 operaterLogService.add("编辑菜单信息【" + existMenu + "】"); return Result.success(true); } /** * 删除菜单信息 * @param request * @param id * @return */ @RequestMapping(value="/delete",method=RequestMethod.POST) @ResponseBody public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){ try { menuService.delete(id); } catch (Exception e) { return Result.error(CodeMsg.ADMIN_MENU_DELETE_ERROR); } //数据库添加操作成功,记录日志 operaterLogService.add("删除菜单信息,菜单ID【" + id + "】"); return Result.success(true); } }
menu.setParent(null); } } //表示验证都通过,开始添加数据库 if(menuService.save(menu) == null){ Result.error(CodeMsg.ADMIN_MENU_ADD_ERROR); } //数据库添加操作成功,记录日志 operaterLogService.add("添加菜单信息【" + menu + "】"); return Result.success(true); } /** * 菜单编辑页面 * @param model * @param id * @return */ @RequestMapping(value="/edit",method=RequestMethod.GET) public String eidt(Model model,@RequestParam(name="id",required=true)Long id){ List<Menu> findAll = menuService.findAll(); model.addAttribute("title","菜单列表"); model.addAttribute("topMenus",MenuUtil.getTopMenus(findAll)); model.addAttribute("secondMenus",MenuUtil.getSecondMenus(findAll)); model.addAttribute("menu",menuService.find(id)); return "admin/menu/edit"; } /** * 菜单编辑页面表单提交处理 * @param request * @param menu * @return */ @RequestMapping(value="/edit",method=RequestMethod.POST) @ResponseBody public Result<Boolean> edit(Menu menu){ if(menu == null){ Result.error(CodeMsg.DATA_ERROR); } if(menu.getId() == null){ Result.error(CodeMsg.ADMIN_MENU_ID_EMPTY); } //用统一验证实体方法验证是否合法 CodeMsg validate = ValidateEntityUtil.validate(menu); if(validate.getCode() != CodeMsg.SUCCESS.getCode()){ return Result.error(validate); } if(menu.getParent() != null){ if(menu.getParent().getId() == null){ menu.setParent(null); } }
if (user == null) { return Result.error(CodeMsg.DATA_ERROR); } //用统一验证实体方法验证是否合法 CodeMsg validate = ValidateEntityUtil.validate(user); if (validate.getCode() != CodeMsg.SUCCESS.getCode()) { return Result.error(validate); } //表示实体信息合法,开始验证验证码是否为空 if (StringUtils.isEmpty(cpacha)) { return Result.error(CodeMsg.CPACHA_EMPTY); } //说明验证码不为空,从session里获取验证码 Object attribute = request.getSession().getAttribute("admin_login"); if (attribute == null) { return Result.error(CodeMsg.SESSION_EXPIRED); } //表示session未失效,进一步判断用户填写的验证码是否正确 if (!cpacha.equalsIgnoreCase(attribute.toString())) { return Result.error(CodeMsg.CPACHA_ERROR); } //表示验证码正确,开始查询数据库,检验密码是否正确 User findByUsername = userService.findByUsername(user.getUsername()); //判断是否为空 if (findByUsername == null) { return Result.error(CodeMsg.ADMIN_USERNAME_NO_EXIST); } //表示用户存在,进一步对比密码是否正确 if (!findByUsername.getPassword().equals(user.getPassword())) { return Result.error(CodeMsg.ADMIN_PASSWORD_ERROR); } //表示密码正确,接下来判断用户状态是否可用 if (findByUsername.getStatus() == User.ADMIN_USER_STATUS_UNABLE) { return Result.error(CodeMsg.ADMIN_USER_UNABLE); } //检查用户所属角色状态是否可用 if (findByUsername.getRole() == null || findByUsername.getRole().getStatus() == Role.ADMIN_ROLE_STATUS_UNABLE) { return Result.error(CodeMsg.ADMIN_USER_ROLE_UNABLE); } //检查用户所属角色的权限是否存在 if (findByUsername.getRole().getAuthorities() == null || findByUsername.getRole().getAuthorities().size() == 0) { return Result.error(CodeMsg.ADMIN_USER_ROLE_AUTHORITES_EMPTY); } //检查一切符合,可以登录,将用户信息存放至session request.getSession().setAttribute(SessionConstant.SESSION_USER_LOGIN_KEY, findByUsername); //销毁session中的验证码 request.getSession().setAttribute("admin_login", null); //将登陆记录写入日志库 operaterLogService.add("用户【" + user.getUsername() + "】于【" + StringUtil.getFormatterDate(new Date(), "yyyy-MM-dd HH:mm:ss") + "】登录系统!"); log.info("用户成功登录,user = " + findByUsername); return Result.success(true); }
/** * 菜单编辑页面表单提交处理 * @param request * @param menu * @return */ @RequestMapping(value="/edit",method=RequestMethod.POST) @ResponseBody public Result<Boolean> edit(Menu menu){ if(menu == null){ Result.error(CodeMsg.DATA_ERROR); } if(menu.getId() == null){ Result.error(CodeMsg.ADMIN_MENU_ID_EMPTY); } //用统一验证实体方法验证是否合法 CodeMsg validate = ValidateEntityUtil.validate(menu); if(validate.getCode() != CodeMsg.SUCCESS.getCode()){ return Result.error(validate); } if(menu.getParent() != null){ if(menu.getParent().getId() == null){ menu.setParent(null); } } Menu existMenu = menuService.find(menu.getId()); if(existMenu == null){ Result.error(CodeMsg.ADMIN_MENU_ID_ERROR); } //表示验证都通过,开始添加数据库 existMenu.setIcon(menu.getIcon()); existMenu.setName(menu.getName()); existMenu.setParent(menu.getParent()); existMenu.setSort(menu.getSort()); existMenu.setUrl(menu.getUrl()); existMenu.setButton(menu.isButton()); existMenu.setShow(menu.isShow()); if(menuService.save(existMenu) == null){ Result.error(CodeMsg.ADMIN_MENU_ADD_ERROR); } //数据库添加操作成功,记录日志 operaterLogService.add("编辑菜单信息【" + existMenu + "】"); return Result.success(true);
String requestURI = request.getRequestURI(); HttpSession session = request.getSession(); session.setAttribute(SessionConstant.SESSION_USER_AUTH_KEY, AppConfig.ORDER_AUTH); Object attribute = session.getAttribute(SessionConstant.SESSION_USER_LOGIN_KEY); if(attribute == null){ log.info("用户还未登录或者session失效,重定向到登录页面,当前URL=" + requestURI); //首先判断是否是ajax请求 if(StringUtil.isAjax(request)){ //表示是ajax请求 try { response.setCharacterEncoding("UTF-8"); response.getWriter().write(JSON.toJSONString(CodeMsg.USER_SESSION_EXPIRED)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } //说明是普通的请求,可直接重定向到登录页面 //用户还未登录或者session失效,重定向到登录页面 try { response.sendRedirect("/system/login"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } log.info("该请求符合登录要求,放行" + requestURI); if(!StringUtil.isAjax(request)){ //若不是ajax请求,则将菜单信息放入页面模板变量 User user = (User)attribute; List<Menu> authorities = user.getRole().getAuthorities(); request.setAttribute("userTopMenus", MenuUtil.getTopMenus(authorities)); List<Menu> secondMenus = MenuUtil.getSecondMenus(user.getRole().getAuthorities()); request.setAttribute("userSecondMenus", secondMenus); request.setAttribute("userThirdMenus", MenuUtil.getChildren(MenuUtil.getMenuIdByUrl(requestURI,secondMenus),authorities)); request.setAttribute("siteName", siteConfig.getSiteName()); request.setAttribute("siteUrl", siteConfig.getSiteUrl()); } return true; } }
bufferedReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (Exception e) { // TODO: handle exception } return string; } /** * 获取指定格式时间再指定分钟后的时间字符串 * * @param date * @param formatter * @param minites * @return */ public static String getFormatterDate(String date, String formatter, int minites) { SimpleDateFormat sdf = new SimpleDateFormat(formatter); String ret = null; try { Date parse = sdf.parse(date); Calendar calendar = Calendar.getInstance(); calendar.setTime(parse); calendar.add(Calendar.MINUTE, minites); ret = sdf.format(calendar.getTime()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ret; } /** * 获取指定分钟前的时间 * * @param date * @param beforeMinites * @return */ public static Date getBeforeDate(Date date, int beforeMinites) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MINUTE, -beforeMinites); return calendar.getTime(); } /**
* @param id * @return */ @RequestMapping(value="/delete",method=RequestMethod.POST) @ResponseBody public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){ try { menuService.delete(id); } catch (Exception e) { return Result.error(CodeMsg.ADMIN_MENU_DELETE_ERROR); } //数据库添加操作成功,记录日志 operaterLogService.add("删除菜单信息,菜单ID【" + id + "】"); return Result.success(true); } }
*/ @Component public class LoginInterceptor implements HandlerInterceptor{ private Logger log = LoggerFactory.getLogger(LoginInterceptor.class); @Autowired private SiteConfig siteConfig; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){ String requestURI = request.getRequestURI(); HttpSession session = request.getSession(); session.setAttribute(SessionConstant.SESSION_USER_AUTH_KEY, AppConfig.ORDER_AUTH); Object attribute = session.getAttribute(SessionConstant.SESSION_USER_LOGIN_KEY); if(attribute == null){ log.info("用户还未登录或者session失效,重定向到登录页面,当前URL=" + requestURI); //首先判断是否是ajax请求 if(StringUtil.isAjax(request)){ //表示是ajax请求 try { response.setCharacterEncoding("UTF-8"); response.getWriter().write(JSON.toJSONString(CodeMsg.USER_SESSION_EXPIRED)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } //说明是普通的请求,可直接重定向到登录页面 //用户还未登录或者session失效,重定向到登录页面 try { response.sendRedirect("/system/login"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } log.info("该请求符合登录要求,放行" + requestURI); if(!StringUtil.isAjax(request)){ //若不是ajax请求,则将菜单信息放入页面模板变量 User user = (User)attribute; List<Menu> authorities = user.getRole().getAuthorities(); request.setAttribute("userTopMenus", MenuUtil.getTopMenus(authorities)); List<Menu> secondMenus = MenuUtil.getSecondMenus(user.getRole().getAuthorities()); request.setAttribute("userSecondMenus", secondMenus); request.setAttribute("userThirdMenus", MenuUtil.getChildren(MenuUtil.getMenuIdByUrl(requestURI,secondMenus),authorities)); request.setAttribute("siteName", siteConfig.getSiteName()); request.setAttribute("siteUrl", siteConfig.getSiteUrl()); } return true; } }
/** * 后台角色管理控制器 * */ @RequestMapping("/role") @Controller public class RoleController { private Logger log = LoggerFactory.getLogger(RoleController.class); @Autowired private MenuService menuService; @Autowired private OperaterLogService operaterLogService; @Autowired private RoleService roleService; /** * 分页搜索角色列表 * @param model * @param role * @param pageBean * @return */ @RequestMapping(value="/list") public String list(Model model,Role role,PageBean<Role> pageBean){ model.addAttribute("title", "角色列表"); model.addAttribute("name", role.getName()); model.addAttribute("pageBean", roleService.findByName(role, pageBean)); return "admin/role/list"; } /** * 角色添加页面 * @param model * @return */ @RequestMapping(value="/add",method=RequestMethod.GET) public String add(Model model){ List<Menu> findAll = menuService.findAll(); model.addAttribute("topMenus",MenuUtil.getTopMenus(findAll)); model.addAttribute("secondMenus",MenuUtil.getSecondMenus(findAll));
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。