关于思否技术征文:Java开发实现用户注册登录的功能

7次阅读

共计 4881 个字符,预计需要花费 13 分钟才能阅读完成。

本文参加了「思否技术征文」,欢送正在浏览的你也退出。

一、前言

在 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 文件的外围代码

public interface UserService {
    /**
     * 用户注册
     *
     * @param userId
     * @param dto
     * @throws Exception
     */
    void userRegister(Long userId, UserRegisterDTO dto) throws Exception;
    /**
     * 遗记明码
     *
     * @param userId
     * @param dto
     * @throws Exception
     */
    void updatePassword(Long userId, UpdatePasswordDTO dto) throws Exception;
    /**
     * 通过邮箱发送验证码
     *
     * @param userId
     * @param email
     * @throws BusinessException
     */
    void sendVerificationCode(Long userId, String email) throws BusinessException;
    /**
     * 通过用户名明码获取用户
     *
     * @param loginName
     * @param loginPwd
     * @return
     * @throws BusinessException
     */
    User getUser(String loginName, String loginPwd) throws BusinessException;
}

2、UserController.java 文件的外围代码

@RestController
@Slf4j
public class UserController extends BaseController {
    private final UserService userService;
    @Autowired
    public UserController(UserService userService) {this.userService = userService;}
    /**
     * 会员注册
     *
     * @param dto
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "会员注册", produces = "application/json")
    @ApiResponses({@ApiResponse(code = AjaxReturn.SUCCESS, message = "注冊胜利", response = AjaxReturn.class)})
    @PostMapping(path = {"/user-save"})
    public AjaxReturn userRegister(@ModelAttribute UserRegisterDTO dto, HttpServletRequest request) throws Exception {log.info(dto.toString());
        Long userId = getAuthentication(request);
        if (StringUtils.isBlank(dto.getMobile()) && StringUtils.isBlank(dto.getEmail())) {throw new BusinessException("请输出手机号或邮箱");
        }
        if (StringUtils.isNotBlank(dto.getMobile()) && !StringUtils.isNumeric(dto.getMobile())) {throw new BusinessException("请输出正确的手机号");
        }
        if (StringUtils.isNotBlank(dto.getEmail()) && !StringUtils.isEmail(dto.getEmail())) {throw new BusinessException("请输出正确的邮箱");
        }
        if (StringUtils.isBlank(dto.getLoginPwd())) {throw new BusinessException("password must not be null");
        }
        // 明码 MD5 加密
        dto.setLoginPwd(DigestUtils.md5Hex(dto.getLoginPwd()));
        if (StringUtils.isBlank(dto.getVerificationCode())) {throw new BusinessException("verification code must not be null");
        }
        userService.userRegister(userId, dto);
        return AjaxReturn.builder().build();
    }
    /**
     * 遗记明码
     *
     * @param dto
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "遗记明码", produces = "application/json")
    @ApiResponses({@ApiResponse(code = AjaxReturn.SUCCESS, message = "更新明码胜利", response = AjaxReturn.class)})
    @PostMapping(path = {"/user-password-forget"})
    public AjaxReturn updatePassword(@ModelAttribute UpdatePasswordDTO dto, HttpServletRequest request) throws Exception {Long userId = getAuthentication(request);
        if (StringUtils.isBlank(dto.getMobile()) && StringUtils.isBlank(dto.getEmail())) {throw new BusinessException("请输出手机号或邮箱");
        }
        if (StringUtils.isNotBlank(dto.getMobile()) && !StringUtils.isNumeric(dto.getMobile())) {throw new BusinessException("请输出正确的手机号");
        }
        if (StringUtils.isNotBlank(dto.getEmail()) && !StringUtils.isEmail(dto.getMobile())) {throw new BusinessException("请输出正确的邮箱");
        }
        if (StringUtils.isBlank(dto.getLoginPwd())) {throw new BusinessException("password must not be null");
        }
        // 明码 MD5 加密
        dto.setLoginPwd(DigestUtils.md5Hex(dto.getLoginPwd()));
        if (StringUtils.isBlank(dto.getVerificationCode())) {throw new BusinessException("verification code must not be null");
        }
        userService.updatePassword(userId, dto);
        return AjaxReturn.builder().build();
    }
    /**
     * 通过邮件发送验证码
     *
     * @param email
     * @param request
     * @return
     * @throws BusinessException
     */
    @ApiOperation(value = "通过邮件发送验证码", produces = "application/json")
    @ApiResponses({@ApiResponse(code = AjaxReturn.SUCCESS, message = "通过邮件发送验证码胜利", response = AjaxReturn.class)})
    @PostMapping(path = {"/verification-code-send"})
    public AjaxReturn sendVerificationCode(@ApiParam(name = "email", value = "邮箱", required = true) @RequestParam String email, HttpServletRequest request) throws BusinessException {Long userId = getAuthentication(request);
        userService.sendVerificationCode(userId, email);
        return AjaxReturn.builder().build();
    }
}

3、LoginController 文件

五、注意事项

1、留神代码的书写、命名标准;
2、在要害代码处加注解,不便前期保护;
3、思考控件摆放参差,注意界面好看;
4、在操作数据库的时候须要留神必要的异样解决,建设容错机制。

最初

通过上文讲述的流程步骤,就简略实现了一个比拟全面的用户注册登录的性能,尽管这个性能很广泛,然而对于 Java 开发刚入门的老手来说还是有难度的,这个命题能够作为出入 Java 开发者来作为练习的知识点,以上就是本文的全部内容,如有不妥之处,还请多多提出来。

正文完
 0