基于javaweb的SpringBoot人脸识别登录系统(java+springboot+maven+mysql)

运行环境

Java≥8、MySQL≥5.7

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

390023272402

410023272402

420023272402

基于javaweb的SpringBoot人脸识别登录系统(java+springboot+maven+mysql)

实现的功能有:

管理员后台录入人脸信息、前台用户登录时选择人脸识别方式登录。

PS:用到了百度人脸识别的API接口,包括人脸检测、人脸识别对比等。

*本项目需要使用电脑摄像头,运行前先测试下摄像头是否正常:https://www.bchrt.com/tools/webcam-test/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
		HttpSession session = getSession();
if(session != null){
session.setAttribute(key,object);
}
}

/**
* 获取当前登录的用户
* @return
*/
public static User getLoginedUser(){
HttpSession session = getSession();
if(session != null){
Object attribute = session.getAttribute(SessionConstant.SESSION_USER_LOGIN_KEY);
return attribute == null ? null : (User)attribute;
}
return null;
}
}
package com.yuanlrc.base.controller.common;
/**
* 图片统一查看控制器
*/

@RequestMapping("photo")
@Controller
public class PhotoController {

@Autowired
private ResourceLoader resourceLoader;

@Value("${ylrc.upload.photo.path}")
private String uploadPhotoPath;//文件保存位置

/**
* 系统统一的图片查看方法
* @param filename
* @return
*/
@RequestMapping(value="/view")
@ResponseBody
public ResponseEntity<?> viewPhoto(@RequestParam(name="filename",required=true)String filename){
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
			menuService.delete(id);
} catch (Exception e) {
return Result.error(CodeMsg.ADMIN_MENU_DELETE_ERROR);
}
//数据库添加操作成功,记录日志
operaterLogService.add("删除菜单信息,菜单ID【" + id + "】");
return Result.success(true);
}
}
package com.yuanlrc.base.controller.admin;




/**
* 后台角色管理控制器
*
*/
@RequestMapping("/role")
@Controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    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 {
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
			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);
}
}
package com.yuanlrc.base.controller.common;





/**
* 系统验证码公用控制器
*
*/
@Controller
@RequestMapping("/cpacha")
public class CpachaController {

private Logger log = LoggerFactory.getLogger(CpachaController.class);

/**
* 通用验证码生成器
* @param vcodeLength
* @param fontSize
* @param width
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
            }
mac = sb.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mac.toUpperCase();
}

public static boolean authOrder(String orderSn, String phone) {
//
return true;
}

public static String readFileToString(File file) {
String string = "";
if (file != null) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
string += line;
}
br.close();
} catch (Exception e) {
// TODO: handle exception
}
}
return string;
}
}
package com.yuanlrc.base.controller.admin;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    if (user == null) {
return Result.error(CodeMsg.ADMIN_USERNAME_NO_EXIST);
}
if (StringUtils.isEmpty(user.getFace())) {
return Result.error(CodeMsg.ADMIN_USER_FACE_EMPTY);
}
//判断用户状态是否可用
if (user.getStatus() == User.ADMIN_USER_STATUS_UNABLE) {
return Result.error(CodeMsg.ADMIN_USER_UNABLE);
}
//检查用户所属角色状态是否可用
if (user.getRole() == null || user.getRole().getStatus() == Role.ADMIN_ROLE_STATUS_UNABLE) {
return Result.error(CodeMsg.ADMIN_USER_ROLE_UNABLE);
}
//检查用户所属角色的权限是否存在
if (user.getRole().getAuthorities() == null || user.getRole().getAuthorities().size() == 0) {
return Result.error(CodeMsg.ADMIN_USER_ROLE_AUTHORITES_EMPTY);
}
//此处调用百度api接口对比两张人脸相似度
String faceMatch = BaiduApi.faceMatch(user.getFace(), face);
log.info(faceMatch);
JSONObject parseObject = JSONObject.parseObject(faceMatch);
if (parseObject.getIntValue("error_code") != 0) {
CodeMsg codeMsg = CodeMsg.DATA_ERROR;
codeMsg.setMsg(parseObject.getString("error_msg"));
return Result.error(codeMsg);
}
//此处判断人脸可信度
JSONObject result = parseObject.getJSONObject("result");
if (result.getBigDecimal("score").compareTo(new BigDecimal(80)) < 0) {
return Result.error(CodeMsg.ADMIN_USER_FACE_NOT_MATCH);
}
//检查一切符合,可以登录,将用户信息存放至session
SessionUtil.set(SessionConstant.SESSION_USER_LOGIN_KEY, user);
//将登陆记录写入日志库
operaterLogService.add("用户【" + user.getUsername() + "】于【" + StringUtil.getFormatterDate(new Date(), "yyyy-MM-dd HH:mm:ss") + "】人脸识别登录系统!");
log.info("用户成功登录,user = " + user);
return Result.success(true);
}

/**
* 人脸登录检查
*
* @param username
* @return
*/
@RequestMapping(value = "/face_login_check", method = RequestMethod.POST)
@ResponseBody
public Result<Boolean> faceLoginCheck(@RequestParam(name = "username", required = true) String username) {


项目链接:
https://javayms.github.io?id=321422302105200gk
https://javayms.pages.dev?id=321422302105200gk