用户管理中创建用户功能完成

已登录用户不应能够再访问login登录页

Signed-off-by: Chenx221 <chenx221@yandex.com>
This commit is contained in:
Chenx221 2023-06-15 10:56:47 +08:00
parent 7580129273
commit 2cd656e4c0
16 changed files with 131 additions and 31 deletions

View File

@ -16,4 +16,8 @@
SELECT * SELECT *
FROM user FROM user
</select> </select>
<insert id="insertUserv2" parameterType="cyou.chenx221.pojo.User">
INSERT INTO user (username, password, roles, detail)
VALUES (#{username}, #{password}, #{roles}, #{detail})
</insert>
</mapper> </mapper>

View File

@ -26,6 +26,7 @@
<security:intercept-url pattern="/score/**" access="hasRole('admin')"/> <security:intercept-url pattern="/score/**" access="hasRole('admin')"/>
<security:intercept-url pattern="/output/**" access="hasRole('admin')"/> <security:intercept-url pattern="/output/**" access="hasRole('admin')"/>
<security:intercept-url pattern="/system/**" access="hasRole('admin')"/> <security:intercept-url pattern="/system/**" access="hasRole('admin')"/>
<security:intercept-url pattern="/user/**" access="hasRole('admin')"/>
<!-- 未登录状态下会自动跳转到/login登录页--> <!-- 未登录状态下会自动跳转到/login登录页-->
<security:form-login login-page="/login" <security:form-login login-page="/login"
default-target-url="/dashboard" default-target-url="/dashboard"

View File

@ -32,7 +32,7 @@
</a> </a>
<ul class="navbar-nav"> <ul class="navbar-nav">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">首页</a> <a class="nav-link active" aria-current="page" href="/dashboard">首页</a>
</li> </li>
<li class="nav-item dropdown"> <li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown1" role="button" <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown1" role="button"
@ -85,7 +85,7 @@
<ul class="dropdown-menu" aria-labelledby="navbarDropdown4"> <ul class="dropdown-menu" aria-labelledby="navbarDropdown4">
<li><a class="dropdown-item" href="#">个人设定</a></li> <li><a class="dropdown-item" href="#">个人设定</a></li>
<li><a class="dropdown-item" href="/system/settings">系统设定</a></li> <li><a class="dropdown-item" href="/system/settings">系统设定</a></li>
<li><a class="dropdown-item" href="#">用户管理</a></li> <li><a class="dropdown-item" href="/user/usermanage">用户管理</a></li>
<li><a class="dropdown-item" href="#">日志管理</a></li> <li><a class="dropdown-item" href="#">日志管理</a></li>
<li><a class="dropdown-item" href="#">版本信息</a></li> <li><a class="dropdown-item" href="#">版本信息</a></li>
</ul> </ul>

View File

@ -1,3 +1,4 @@
<%@ page import="cyou.chenx221.helper.UsernameHelper" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html> <!DOCTYPE html>
@ -19,7 +20,13 @@
</head> </head>
<body style="font-family: 'Noto Sans SC Regular',serif"> <body style="font-family: 'Noto Sans SC Regular',serif">
<%
if(new UsernameHelper().getCurrentUsername()!=null){
//redirect /dashboard
//note: 登陆了的就别乱访问了
response.sendRedirect("dashboard");
}
%>
<header> <header>
<nav class="navbar navbar-expand-lg navbar-light bg-light" style="z-index: 1;min-height: 58.59px"> <nav class="navbar navbar-expand-lg navbar-light bg-light" style="z-index: 1;min-height: 58.59px">
<div class="container"> <div class="container">
@ -227,6 +234,7 @@
$('#exampleModal2').modal('show'); $('#exampleModal2').modal('show');
</c:if> </c:if>
}); });
</script> </script>
</body> </body>
</html> </html>

View File

