仿牛客论坛之用户注册部分

本资料来源于牛客网,仅供学习和交流使用,禁止商用。

1 用户注册功能开发

1 数据库建表

DROP TABLE IF EXISTS `user`;
 SET character_set_client = utf8mb4 ;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  `salt` varchar(50) DEFAULT NULL,
  `email` varchar(100) DEFAULT NULL,
  `type` int(11) DEFAULT NULL COMMENT '0-普通用户; 1-超级管理员; 2-版主;',
  `status` int(11) DEFAULT NULL COMMENT '0-未激活; 1-已激活;',
  `activation_code` varchar(100) DEFAULT NULL,
  `header_url` varchar(200) DEFAULT NULL,
  `create_time` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `index_username` (`username`(20)),
  KEY `index_email` (`email`(20))
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8;

相关参数说明:
password 为加密后的密码
salt 为相关加密参数(目前还没研究懂)
activation_code 存放激活码
header_url 存放用户头像

2 编写用户实体类

public class User {

    private int id;
    private String username;
    private String password;
    private String salt;
    private String email;
    private int type;
    private int status;
    private String activationCode;
    private String headerUrl;
    private Date createTime;
	// 省略getter和setter方法
	// 省略toString方法
}

3 util工具包编写

生成随机字符串和编写加密算法。

public class CommunityUtil {

    // 生成随机字符串
    public static String generateUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    // MD5 加密 (每次加密的结果不会变的)
    // 密码+随机的字符串(防止我们的密码被盗)
    public static String md5(String key) {
        if(StringUtils.isBlank(key)) {
            return null;
        }
        return DigestUtils.md5DigestAsHex(key.getBytes());
    }
}

4 DAO层开发

@Mapper
public interface LoginTicketMapper {

    // 使用sql语句注解会比较直观,但是复杂的sql语句推荐使用xml
    @Insert({
            "insert into login_ticket(user_id, ticket, status, expired) ",
            "values(#{userId}, #{ticket}, #{status}, #{expired}) "
    })
    @Options(useGeneratedKeys = true, keyProperty = "id") // 是否启用主键,将主键注入到哪个属性中
    int insertLoginTicket(LoginTicket loginTicket);
}

5 service业务层开发

@Service
public class UserService implements CommunityConstant {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MailClient mailClient;

    // 模板引擎
    @Autowired
    private TemplateEngine templateEngine;

    @Value("${community.path.domain}")
    private String domain; // 域名

    @Value("${server.servlet.context-path}")
    private String contextPath; // 路径


    public User findUserById(int id) {
        return userMapper.selectById(id);
    }

    public Map<String, Object> register(User user) {
        Map<String, Object> map = new HashMap<>();
        // 空值的判断处理
        if(user == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }
        if(StringUtils.isBlank(user.getUsername())) {
            map.put("usernameMsg", "账号不能为空!");
            return map;
        }
        if(StringUtils.isBlank(user.getPassword())) {
            map.put("passwordMsg", "密码不能为空!");
            return map;
        }
        if(StringUtils.isBlank(user.getEmail())) {
            map.put("emailMsg", "邮箱不能为空!");
            return map;
        }

        // 从数据库中查询
        // 验证账号
        User u = userMapper.selectByName(user.getUsername());
        if(u != null) {
            map.put("usernameMsg", "该账号已存在");
            return map;
        }
        // 验证邮箱
        u = userMapper.selectByEmail(user.getEmail());
        if(u != null) {
            map.put("emailMsg", "该邮箱已被注册");
            return map;
        }

        // 注册用户
        // 生成随机字符串
        user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
        user.setPassword(CommunityUtil.md5(user.getPassword() + user.getSalt()));
        user.setType(0); // 通过注册方案的用户默认为普通用户
        user.setStatus(0); // 默认没有激活
        user.setActivationCode(CommunityUtil.generateUUID());
        user.setHeaderUrl(String.format("http://images.nowcoder.com/head/%dt.png", new Random().nextInt(1000)));
        user.setCreateTime(new Date());
        userMapper.insertUser(user);

        // 给用户发送激活邮件  Context为thymeleaf下的对象
        Context context = new Context();
        context.setVariable("email", user.getEmail());
        // http://localhost:8080/community/activation/101/code
        // /activation/101/code : /激活的路径/用户id/激活码
        // user.getId()是通过application配置里的mybatis.configuration.useGeneratedKeys=true获取的
        String url = domain + contextPath + "/activation/" + user.getId() + "/" + user.getActivationCode();
        context.setVariable("url", url);
        String content = templateEngine.process("/mail/activation", context);
        mailClient.sendMail(user.getEmail(), "激活账号", content);
        return map;
    }

