Java开发:实现用户注册登录的功能

深碍√TFBOYSˉ_ 2024-04-06 09:57 229阅读 0赞

一、前言

在Java开发过程中,实现用户的注册功能是最基本的,用户通过手机号或者邮箱作为注册账号也是非常常见的操作方式,不管是通过手机号注册或者邮箱注册,原理都差不多,那么本文就来分享一下在Java开发过程中的用户注册账号的功能实现。

二、准备工作

1、通过Java语言来实现用户注册登录的后台功能;

2、使用环境有JDK6、Eclipse、Oracle10G、Tomcat等;

三、具体实现思路及核心步骤

1、数据库设计

①数据库的表名称以及要求:

表名:users 主键:id

字段名称:id:用户id,username:用户名称,password:密码,group_id:用户类型id ②创建数据表,创建主、外键,创建序列,新加测试数据

2、使用Eclipse创建web项目UserDemo

3、给项目工程添加Spring、Hibernate等支持,并且正确引入集成到项目中,以及配置

4、创建数据持久化类,以及对应的映射文件,让用户类型和用户之间建立双向一对多的关系

5、新建接口以及实现类,使用spring数据库对象实现对应数据库的操作

6、创建service接口以及实现类,并且实现对应的业务逻辑

7、创建action类,并引入接口和访问器,完成配置文件

8、新建spring配置文件,实现对应的对象声明和配置

9、前端部分的界面搭建,以及接口联调

10、测试环节:调试运行成功之后将对应的相关数据库对象导出sql文件,以及用户注册数据的备份机制处理,完成测试,实现用户注册登录的功能。

四、核心代码

1、UserService.java文件的核心代码

  1. public interface UserService {
  2. /**
  3. * 用户注册
  4. *
  5. * @param userId
  6. * @param dto
  7. * @throws Exception
  8. */
  9. void userRegister(Long userId, UserRegisterDTO dto) throws Exception;
  10. /**
  11. * 忘记密码
  12. *
  13. * @param userId
  14. * @param dto
  15. * @throws Exception
  16. */
  17. void updatePassword(Long userId, UpdatePasswordDTO dto) throws Exception;
  18. /**
  19. * 通过邮箱发送验证码
  20. *
  21. * @param userId
  22. * @param email
  23. * @throws BusinessException
  24. */
  25. void sendVerificationCode(Long userId, String email) throws BusinessException;
  26. /**
  27. * 通过用户名密码获取用户
  28. *
  29. * @param loginName
  30. * @param loginPwd
  31. * @return
  32. * @throws BusinessException
  33. */
  34. User getUser(String loginName, String loginPwd) throws BusinessException;
  35. }