@ -199,7 +199,7 @@
<div class="tab-pane fade show active" id="ex1-tabs-1" role="tabpanel" <div class="tab-pane fade show active" id="ex1-tabs-1" role="tabpanel"
aria-labelledby="ex1-tab-1"> aria-labelledby="ex1-tab-1">
<div class="card-body" style="min-width: 175px"> <div class="card-body" style="min-width: 175px">
<form action="" method="post"> <form action="create" method="post">
<div class="row mb-2"> <div class="row mb-2">
<div class="col-12"> <div class="col-12">
<div class="form-outline"> <div class="form-outline">
@ -231,7 +231,7 @@
<div class="col-12"> <div class="col-12">
<div class="form-outline"> <div class="form-outline">
<input type="number" id="typeText4" class="form-control" <input type="number" id="typeText4" class="form-control"
name="roleid"/> name="roleid" disabled/>
<label class="form-label" for="typeText4">身份ID</label> <label class="form-label" for="typeText4">身份ID</label>
</div> </div>
</div> </div>
@ -276,7 +276,7 @@
<div class="form-outline"> <div class="form-outline">
<input type="password" id="typeText6" class="form-control" <input type="password" id="typeText6" class="form-control"
name="password" required/> name="password" required/>
<label class="form-label" for="typeText6">新的密码</label> <label class="form-label" for="typeText6">新的密码(必填)</label>
</div> </div>
</div> </div>
</div> </div>
@ -285,7 +285,7 @@
<div class="form-outline"> <div class="form-outline">
<input type="password" id="typeText7" class="form-control" <input type="password" id="typeText7" class="form-control"
name="re_password" required/> name="re_password" required/>
<label class="form-label" for="typeText7">重复密码</label> <label class="form-label" for="typeText7">重复密码(必填)</label>
</div> </div>
</div> </div>
</div> </div>
@ -376,9 +376,17 @@
function resetPWD(id) { function resetPWD(id) {
$('#typeText5').val(id); $('#typeText5').val(id);
const triggerEl = document.querySelector('#ex1 a[href="#密码重置"]'); $('#ex1-tab-2').tab('show');
mdb.Tab.getInstance(triggerEl).show(); // Select tab by name
} }
// 监听权限组选择框的变化
document.getElementById('role-select').addEventListener('change', function() {
var roleSelect = document.getElementById('role-select');
var typeText4 = document.getElementById('typeText4');
// 如果选择的是管理员权限组则禁用身份ID输入框否则启用身份ID输入框
typeText4.disabled = roleSelect.value === 'admin';
});
</script> </script>
</body> </body>

View File

