基于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 model
* @param operaterLog
* @param pageBean
* @return
*/
@RequestMapping(value = "/operator_log_list")
public String operatorLogList(Model model, OperaterLog operaterLog, PageBean<OperaterLog> pageBean) {
model.addAttribute("pageBean", operaterLogService.findList(operaterLog, pageBean));
model.addAttribute("operator", operaterLog.getOperator());
model.addAttribute("title", "日志列表");
return "admin/system/operator_log_list";
}
/**
* 删除操作日志,可删除多个
*
* @param ids
* @return
*/
@RequestMapping(value = "/delete_operator_log", method = RequestMethod.POST)
@ResponseBody
public Result<Boolean> delete(String ids) {
if (!StringUtils.isEmpty(ids)) {
String[] splitIds = ids.split(",");
for (String id : splitIds) {
operaterLogService.delete(Long.valueOf(id));
}
}
return Result.success(true);
}
/**
* 验证订单
*
* @param orderSn
* @param phone
* @return
*/
@RequestMapping(value = "/auth_order", method = RequestMethod.POST)
@ResponseBody
public Result<Boolean> authOrder(@RequestParam(name = "orderSn", required = true) String orderSn, @RequestParam(name = "phone", required = true) String phone) {
if (orderSn.length() < 18) {
return Result.error(CodeMsg.ORDER_SN_ERROR);
}
if (phone.length() < 11) {
return Result.error(CodeMsg.PHONE_ERROR);
}
if (!StringUtil.authOrder(orderSn, phone)) {
return Result.error(CodeMsg.ORDER_AUTH_ERROR);
}
OrderAuth orderAuth = new OrderAuth();
orderAuth.setMac(StringUtil.getMac());
/**
* 菜单编辑页面表单提交处理
* @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);
}
/**
* 删除菜单信息
model.addAttribute("title", "日志列表");
return "admin/system/operator_log_list";
}
/**
* 删除操作日志,可删除多个
*
* @param ids
* @return
*/
@RequestMapping(value = "/delete_operator_log", method = RequestMethod.POST)
@ResponseBody
public Result<Boolean> delete(String ids) {
if (!StringUtils.isEmpty(ids)) {
String[] splitIds = ids.split(",");
for (String id : splitIds) {
operaterLogService.delete(Long.valueOf(id));
}
}
return Result.success(true);
}
/**
* 验证订单
*
* @param orderSn
* @param phone
* @return
*/
@RequestMapping(value = "/auth_order", method = RequestMethod.POST)
@ResponseBody
public Result<Boolean> authOrder(@RequestParam(name = "orderSn", required = true) String orderSn, @RequestParam(name = "phone", required = true) String phone) {
if (orderSn.length() < 18) {
return Result.error(CodeMsg.ORDER_SN_ERROR);
}
if (phone.length() < 11) {
return Result.error(CodeMsg.PHONE_ERROR);
}
if (!StringUtil.authOrder(orderSn, phone)) {
return Result.error(CodeMsg.ORDER_AUTH_ERROR);
}
OrderAuth orderAuth = new OrderAuth();
orderAuth.setMac(StringUtil.getMac());
orderAuth.setOrderSn(orderSn);
orderAuth.setPhone(phone);
orderAuthService.save(orderAuth);
AppConfig.ORDER_AUTH = 1;
return Result.success(true);
}
/**
* 清空整个日志
*
* @return
br.close();
} catch (Exception e) {
// TODO: handle exception
}
}
return string;
}
}
/**
* 后台菜单管理控制器
*
*/
@RequestMapping("/menu")
@Controller
public class MenuController {
@Autowired
private MenuService menuService;
@Autowired
private OperaterLogService operaterLogService;
/**
* 菜单列表展示页面
* @param model
* @return
*/
@RequestMapping(value="/list")
public String list(Model model){
List<Menu> findAll = menuService.findAll();
model.addAttribute("title","菜单列表");
model.addAttribute("topMenus",MenuUtil.getTopMenus(findAll));
model.addAttribute("secondMenus",MenuUtil.getSecondMenus(findAll));
* @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";
}
/**
* 新增用户页面
* @param model
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String add(Model model){
model.addAttribute("roles", roleService.findAll());
return "admin/user/add";
}
/**
* 录入人脸信息
* @param model
* @return
*/
@RequestMapping(value="/add_face",method=RequestMethod.GET)
public String addFace(Model model,@RequestParam(name="id",required=true)Long id){
model.addAttribute("user", userService.find(id));
return "admin/user/add_face";
}
/**
* 录入人脸信息
* @param user
* @return
*/
@RequestMapping(value="/add_face",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> addFace(User user){
//判断是否有人脸信息
if(StringUtils.isEmpty(user.getFace())){
return Result.error(CodeMsg.ADMIN_USE_ADD_FACE_ERROR);
}
User existUser = userService.find(user.getId());
if(existUser == null){
return Result.error(CodeMsg.ADMIN_USE_NO_EXIST);
}
/**
* 修改密码表单提交
*
* @param oldPwd
* @param newPwd
* @return
*/
@RequestMapping(value = "/update_pwd", method = RequestMethod.POST)
@ResponseBody
public Result<Boolean> updatePwd(@RequestParam(name = "oldPwd", required = true) String oldPwd,
@RequestParam(name = "newPwd", required = true) String newPwd
) {
User loginedUser = SessionUtil.getLoginedUser();
if (!loginedUser.getPassword().equals(oldPwd)) {
return Result.error(CodeMsg.ADMIN_USER_UPDATE_PWD_ERROR);
}
if (StringUtils.isEmpty(newPwd)) {
return Result.error(CodeMsg.ADMIN_USER_UPDATE_PWD_EMPTY);
}
loginedUser.setPassword(newPwd);
//保存数据库
userService.save(loginedUser);
//更新session
SessionUtil.set(SessionConstant.SESSION_USER_LOGIN_KEY, loginedUser);
return Result.success(true);
}
/**
* 日志管理列表
*
* @param model
* @param operaterLog
* @param pageBean
* @return
*/
@RequestMapping(value = "/operator_log_list")
public String operatorLogList(Model model, OperaterLog operaterLog, PageBean<OperaterLog> pageBean) {
model.addAttribute("pageBean", operaterLogService.findList(operaterLog, pageBean));
model.addAttribute("operator", operaterLog.getOperator());
model.addAttribute("title", "日志列表");
return "admin/system/operator_log_list";
}
/**
* 删除操作日志,可删除多个
*
* @param ids
* @return
*/
* @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();
} catch (IOException e) {
/**
* 后台用户管理控制器
*
*/
@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";
}
/**
* @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();
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 {
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) {
* 用户列表页面
* @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";
}
/**
* 新增用户页面
* @param model
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String add(Model model){
model.addAttribute("roles", roleService.findAll());
return "admin/user/add";
}
/**
* 录入人脸信息
* @param model
* @return
*/
@RequestMapping(value="/add_face",method=RequestMethod.GET)
public String addFace(Model model,@RequestParam(name="id",required=true)Long id){
model.addAttribute("user", userService.find(id));
return "admin/user/add_face";
}
/**
* 录入人脸信息
* @param user
* @return
*/
@RequestMapping(value="/add_face",method=RequestMethod.POST)
@RequestMapping(value="delete",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){
try {
roleService.delete(id);
} catch (Exception e) {
// TODO: handle exception
return Result.error(CodeMsg.ADMIN_ROLE_DELETE_ERROR);
}
log.info("编辑角色ID【"+id+"】");
operaterLogService.add("删除角色ID【"+id+"】");
return Result.success(true);
}
}
/**
* 登录拦截器
*
*/
@Component
public class LoginInterceptor implements HandlerInterceptor{
*
* @param model
* @return
*/
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model) {
return "admin/system/login";
}
/**
* 用户登录提交表单处理方法
*
* @param request
* @param user
* @param cpacha
* @return
*/
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public Result<Boolean> login(HttpServletRequest request, User user, String cpacha) {
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);
/**
* 公用的上传类
*
*/
@RequestMapping("/upload")
@Controller
public class UploadController {
@Value("${ylrc.upload.photo.sufix}")
private String uploadPhotoSufix;
@Value("${ylrc.upload.photo.maxsize}")
private long uploadPhotoMaxSize;
@Value("${ylrc.upload.photo.path}")
private String uploadPhotoPath;//文件保存位置
private Logger log = LoggerFactory.getLogger(UploadController.class);
/**
* 图片统一上传类
* @param photo
* @return
*/
@RequestMapping(value="/upload_photo",method=RequestMethod.POST)
@ResponseBody
public Result<String> uploadPhoto(@RequestParam(name="photo",required=true)MultipartFile photo){
//判断文件类型是否是图片
String originalFilename = photo.getOriginalFilename();
//获取文件后缀
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."),originalFilename.length());
if(!uploadPhotoSufix.contains(suffix.toLowerCase())){
return Result.error(CodeMsg.UPLOAD_PHOTO_SUFFIX_ERROR);
}
if(photo.getSize()/1024 > uploadPhotoMaxSize){
CodeMsg codeMsg = CodeMsg.UPLOAD_PHOTO_ERROR;
codeMsg.setMsg("图片大小不能超过" + (uploadPhotoMaxSize/1024) + "M");
public Result<Boolean> edit(User user){
//用统一验证实体方法验证是否合法
CodeMsg validate = ValidateEntityUtil.validate(user);
if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
return Result.error(validate);
}
if(user.getRole() == null || user.getRole().getId() == null){
return Result.error(CodeMsg.ADMIN_USER_ROLE_EMPTY);
}
if(user.getId() == null || user.getId().longValue() <= 0){
return Result.error(CodeMsg.ADMIN_USE_NO_EXIST);
}
if(userService.isExistUsername(user.getUsername(), user.getId())){
return Result.error(CodeMsg.ADMIN_USERNAME_EXIST);
}
//到这说明一切符合条件,进行数据库保存
User findById = userService.find(user.getId());
//讲提交的用户信息指定字段复制到已存在的user对象中,该方法会覆盖新字段内容
BeanUtils.copyProperties(user, findById, "id","createTime","updateTime");
if(userService.save(findById) == null){
return Result.error(CodeMsg.ADMIN_USE_EDIT_ERROR);
}
operaterLogService.add("编辑用户,用户名:" + user.getUsername());
return Result.success(true);
}
/**
* 删除用户
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){
try {
userService.delete(id);
} catch (Exception e) {
return Result.error(CodeMsg.ADMIN_USE_DELETE_ERROR);
}
operaterLogService.add("添加用户,用户ID:" + id);
return Result.success(true);
}
}
}
filePath = new File(uploadPhotoPath + "/" + StringUtil.getFormatterDate(new Date(), "yyyyMMdd"));
//判断当天日期的文件夹是否存在,若不存在,则创建
if(!filePath.exists()){
//若不存在文件夹,则创建一个文件夹
filePath.mkdir();
}
String filename = StringUtil.getFormatterDate(new Date(), "yyyyMMdd") + "/" + System.currentTimeMillis() + suffix;
try {
photo.transferTo(new File(uploadPhotoPath+"/"+filename));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.info("图片上传成功,保存位置:" + uploadPhotoPath + filename);
return Result.success(filename);
}
}
/**
* 系统验证码公用控制器
*
*/
@Controller
/**
* 后台用户管理控制器
*
*/
@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";
}
/**
* 新增用户页面
* @param model
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String add(Model model){
model.addAttribute("roles", roleService.findAll());