Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -5,10 +5,7 @@ import com.hellogithub.entity.userEntity;
|
|||||||
import com.hellogithub.utils.JdbcUtils;
|
import com.hellogithub.utils.JdbcUtils;
|
||||||
import org.apache.commons.dbutils.QueryRunner;
|
import org.apache.commons.dbutils.QueryRunner;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.*;
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class AdminDao {
|
public class AdminDao {
|
||||||
public static adminEntity AdminLogin(String adminName, String password,String loginTime) {
|
public static adminEntity AdminLogin(String adminName, String password,String loginTime) {
|
||||||
@@ -38,20 +35,81 @@ public class AdminDao {
|
|||||||
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
|
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
public int updateAdminLoginTime(String name){
|
* 更新管理员登录时间
|
||||||
String label="";
|
* */
|
||||||
|
public int updateAdminLoginTime(String adminName, String loginTime){
|
||||||
int num;
|
int num;
|
||||||
try {
|
try {
|
||||||
Connection conn = JdbcUtils.getConnection();
|
Connection conn = JdbcUtils.getConnection();
|
||||||
QueryRunner runner = new QueryRunner();
|
QueryRunner runner = new QueryRunner();
|
||||||
String sql="UPDATE user SET searchHistory = ? WHERE userName= ? and isValid = 1";
|
String sql="UPDATE admin SET loginTime = ? WHERE adminName= ? and isValid = 1";
|
||||||
num = runner.update(conn,sql,label,name);
|
num = runner.update(conn,sql,loginTime,adminName);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 根据用户名查询管理员
|
||||||
|
* */
|
||||||
|
public adminEntity findByAdminName(String adminName) {
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
PreparedStatement preparedStatement = null;
|
||||||
|
Connection connection = null;
|
||||||
|
try {
|
||||||
|
connection = JdbcUtils.getConnection();
|
||||||
|
String Sql = "select * from admin where adminName=? and isValid = 1";
|
||||||
|
preparedStatement = connection.prepareStatement(Sql);
|
||||||
|
preparedStatement.setString(1, adminName);
|
||||||
|
resultSet = preparedStatement.executeQuery();
|
||||||
|
if (!resultSet.next()) { // 查询不到用户数据
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// 将db中数据 返回给客户端 查询到数据
|
||||||
|
Integer id = resultSet.getInt(1);
|
||||||
|
String dbUserName = resultSet.getString(2);
|
||||||
|
String dbUserPwd = resultSet.getString(3);
|
||||||
|
adminEntity adminEntity = new adminEntity(dbUserName, dbUserPwd);
|
||||||
|
return adminEntity;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 管理员注册
|
||||||
|
*
|
||||||
|
* */
|
||||||
|
public int adminRegister(String adminName,String adminPwd){
|
||||||
|
Connection connection = null;
|
||||||
|
PreparedStatement preparedStatement = null;
|
||||||
|
try {
|
||||||
|
connection = JdbcUtils.getConnection();
|
||||||
|
// sql语句写的操作 ----加上事务
|
||||||
|
JdbcUtils.beginTransaction(connection); // 开启事务
|
||||||
|
String insertSql = "INSERT INTO admin (adminId, adminName, adminPassword,isValid,loginTime) VALUES (null, ?,?,1,?);";
|
||||||
|
preparedStatement = connection.prepareStatement(insertSql);
|
||||||
|
preparedStatement.setString(1,adminName);
|
||||||
|
preparedStatement.setString(2, adminPwd);
|
||||||
|
Date time= new java.sql.Date(new java.util.Date().getTime());
|
||||||
|
preparedStatement.setDate(3, time);
|
||||||
|
int result = preparedStatement.executeUpdate();
|
||||||
|
// 代码执行没有问题的情况下 则会提交数据
|
||||||
|
JdbcUtils.commitTransaction(connection); // 提交事务
|
||||||
|
return result;
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 程序代码报错之后 是需要回滚事务
|
||||||
|
e.printStackTrace();
|
||||||
|
JdbcUtils.rollBackTransaction(connection);// 回滚事务
|
||||||
|
return 0;
|
||||||
|
} finally {
|
||||||
|
JdbcUtils.closeConnection(preparedStatement, connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -8,7 +8,20 @@ public class AdminService {
|
|||||||
AdminDao adminDao=new AdminDao();
|
AdminDao adminDao=new AdminDao();
|
||||||
|
|
||||||
public adminEntity adminLogin(String userName, String password,String loginTime){
|
public adminEntity adminLogin(String userName, String password,String loginTime){
|
||||||
return AdminDao.AdminLogin(userName,password,loginTime);
|
int num=adminDao.updateAdminLoginTime(userName,loginTime);
|
||||||
|
if(num==1){
|
||||||
|
return AdminDao.AdminLogin(userName,password,loginTime);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
public adminEntity findByAdminName(String adminName){
|
||||||
|
return adminDao.findByAdminName(adminName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int adminRegister(String adminName,String pwd){
|
||||||
|
return adminDao.adminRegister(adminName,pwd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,6 +16,9 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import javax.servlet.http.Cookie;
|
import javax.servlet.http.Cookie;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
@WebServlet("/adminLogin")
|
@WebServlet("/adminLogin")
|
||||||
@@ -23,7 +26,7 @@ public class AdminLoginServlet extends HttpServlet {
|
|||||||
AdminService adminService=new AdminService();
|
AdminService adminService=new AdminService();
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
doGet(req, resp);
|
doPost(req, resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -46,7 +49,6 @@ public class AdminLoginServlet extends HttpServlet {
|
|||||||
setResultError("用户名不能够是为空!", writer);
|
setResultError("用户名不能够是为空!", writer);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String adminPwd = req.getParameter("password");
|
String adminPwd = req.getParameter("password");
|
||||||
// 参数验证
|
// 参数验证
|
||||||
if (StringUtils.isEmpty(adminPwd)) {
|
if (StringUtils.isEmpty(adminPwd)) {
|
||||||
@@ -54,13 +56,10 @@ public class AdminLoginServlet extends HttpServlet {
|
|||||||
setResultError("密码不能够是为空!", writer);
|
setResultError("密码不能够是为空!", writer);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Date now = new Date();
|
||||||
String loginTime=req.getParameter("loginTime");
|
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
if (StringUtils.isEmpty(loginTime)){
|
String loginTime=sdf.format(now);
|
||||||
//转发到错误页面
|
System.out.println(loginTime);
|
||||||
setResultError("登陆时间不能够是为空!", writer);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 从session中获取图形验证码
|
// 从session中获取图形验证码
|
||||||
HttpSession session = req.getSession();
|
HttpSession session = req.getSession();
|
||||||
|
@@ -0,0 +1,88 @@
|
|||||||
|
package com.hellogithub.servlet.systemAdmin;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.hellogithub.entity.adminEntity;
|
||||||
|
import com.hellogithub.entity.userEntity;
|
||||||
|
import com.hellogithub.service.AdminService;
|
||||||
|
import com.hellogithub.utils.RandomValidateCode;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.annotation.WebServlet;
|
||||||
|
import jakarta.servlet.http.HttpServlet;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.util.HashMap;
|
||||||
|
@WebServlet("/adminRegister")
|
||||||
|
public class AdminRegisterServlet extends HttpServlet {
|
||||||
|
AdminService adminService=new AdminService();
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
doPost(req, resp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
resp.setCharacterEncoding("UTF-8");
|
||||||
|
resp.setContentType("text/html; charset=utf-8");
|
||||||
|
// 设置响应头允许ajax跨域访问
|
||||||
|
String curOrigin = req.getHeader("Origin");
|
||||||
|
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
|
||||||
|
resp.setHeader("Access-Control-Allow-Credentials", "true");
|
||||||
|
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
|
||||||
|
resp.setHeader("Access-Control-Max-Age", "3600");
|
||||||
|
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
|
||||||
|
resp.setContentType("application/json;charset=UTF-8");
|
||||||
|
PrintWriter writer = resp.getWriter();
|
||||||
|
String adminName = req.getParameter("adminname");
|
||||||
|
if (StringUtils.isEmpty(adminName)) {
|
||||||
|
setResultError("用户名称不能够是为空", writer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String adminPwd = req.getParameter("password");
|
||||||
|
// 参数验证
|
||||||
|
if (StringUtils.isEmpty(adminPwd)) {
|
||||||
|
setResultError("密码不能够是为空", writer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 管理员注册之前根据用户名称查询该管理员是否存在如果不存在的情况下才可以注册 如果存在的话就无法注册
|
||||||
|
adminEntity adminEntity = adminService.findByAdminName(adminName);
|
||||||
|
if (adminEntity != null) {
|
||||||
|
setResultError("用户名: " + adminName + "已被注册!", writer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//用户数据注册
|
||||||
|
int register = adminService.adminRegister(adminName, adminPwd);
|
||||||
|
if (register <= 0) {
|
||||||
|
// 注册失败了 //转发到错误页面
|
||||||
|
setResultError("注册失败!", writer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setResultOK("注册成功!", writer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
super.doDelete(req, resp);
|
||||||
|
}
|
||||||
|
public void setResult(Integer code, String msg, PrintWriter writer) {
|
||||||
|
HashMap<String, Object> result = new HashMap<>();
|
||||||
|
result.put("code", code);
|
||||||
|
result.put("msg", msg);
|
||||||
|
String jsonString = JSONObject.toJSONString(result);
|
||||||
|
writer.println(jsonString);
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResultError(String msg, PrintWriter writer) {
|
||||||
|
setResult(500, msg, writer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResultOK(String msg, PrintWriter writer) {
|
||||||
|
setResult(200, msg, writer);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user