@ -1,5 +1,6 @@
package cyou.chenx221.controller; package cyou.chenx221.controller;
import cyou.chenx221.helper.UsernameHelper;
import cyou.chenx221.mapper.UserMapper; import cyou.chenx221.mapper.UserMapper;
import cyou.chenx221.pojo.Password; import cyou.chenx221.pojo.Password;
import cyou.chenx221.pojo.User; import cyou.chenx221.pojo.User;
@ -27,9 +28,6 @@ public class UserController {
@RequestMapping(path = "/signup", method = {RequestMethod.POST}) //注册 @RequestMapping(path = "/signup", method = {RequestMethod.POST}) //注册
public String register(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("re-password") String repassword, Model model) throws NoSuchAlgorithmException { public String register(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("re-password") String repassword, Model model) throws NoSuchAlgorithmException {
//debug start
// System.out.println("username:" + username);
//debug end
model.addAttribute("comeFrom", "reg"); model.addAttribute("comeFrom", "reg");
//检查两次输入的密码是否相同 //检查两次输入的密码是否相同
if (!password.equals(repassword)) { if (!password.equals(repassword)) {
@ -81,8 +79,71 @@ public class UserController {
@RequestMapping(path = "/user/usermanage", method = {RequestMethod.GET}) @RequestMapping(path = "/user/usermanage", method = {RequestMethod.GET})
public String userManage(Model model) { public String userManage(Model model) {
String username = new UsernameHelper().getCurrentUsername();
if (username != null) {
model.addAttribute("username", username);
}
List<User> userList = userMapper.getAllUsers(); List<User> userList = userMapper.getAllUsers();
model.addAttribute("userList", userList); model.addAttribute("userList", userList);
return "usermanage"; return "usermanage";
} }
@RequestMapping(path = "/user/create", method = {RequestMethod.POST})
public String createUser(@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestParam("re_password") String repassword,
@RequestParam(value = "roleid", defaultValue = "-1", required = false) int roleid,
@RequestParam("role") String role,
Model model) throws NoSuchAlgorithmException {
//check
if (!password.equals(repassword)) {
model.addAttribute("errorMessage", "两次输入的密码不一致,请重新输入。");
return "redirect:/user/usermanage";
}
User user = userMapper.getUserByUsername(username); //check if user exists
if (user == null) {
//检查密码强度(8~32位)需要有数字和字母允许大小写英文字母数字密码常见符号
if (password.length() < 8 || password.length() > 32) {
model.addAttribute("errorMessage", "密码长度不符合要求,请重新输入。");
return "redirect:/user/usermanage";
} 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", "密码强度不符合要求请重新输入。要求8~32位需要有数字和字母允许大小写英文字母、数字、密码常见符号");
//FAQ: 开头测试用的账户的密码可能还真不符合要求
return "redirect:/user/usermanage";
}
// start to create
Password pw = new Password(password);
System.out.println("username:" + username + " password:" + pw.getEncryptedPassword() + " role:" + role + " roleid:" + roleid); //debug
user = new User(username, pw.getEncryptedPassword(), role, (roleid==-1?null:roleid));
int status_code = userMapper.insertUserv2(user);
if (status_code == 0) {
model.addAttribute("errorMessage", "创建用户失败,请重试。");
} else {
model.addAttribute("successMessage", "创建成功");
}
return "redirect:/user/usermanage";
}
} else {
model.addAttribute("errorMessage", "用户已存在,请重新输入。");
return "redirect:/user/usermanage";
}
}
} }

View File

@ -12,4 +12,7 @@ public interface UserMapper {
void insertUser(User user); void insertUser(User user);
List<User> getAllUsers(); List<User> getAllUsers();
int insertUserv2(User user);
} }

View File

@ -5,7 +5,7 @@ public class User {
private String username; private String username;
private String password; private String password;
private String roles; private String roles;
private int detail;//对应身份的id private Integer detail;//对应身份的id // roleid
private int disabled;//是否被禁用 private int disabled;//是否被禁用
private boolean disabled_str; private boolean disabled_str;
@ -26,18 +26,18 @@ public class User {
this.disabled_str = (disabled != 0); this.disabled_str = (disabled != 0);
} }
public User(String username, String password, String roles, int detail) { public User(String username, String password, String roles, Integer detail) {
this.username = username; this.username = username;
this.password = password; this.password = password;
this.roles = roles; this.roles = roles;
this.detail = detail; this.detail = detail;
} }
public int getDetail() { public Integer getDetail() {
return detail; return detail;
} }
public void setDetail(int detail) { public void setDetail(Integer detail) {
this.detail = detail; this.detail = detail;
} }

View File

@ -16,4 +16,8 @@
SELECT * SELECT *
FROM user FROM user
</select> </select>
<insert id="insertUserv2" parameterType="cyou.chenx221.pojo.User">
INSERT INTO user (username, password, roles, detail)
VALUES (#{username}, #{password}, #{roles}, #{detail})
</insert>
</mapper> </mapper>

View File

@ -26,6 +26,7 @@
<security:intercept-url pattern="/score/**" access="hasRole('admin')"/> <security:intercept-url pattern="/score/**" access="hasRole('admin')"/>
<security:intercept-url pattern="/output/**" access="hasRole('admin')"/> <security:intercept-url pattern="/output/**" access="hasRole('admin')"/>
<security:intercept-url pattern="/system/**" access="hasRole('admin')"/> <security:intercept-url pattern="/system/**" access="hasRole('admin')"/>
<security:intercept-url pattern="/user/**" access="hasRole('admin')"/>
<!-- 未登录状态下会自动跳转到/login登录页--> <!-- 未登录状态下会自动跳转到/login登录页-->
<security:form-login login-page="/login" <security:form-login login-page="/login"
default-target-url="/dashboard" default-target-url="/dashboard"

View File

@ -32,7 +32,7 @@
</a> </a>
<ul class="navbar-nav"> <ul class="navbar-nav">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">首页</a> <a class="nav-link active" aria-current="page" href="/dashboard">首页</a>
</li> </li>
<li class="nav-item dropdown"> <li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown1" role="button" <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown1" role="button"
@ -44,17 +44,11 @@
<li><a class="dropdown-item" href="/student/queryinfo">学生信息查询</a></li> <li><a class="dropdown-item" href="/student/queryinfo">学生信息查询</a></li>
<li><a class="dropdown-item" href="/student/modstep1">学生信息修改(旧)</a></li> <li><a class="dropdown-item" href="/student/modstep1">学生信息修改(旧)</a></li>
<li><a class="dropdown-item" href="/student/infomodv2">学生信息修改</a></li> <li><a class="dropdown-item" href="/student/infomodv2">学生信息修改</a></li>
<li><a class="dropdown-item disabled"><del>自己输入sql语句查</del>(划掉,没做)</a></li>
<li> <li>
<hr class="dropdown-divider" /> <hr class="dropdown-divider" />
</li> </li>
<li><a class="dropdown-item" href="/score/all">成绩查看</a></li> <li><a class="dropdown-item" href="/score/all">成绩查看</a></li>
<li><a class="dropdown-item" href="/score/infomodv2">成绩管理</a></li> <li><a class="dropdown-item" href="/score/infomodv2">成绩管理</a></li>
<li><a class="dropdown-item" href="#">学生成绩管理</a></li>
<li>
<hr class="dropdown-divider" />
</li>
<li><a class="dropdown-item" href="#">学生选课管理</a></li>
</ul> </ul>
</li> </li>
<li class="nav-item dropdown"> <li class="nav-item dropdown">
@ -85,7 +79,7 @@
<ul class="dropdown-menu" aria-labelledby="navbarDropdown4"> <ul class="dropdown-menu" aria-labelledby="navbarDropdown4">
<li><a class="dropdown-item" href="#">个人设定</a></li> <li><a class="dropdown-item" href="#">个人设定</a></li>
<li><a class="dropdown-item" href="/system/settings">系统设定</a></li> <li><a class="dropdown-item" href="/system/settings">系统设定</a></li>
<li><a class="dropdown-item" href="#">用户管理</a></li> <li><a class="dropdown-item" href="/user/usermanage">用户管理</a></li>
<li><a class="dropdown-item" href="#">日志管理</a></li> <li><a class="dropdown-item" href="#">日志管理</a></li>
<li><a class="dropdown-item" href="#">版本信息</a></li> <li><a class="dropdown-item" href="#">版本信息</a></li>
</ul> </ul>

View File

@ -1,3 +1,4 @@
<%@ page import="cyou.chenx221.helper.UsernameHelper" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html> <!DOCTYPE html>
@ -19,7 +20,13 @@
</head> </head>
<body style="font-family: 'Noto Sans SC Regular',serif"> <body style="font-family: 'Noto Sans SC Regular',serif">
<%
if(new UsernameHelper().getCurrentUsername()!=null){
//redirect /dashboard
//note: 登陆了的就别乱访问了
response.sendRedirect("dashboard");
}
%>
<header> <header>
<nav class="navbar navbar-expand-lg navbar-light bg-light" style="z-index: 1;min-height: 58.59px"> <nav class="navbar navbar-expand-lg navbar-light bg-light" style="z-index: 1;min-height: 58.59px">
<div class="container"> <div class="container">
@ -227,6 +234,7 @@
$('#exampleModal2').modal('show'); $('#exampleModal2').modal('show');
</c:if> </c:if>
}); });
</script> </script>
</body> </body>
</html> </html>