2、UserController.java文件的核心代码

  1. @RestController
  2. @Slf4j
  3. public class UserController extends BaseController {
  4. private final UserService userService;
  5. @Autowired
  6. public UserController(UserService userService) {
  7. this.userService = userService;
  8. }
  9. /**
  10. * 会员注册
  11. *
  12. * @param dto
  13. * @param request
  14. * @return
  15. * @throws Exception
  16. */
  17. @ApiOperation(value = "会员注册", produces = "application/json")
  18. @ApiResponses({@ApiResponse(code = AjaxReturn.SUCCESS, message = "注冊成功", response = AjaxReturn.class)})
  19. @PostMapping(path = {"/user-save"})
  20. public AjaxReturn userRegister(@ModelAttribute UserRegisterDTO dto, HttpServletRequest request) throws Exception {
  21. log.info(dto.toString());
  22. Long userId = getAuthentication(request);
  23. if (StringUtils.isBlank(dto.getMobile()) && StringUtils.isBlank(dto.getEmail())) {
  24. throw new BusinessException("请输入手机号或邮箱");
  25. }
  26. if (StringUtils.isNotBlank(dto.getMobile()) && !StringUtils.isNumeric(dto.getMobile())) {
  27. throw new BusinessException("请输入正确的手机号");
  28. }
  29. if (StringUtils.isNotBlank(dto.getEmail()) && !StringUtils.isEmail(dto.getEmail())) {
  30. throw new BusinessException("请输入正确的邮箱");
  31. }
  32. if (StringUtils.isBlank(dto.getLoginPwd())) {
  33. throw new BusinessException("password must not be null");
  34. }
  35. // 密码MD5加密
  36. dto.setLoginPwd(DigestUtils.md5Hex(dto.getLoginPwd()));
  37. if (StringUtils.isBlank(dto.getVerificationCode())) {
  38. throw new BusinessException("verification code must not be null");
  39. }
  40. userService.userRegister(userId, dto);
  41. return AjaxReturn.builder().build();
  42. }
  43. /**
  44. * 忘记密码
  45. *
  46. * @param dto
  47. * @param request
  48. * @return
  49. * @throws Exception
  50. */
  51. @ApiOperation(value = "忘记密码", produces = "application/json")
  52. @ApiResponses({@ApiResponse(code = AjaxReturn.SUCCESS, message = "更新密码成功", response = AjaxReturn.class)})
  53. @PostMapping(path = {"/user-password-forget"})
  54. public AjaxReturn updatePassword(@ModelAttribute UpdatePasswordDTO dto, HttpServletRequest request) throws Exception {
  55. Long userId = getAuthentication(request);
  56. if (StringUtils.isBlank(dto.getMobile()) && StringUtils.isBlank(dto.getEmail())) {
  57. throw new BusinessException("请输入手机号或邮箱");
  58. }
  59. if (StringUtils.isNotBlank(dto.getMobile()) && !StringUtils.isNumeric(dto.getMobile())) {
  60. throw new BusinessException("请输入正确的手机号");
  61. }
  62. if (StringUtils.isNotBlank(dto.getEmail()) && !StringUtils.isEmail(dto.getMobile())) {
  63. throw new BusinessException("请输入正确的邮箱");
  64. }
  65. if (StringUtils.isBlank(dto.getLoginPwd())) {
  66. throw new BusinessException("password must not be null");
  67. }
  68. // 密码MD5加密
  69. dto.setLoginPwd(DigestUtils.md5Hex(dto.getLoginPwd()));
  70. if (StringUtils.isBlank(dto.getVerificationCode())) {
  71. throw new BusinessException("verification code must not be null");
  72. }
  73. userService.updatePassword(userId, dto);
  74. return AjaxReturn.builder().build();
  75. }
  76. /**
  77. * 通过邮件发送验证码
  78. *
  79. * @param email
  80. * @param request
  81. * @return
  82. * @throws BusinessException
  83. */
  84. @ApiOperation(value = "通过邮件发送验证码", produces = "application/json")
  85. @ApiResponses({@ApiResponse(code = AjaxReturn.SUCCESS, message = "通过邮件发送验证码成功", response = AjaxReturn.class)})
  86. @PostMapping(path = {"/verification-code-send"})
  87. public AjaxReturn sendVerificationCode(@ApiParam(name = "email", value = "邮箱", required = true) @RequestParam String email, HttpServletRequest request) throws BusinessException {
  88. Long userId = getAuthentication(request);
  89. userService.sendVerificationCode(userId, email);
  90. return AjaxReturn.builder().build();
  91. }
  92. }

3、LoginController文件

b61ee6b29b7709263f050e11e002324f.jpeg

五、注意事项

1、注意代码的书写、命名规范;

2、在关键代码处加注解,方便后期维护;

3、考虑控件摆放整齐,留意界面美观;

4、在操作数据库的时候需要注意必要的异常处理,建立容错机制。

最后

通过上文讲述的流程步骤,就简单实现了一个比较全面的用户注册登录的功能,虽然这个功能很普遍,但是对于Java开发刚入门的新手来说还是有难度的,这个命题可以作为出入Java开发者来作为练习的知识点,以上就是本文的全部内容,如有不妥之处,还请多多提出来。

发表评论

表情:
评论列表 (有 0 条评论,229人围观)

还没有评论,来说两句吧...

相关阅读