    public int activation(int userId, String code) {
        User user = userMapper.selectById(userId);
        // 初始激活状态为0, 激活后变为1
        if(user.getStatus() == 1) {
            return ACTIVATION_REPEAT; // 重复激活
        } else if(user.getActivationCode().equals(code)) {
            // 改变用户的激活状态
            userMapper.updateStatus(userId, 1);
            return ACTIVATION_SUCCESS; // 激活成功
        } else {
            return ACTIVATION_FAILURE; // 激活失败, 比如激活码不相等
        }
    }
}

6 Controller层开发

@Controller
public class LoginController implements CommunityConstant {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserService userService;

	// 跳转到注册页面,get请求
    @RequestMapping(path = "/register", method = RequestMethod.GET)
    public String getRegisterPage() {
        return "/site/register";
    }

    /**
     * 注册: 处理前端的注册请求, 完成注册
     * @param model 
     * @param user 
     * @return 
     */
    @RequestMapping(path = "/register", method = RequestMethod.POST)
    public String register(Model model, User user) {
        Map<String, Object> map = userService.register(user);
        if(map == null || map.isEmpty()) {
            model.addAttribute("msg", "注册成功,我们已向您的邮箱发送了激活邮件,请尽快激活。");
            model.addAttribute("target", "/index");
            return "/site/operate-result";
        } else {
            // 账号、邮箱、密码中的某部分有问题,后期可以完善
            model.addAttribute("usernameMsg", map.get("usernameMsg"));
            model.addAttribute("passwordMsg", map.get("passwordMsg"));
            model.addAttribute("emailMsg", map.get("emailMsg"));
            return "/site/register";
        }
    }
}

7 前端代码处理

login.html

代码解释:在保证用户没有登录的情况下提供注册功能,通过thymeleaf模板进行路径的改写(登录功能会在后续开发,所以刚开始可以不写)

<li th:if="${loginUser==null}">
	<a th:href="@{/register}">注册</a>
</li>

register.html

<!-- 内容 -->
	<div>
		<div>
			<h3>&nbsp;&nbsp;</h3>
			<form method="post" th:action="@{/register}">
				<div>
					<label>账号:</label>
					<div>
						<!-- 此处传入name参数,记得要和user对象的name属性相对应 -->
							<input type="text" th:class="|form-control ${usernameMsg!=null?'is-invalid':''}|"
								   th:value="${user!=null?user.username:''}"
							   id="username" name="username" placeholder="请输入您的账号!" required>
						<div th:text="${usernameMsg}">
							该账号已存在!
						</div>
					</div>
				</div>
				<div>
					<label>密码:</label>
					<div>
						<input type="password" th:class="|form-control ${passwordMsg!=null?'is-invalid':''}|"
							   th:value="${user!=null?user.password:''}"
							   id="password" name="password" placeholder="请输入您的密码!" required>
						<div th:text="${passwordMsg}">
							密码长度不能小于8位!
						</div>
					</div>
				</div>
				<div>
					<label>确认密码:</label>
					<div>
						<input type="password" class="form-control" th:value="${user!=null?user.password:''}"
							   id="confirm-password" placeholder="请再次输入密码!" required>
						<div class="invalid-feedback">
							两次输入的密码不一致!
						</div>
					</div>
				</div>
				<div>
					<label>邮箱:</label>
					<div>
						<input type="email" th:class="|form-control ${emailMsg!=null?'is-invalid':''}|"
							   th:value="${user!=null?user.email:''}"
							   id="email" name="email" placeholder="请输入您的邮箱!" required>
						<div th:text="${emailMsg}">
							该邮箱已注册!
						</div>
					</div>
				</div>
				<div>
					<div></div>
					<div>
						<button type="submit">立即注册</button>
					</div>
				</div>
			</form>
		</div>
	</div>

