SpringBoot开发七-开发注册功能

需求介绍—开发注册功能

首先访问注册页面—点击顶部的链接,打开注册页面

提交注册数据

  • 通过表单提交数据
  • 服务端验证账号是否存在,邮箱是否已经注册
  • 服务端发送激活邮件

激活注册账号

点击邮件中的链接,访问服务端的激活服务

实现代码

按着需求一个个完成。

首先访问注册页面

只是打开页面,没有业务,访问数据库。只需要请求提交给Controller,然后调用模板,模板做出相应就可以了

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.nowcoder.community.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {
@RequestMapping(path = "/register", method = RequestMethod.GET)
public String getRegisterpage() {
return "/site/register";
}
}

然后就去处理对应的"/site/register"进行模板改造就可以了。同时你要在index.html改一下链接就好了。

效果如下:

再完成提交注册数据

首先因为在这个过程中经常会到判断字符串,集合等常用的数据空值的情况,我们引入以下包

1
2
3
4
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

然后在application.properties配置我们网站的域名:

1
2
# community
community.path.domain=http://localhost:8080

然后我们写一个工具类CommunityUtil用来提供两个方法支持我们的注册功能,比方生成随机字符串这个方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.nowcoder.community.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

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

// MD5加密 只能加密不能解密,采用加盐加密
public static String md5(String key) {
if(StringUtils.isBlank(key)) {
return null;
}
return DigestUtils.md5DigestAsHex(key.getBytes());
}
}

因为我们的注册是针对用户表的操作,所以逻辑应该写在UserService里面

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.nowcoder.community.service;

import com.nowcoder.community.dao.LoginTicketMapper;
import com.nowcoder.community.dao.UserMapper;
import com.nowcoder.community.entity.LoginTicket;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.CommunityUtil;
import com.nowcoder.community.util.MailClient;
import com.nowcoder.community.util.RedisKeyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class UserService implements CommunityConstant {
@Autowired
private UserMapper userMapper;

@Autowired
private MailClient mailClient;

@Autowired
private TemplateEngine templateEngine;

@Autowired
private RedisTemplate redisTemplate;

// 要把域名和项目名注入进来
@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;
}

// 注册用户,生成随机字符串利用MD5加密
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://image.nowcoder.com/head/%dt.png", new Random().nextInt(1000)));
user.setCreateTime(new Date());
userMapper.insertUser(user);

// 激活邮件
Context context = new Context();
context.setVariable("email", user.getEmail());
// http://localhost:8080/community/activation/101/code
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 avtivation(int userId, String code) {
User user = userMapper.selectById(userId);
if (user.getStatus() == 1) {
return ACTIVATION_REPATE;
}
else if (user.getActivationCode().equals(code)) {
userMapper.updateStatus(userId,1);
clearCache(userId);
return ACTIVATION_SUCCESS;
}
else {
return ACTIVATION_FAILURE;
}
}
}

那么业务写完了,我们需要继续在LoginController里面写前后端交互

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
package com.nowcoder.community.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {
@Autowired
private UserService userService;

@RequestMapping(path = "/register", method = RequestMethod.GET)
public String getRegisterpage() {
return "/site/register";
}

@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";
}
}
}

处理激活账号的事情

那么我们应该在UserService里增加方法。但是因为这个情况有很多的情况,比方说激活成功,重复激活,激活失败,所以我们定义一些常量接口CommunityConstant

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.nowcoder.community.util;

public interface CommunityConstant {
/**
* 激活成功
*/
int ACTIVATION_SUCCESS = 0;

/**
* 重复激活
*/
int ACTIVATION_REPATE = 1;

/**
* 激活失败
*/
int ACTIVATION_FAILURE = 2;
}

那么现在写对应的方法了

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
55
56
57
58
59
package com.nowcoder.community.service;

import com.nowcoder.community.dao.LoginTicketMapper;
import com.nowcoder.community.dao.UserMapper;
import com.nowcoder.community.entity.LoginTicket;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.CommunityUtil;
import com.nowcoder.community.util.MailClient;
import com.nowcoder.community.util.RedisKeyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class UserService implements CommunityConstant {
@Autowired
private UserMapper userMapper;

@Autowired
private MailClient mailClient;

@Autowired
private TemplateEngine templateEngine;

@Autowired
private RedisTemplate redisTemplate;

// 要把域名和项目名注入进来
@Value("${community.path.domain}")
private String domain;

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

public int avtivation(int userId, String code) {
User user = userMapper.selectById(userId);
if (user.getStatus() == 1) {
return ACTIVATION_REPATE;
}
else if (user.getActivationCode().equals(code)) {
userMapper.updateStatus(userId,1);
return ACTIVATION_SUCCESS;
}
else {
return ACTIVATION_FAILURE;
}
}
}

那么方法写好了,需要在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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.nowcoder.community.controller;

import com.google.code.kaptcha.Producer;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.CommunityUtil;
import com.nowcoder.community.util.CookieUtil;
import com.nowcoder.community.util.RedisKeyUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.imageio.ImageIO;
import javax.mail.search.SearchTerm;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
public class LoginController implements CommunityConstant {
private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

@Autowired
private UserService userService;

@Autowired
private Producer kaptchaProducer;

@Autowired
private RedisTemplate redisTemplate;

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

@RequestMapping(path = "/register", method = RequestMethod.GET)
public String getRegisterpage() {
return "/site/register";
}

@RequestMapping(path = "/login", method = RequestMethod.GET)
public String getLoginpage() {
return "/site/login";
}

@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";
}
}

// 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.avtivation(userId, code);
if (result == ACTIVATION_SUCCESS) {
model.addAttribute("msg","激活成功,您的账号已经可以正常使用了");
model.addAttribute("target","/login");
}
else if (result == ACTIVATION_REPATE) {
model.addAttribute("msg","无效的操作,该账号已经激活过了");
model.addAttribute("target","/index");
}
else {
model.addAttribute("msg","激活失败,您提供的激活码不正确");
model.addAttribute("target","/index");
}
return "/site/operate-result";
}
}