注册逻辑和mapper已完成

注册成功后事件待完成

Signed-off-by: Chenx221 <chenx221@yandex.com>
This commit is contained in:
Chenx221 2023-06-08 11:03:59 +08:00
parent 7cf12b8fc8
commit d925e15308
5 changed files with 70 additions and 4 deletions

View File

@ -15,9 +15,8 @@ public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping(path = "/signin", method = {RequestMethod.GET, RequestMethod.POST})
@RequestMapping(path = "/signin", method = {RequestMethod.GET, RequestMethod.POST}) //登录
public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model) {
// System.out.println("hello"); debug
User user = userMapper.getUserByUsername(username);
// System.out.println("username:" + username + " try-password:" + password + " real-password:"+user.getPassword());
if (user != null && user.getPassword().equals(password)) {
@ -28,4 +27,61 @@ public class UserController {
return "login";
}
}
@RequestMapping(path = "/signup", method = {RequestMethod.GET, RequestMethod.POST}) //注册
public String register(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("re-password") String repassword, Model model) {
//debug start
System.out.println("username:" + username + " password:" + password + " repassword:" + repassword);
//debug end
//检查两次输入的密码是否相同
if (!password.equals(repassword)) {
model.addAttribute("errorMessage", "两次输入的密码不一致,请重新输入。");
return "login";
}
//检查用户名是否已经存在
User user = userMapper.getUserByUsername(username);
if (user == null) {
//检查密码强度(8~32位)需要有数字和字母允许大小写英文字母数字密码常见符号
if (password.length() < 8 || password.length() > 32) {
model.addAttribute("errorMessage", "密码长度不符合要求,请重新输入。");
return "login";
} else {
//检查密码是否符合要求
boolean hasNumber = false;
boolean hasLetter = false;
boolean hasinValidChar = false;
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (c >= '0' && c <= '9') {
hasNumber = true;
} else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
hasLetter = true;
} else if (c == '!' || c == '@' || c == '#' || c == '$' || c == '%' || c == '^' || c == '&' || c == '*' || c == '.') {
int a = 1;//do nothing
} else {
hasinValidChar = true;
}
}
if (!hasNumber || !hasLetter || hasinValidChar) {
model.addAttribute("errorMessage", "密码不符合要求,请重新输入。");
return "login";
}
// start to register
user = new User(username, password);
userMapper.insertUser(user);
model.addAttribute("message", "注册成功");
}
} else {
model.addAttribute("errorMessage", "用户已存在,请重新输入。");
return "login";
}
if (user != null && user.getPassword().equals(password)) {
model.addAttribute("message", "登陆成功");
return "redirect:/dashboard"; // 重定向到 dashboard 页面
} else {
model.addAttribute("errorMessage", "用户名或密码不正确,请重新输入。");
return "login";
}
}
}

View File

@ -7,4 +7,5 @@ import org.apache.ibatis.annotations.Mapper;
public interface UserMapper {
User getUserByUsername(String username);
void insertUser(User user);
}

View File

@ -14,6 +14,11 @@ public class User {
this.password = password;
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}

View File

@ -8,5 +8,8 @@
FROM user
WHERE username = #{username}
</select>
<insert id="insertUser" parameterType="cyou.chenx221.pojo.User">
INSERT INTO user (username, password)
VALUES (#{username}, #{password})
</insert>
</mapper>

View File

@ -124,7 +124,7 @@
</div>
<div class="tab-pane fade" id="pills-register" role="tabpanel"
aria-labelledby="tab-register">
<form>
<form action="singup" method="post">
<h1 class="text-center mb-4" style="font-family: 'Noto Sans SC Light',serif">
用户注册</h1>
<div class="form-outline mb-4">
@ -182,6 +182,7 @@
</div>
</div>
</div>
<%-- todo: Modal2--%>
</main>
<footer>