2 用户通过邮箱激活

1 编写用户实体类

public interface CommunityConstant {

    // 激活成功
    int ACTIVATION_SUCCESS = 0;

    // ctrl shift u 大小写转换
    // 重复激活
    int ACTIVATION_REPEAT = 0;

    // 激活失败
    int ACTIVATION_FAILURE = 0;
}

2 编写service业务层

@Service
public class UserService implements CommunityConstant {
	public int activation(int userId, String code) {
        User user = userMapper.selectById(userId);
        // 初始激活状态为0, 激活后变为1
        if(user.getStatus() == 1) {
            return ACTIVATION_REPEAT; // 重复激活
        } else if(user.getActivationCode().equals(code)) {
            // 改变用户的激活状态
            userMapper.updateStatus(userId, 1);
            return ACTIVATION_SUCCESS; // 激活成功
        } else {
            return ACTIVATION_FAILURE; // 激活失败, 比如激活码不相等
        }
    }
}

3 编写controller层

    /**
     * 验证激活码状态
     * @param model
     * @param userId
     * @param code
     * @return
     */
    // http://localhost:8080/community/activation/101/code
    @RequestMapping(path = "/activation/{userId}/{code}", method = RequestMethod.GET)
    public String activation(Model model, @PathVariable("userId") int userId, @PathVariable("code") String code) {
        int result = userService.activation(userId, code);
        if (result == ACTIVATION_SUCCESS) {
            model.addAttribute("msg", "激活成功, 您的账号已经可以正常使用啦~ ");
            model.addAttribute("target", "/login");
        } else if (result == ACTIVATION_REPEAT) {
            model.addAttribute("msg", "无效操作, 该账号已经注册过了, 即将跳转到首页~ ");
            model.addAttribute("target", "/index");
        } else {
            model.addAttribute("msg", "激活失败, 您提供的激活码不正确, 即将跳转到首页~ ");
            model.addAttribute("target", "/index");
        }
        return "/site/operate-result";
    }

4 前端代码处理

<div>
		<p>
			<b th:text="${email}">xxx@xxx.com</b>, 您好!
		</p>
		<p>
			您正在注册牛客网, 这是一封激活邮件, 请点击 
			<a th:href="${url}">此链接</a>,
			激活您的牛客账号。
		</p>
	</div>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
仿牛客论坛中,Redis主要用于两个方面的功能:缓存用户信息和生成验证码。 首先,服务器会生成验证码,将这个随机字符串作为Redis的key,将生成的验证码字符串作为value存储在Redis中。这个验证码会存在客户端的cookie里,并且Redis只会保存这个验证码的信息60秒钟。 其次,Redis还用于缓存用户信息。在登录时,服务器会生成一个登录凭证,即LoginTicket,然后将这个LoginTicket存储在Redis中。每次请求时,拦截器会拦截这个LoginTicket,从Redis中获取相应的登录凭证信息。 当用户退出登录时,服务器会将这个登录凭证的状态设置为1,表示已经注销。然后将这个更新后的登录凭证存储在Redis中。 总结来说,仿牛客论坛中的Redis主要用于缓存用户信息和生成验证码。通过使用Redis,可以提高系统的性能和效率,减轻数据库的负载压力。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [4.7仿牛客社区项目——Redis优化登录模块(存验证码、登录凭证、缓存用户信息)](https://blog.csdn.net/Doreen_FF/article/details/118274468)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值