总体来说一共需要发送三次请求:
1. 访问注册页面
点击链接,发送请求,打开注册页面. ----发送第一次请求
创建LoginController,获取注册页面
//获取注册页面
@GetMapping("/register")
public String getRegisterPage(){
return "/register";
}
index.html
注册
2. 提交注册数据
通过表单提交数据. ----发送第二次请求
2.1. 导入commons-lang包
Commons Lang这一组API主要是提供一些基础的、通用的操作和处理,如自动生成toString()的结果、自动实现hashCode()和equals()方法、数组操作、枚举、日期和时间的处理等等。其中我们要用到StringUtils, 该类主要提供对字符串的操作,对null是安全的,主要提供了字符串查找,替换,分割,去空白,去掉非法字符等等操作.
org.apache.commons commons-lang3 3.11
2.2. 配置网站链接
community.path.domain=http://localhost:8080
2.3. 创建工具类
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(StandardCharsets.UTF_8));
}
}
2.4. 创建UserService完成具体业务
如果map返回为空,表示没有问题
@Service
public class UserService{
@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 register(User user){
HashMap 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;
}
// 验证用户名格式
boolean um = user.getUsername().matches("^[0-9a-zA-Z_]{2,13}$");
if(um == false){
map.put("usernameMsg", "错误, 用户名必须由数字、26个英文字母或者下划线组成, 长度在3~14之间");
return map;
}
// 验证密码格式
boolean pm = user.getPassword().matches("^[a-zA-Z0-9]{7,15}$");
if(pm == false){
map.put("passwordMsg", "错误, 密码不能含有非法字符, 长度在8-16之间");
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.setCreateTime(new Date());
userMapper.insertUser(user);
// 激活邮件
Context context = new Context();
context.setVariable("email", user.getEmail());
String url = domain + contextPath + "/activation/" + user.getId() + "/" +user.getActivationCode();
context.setVariable("url", url);
String content = templateEngine.process("/activation", context);
mailClient.sendMail(user.getEmail(), "账号激活", content);
return map;
}
}
2.5. activation.html
xxx@xxx.com, 您好!
您正在注册xx网, 这是一封激活邮件, 请点击 此链接, 激活您的账号!
2.6. 在LoginController中处理前后端交互逻辑
@PostMapping("/register")
public String register(Model model, User user){
Map map = userService.register(user);
if (map == null || map.isEmpty()){
model.addAttribute("msg", "注册成功, 邮件已发送, 请在邮件中进行激活");
model.addAttribute("target", "/index");
return "/operate-result";
} else {
model.addAttribute("usernameMsg", map.get("usernameMsg"));
model.addAttribute("passwordMsg", map.get("passwordMsg"));
model.addAttribute("emailMsg", map.get("emailMsg"));
return "/register";
}
}
2.7. operate-resul.html
2.8. register.html
以账号为例



