diff --git a/WebContent/admin/cardedit.jsp b/WebContent/admin/cardedit.jsp index 57c9e5c..0998127 100644 --- a/WebContent/admin/cardedit.jsp +++ b/WebContent/admin/cardedit.jsp @@ -66,7 +66,7 @@
- placeholder="请输入密码" autocomplete="off" class="layui-input" lay-verify="required"> + placeholder="请输入密码" autocomplete="off" class="layui-input" lay-verify="required">
diff --git a/WebContent/admin/index.jsp b/WebContent/admin/index.jsp index 90d4c88..cb847ef 100644 --- a/WebContent/admin/index.jsp +++ b/WebContent/admin/index.jsp @@ -28,11 +28,10 @@
  • - 贤心 + 系统管理员
    -
    基本资料
    -
    安全设置
    +
    修改密码
  • 注销
  • @@ -64,14 +63,29 @@ + diff --git a/WebContent/admin/managerlist.jsp b/WebContent/admin/managerlist.jsp index 25093b0..154974f 100644 --- a/WebContent/admin/managerlist.jsp +++ b/WebContent/admin/managerlist.jsp @@ -39,8 +39,8 @@ ,height: 600 ,cols: [[ {field:'id', width:80, title: 'ID', sort: true} - ,{field:'name', width:80, title: '用户名'} ,{field:'account', width:80, title: '账号', sort: true} + ,{field:'name', width:80, title: '姓名'} ,{field:'email', title: '邮箱', minWidth: 150} ,{fixed: 'right', title:'操作', toolbar: '#operateBar', align: 'center', width:150} ]] diff --git a/WebContent/admin/updatePassword.jsp b/WebContent/admin/updatePassword.jsp new file mode 100644 index 0000000..6cab7d5 --- /dev/null +++ b/WebContent/admin/updatePassword.jsp @@ -0,0 +1,89 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> + + + + +修改密码 + + + + + + + +
    + +
    + +
    + +
    +
    + +
    + +
    + +
    +
    + +
    + +
    + +
    +
    + + +
    +
    + +
    +
    +
    + + + \ No newline at end of file diff --git a/build/classes/javabean/Admin.class b/build/classes/javabean/Admin.class index a9093d7..f4037de 100644 Binary files a/build/classes/javabean/Admin.class and b/build/classes/javabean/Admin.class differ diff --git a/src/javabean/Admin.java b/src/javabean/Admin.java index d8a703e..a3f4ee8 100644 --- a/src/javabean/Admin.java +++ b/src/javabean/Admin.java @@ -35,7 +35,7 @@ public class Admin { connection = Base.getConnection(); pstmt = (PreparedStatement) connection.prepareStatement(sql); pstmt.setString(1, username); - pstmt.setString(2, password); + pstmt.setString(2, Util.passMd5(password)); resultSet = pstmt.executeQuery(); try{ if (resultSet.next()) { diff --git a/src/javabean/Util.java b/src/javabean/Util.java index 1c1a7cd..506256a 100644 --- a/src/javabean/Util.java +++ b/src/javabean/Util.java @@ -1,5 +1,8 @@ package javabean; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import net.sf.json.JSONObject; @@ -40,6 +43,9 @@ public class Util { return dateFormat.format(date); } + /* + * 返回json数据 + */ public static String jsonResponse(int code, String msg, String data) { JSONObject jsonObject = new JSONObject(); jsonObject.put("code", code); @@ -47,13 +53,36 @@ public class Util { if( data!=null ) { jsonObject.put("data", data); } - return jsonObject.toString(); } + /* + * md5加密 + */ + public static String stringToMD5(String plainText) { + byte[] secretBytes = null; + try { + secretBytes = MessageDigest.getInstance("md5").digest( + plainText.getBytes()); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("没有这个md5算法!"); + } + String md5code = new BigInteger(1, secretBytes).toString(16); + for (int i = 0; i < 32 - md5code.length(); i++) { + md5code = "0" + md5code; + } + return md5code; + } + + public static String passMd5(String password) { + String salt = "ew!.E"; + return Util.stringToMD5(password +salt); + } + public static void main(String[] args) { - java.util.Date date = new java.util.Date(); - SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); - System.out.println(dateFormat.format(date)); + System.out.println(Util.passMd5("admin")); + //java.util.Date date = new java.util.Date(); + //SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); + //System.out.println(dateFormat.format(date)); } } diff --git a/src/servlet/admin/UpdatePassword.java b/src/servlet/admin/UpdatePassword.java new file mode 100644 index 0000000..ebd205c --- /dev/null +++ b/src/servlet/admin/UpdatePassword.java @@ -0,0 +1,98 @@ +package servlet.admin; + +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import javabean.Base; +import javabean.Util; +import net.sf.json.JSONObject; + + +@WebServlet("/admin/updatePassword") +public class UpdatePassword extends HttpServlet { + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("application/json; charset=utf8"); + // 接受数据 + String oldPassword = req.getParameter("oldPassword"); + String newPassword = req.getParameter("newPassword"); + String conPassword = req.getParameter("conPassword"); + HttpSession session = req.getSession(); + String username = (String) session.getAttribute("admin"); + + + // 准备资源 + Connection connection = null; + PreparedStatement pstmt = null; + ResultSet resultSet = null; + int result = 0; + String sql = null; + int count = 0; + // 返回数据 + int code = 1; + String msg = "error"; + JSONObject json = new JSONObject(); + PrintWriter out = resp.getWriter(); + + // 可靠性 + if(conPassword.equals(newPassword)) { + // 查询 + try { + connection = Base.getConnection(); + // 验证账号密码 + sql = "select count(*) as count from admin where username=? and password=?"; + pstmt = connection.prepareStatement(sql); + pstmt.setString(1, username); + pstmt.setString(2, Util.passMd5(oldPassword)); + resultSet = pstmt.executeQuery(); + while(resultSet.next()) { + count = resultSet.getInt("count"); + } + // 修改密码 + // 密码正确 + if(count >= 1) { + sql = "update admin set password=? where username=?"; + pstmt = connection.prepareStatement(sql); + pstmt.setString(1, Util.passMd5(newPassword)); + pstmt.setString(2, username); + result = pstmt.executeUpdate(); + if(result == 1) { + code = 0; + msg = "修改成功"; + }else { + msg = "修改失败"; + } + + }else { + msg = "密码错误"; + } + } catch (ClassNotFoundException e) { + msg = "class notfound"; + } catch (SQLException e) { + msg = "sql错误"; + } finally { + try { + Base.closeResource(connection, pstmt, resultSet); + } catch (SQLException e) { + msg = "关闭失败"; + } + } + + }else { + msg = "两次密码不一致"; + } + out.print(Util.jsonResponse(code, msg, null)); + } + +}