View File

@ -199,7 +199,7 @@
<div class="tab-pane fade show active" id="ex1-tabs-1" role="tabpanel" <div class="tab-pane fade show active" id="ex1-tabs-1" role="tabpanel"
aria-labelledby="ex1-tab-1"> aria-labelledby="ex1-tab-1">
<div class="card-body" style="min-width: 175px"> <div class="card-body" style="min-width: 175px">
<form action="" method="post"> <form action="create" method="post">
<div class="row mb-2"> <div class="row mb-2">
<div class="col-12"> <div class="col-12">
<div class="form-outline"> <div class="form-outline">
@ -231,7 +231,7 @@
<div class="col-12"> <div class="col-12">
<div class="form-outline"> <div class="form-outline">
<input type="number" id="typeText4" class="form-control" <input type="number" id="typeText4" class="form-control"
name="roleid"/> name="roleid" disabled/>
<label class="form-label" for="typeText4">身份ID</label> <label class="form-label" for="typeText4">身份ID</label>
</div> </div>
</div> </div>
@ -276,7 +276,7 @@
<div class="form-outline"> <div class="form-outline">
<input type="password" id="typeText6" class="form-control" <input type="password" id="typeText6" class="form-control"
name="password" required/> name="password" required/>
<label class="form-label" for="typeText6">新的密码</label> <label class="form-label" for="typeText6">新的密码(必填)</label>
</div> </div>
</div> </div>
</div> </div>
@ -285,7 +285,7 @@
<div class="form-outline"> <div class="form-outline">
<input type="password" id="typeText7" class="form-control" <input type="password" id="typeText7" class="form-control"
name="re_password" required/> name="re_password" required/>
<label class="form-label" for="typeText7">重复密码</label> <label class="form-label" for="typeText7">重复密码(必填)</label>
</div> </div>
</div> </div>
</div> </div>
@ -376,9 +376,17 @@
function resetPWD(id) { function resetPWD(id) {
$('#typeText5').val(id); $('#typeText5').val(id);
// const triggerEl = document.querySelector('#ex1-tab-2'); Pending $('#ex1-tab-2').tab('show');
mdb.Tab.getInstance(triggerEl).show(); // Select tab by name
} }
// 监听权限组选择框的变化
document.getElementById('role-select').addEventListener('change', function() {
var roleSelect = document.getElementById('role-select');
var typeText4 = document.getElementById('typeText4');
// 如果选择的是管理员权限组则禁用身份ID输入框否则启用身份ID输入框
typeText4.disabled = roleSelect.value === 'admin';
});
</script> </script>
</body> </body>