SSM整合 管理员登录功能(密码加密功能)

MD5加密类

    /**
     * 对明文字符串进行md5加密
     * @param source 传入的字符串
     * @return 加密的结果
     */
    public static String md5(String source) {
        // 1.判断source是否有效
        if (source == null || source.length() == 0) {
            // 2.如果不是有效的字符串抛出异常
            throw new RuntimeException(CrowdConstant.MESSAGE_STRING_INVALIDATE);
        }
        try {
            // 3.获取MessageDigest对象
            String algorithm = "md5";
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);

            // 4.获取明文字符串对应的字节数组
            byte[] input = source.getBytes();

            // 5.执行加密
            byte[] output = messageDigest.digest(input);

            // 6.创建BigInteger对象
            int signum = 1;
            BigInteger bigInteger = new BigInteger(signum,output);

            // 7.按照16进制将bigInteger的值转换为字符串
            int radix = 16;
            String encoded= bigInteger.toString(radix).toUpperCase();

            return encoded;

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }

pojo层:Admin.java

package com.lyc.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Admin {
    private Integer id;

    private String login_acct;

    private String user_pswd;

    private String user_name;

    private String email;

    private String create_time;


}

mapper层:AdminMapper

public interface AdminMapper {
    
    Admin selectByLoginAcct(String login_acct);
}

mapper层:AdminMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lyc.mapper.AdminMapper">
    <resultMap id="BaseResultMap" type="com.lyc.pojo.Admin">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="login_acct" jdbcType="VARCHAR" property="login_acct"/>
        <result column="user_pswd" jdbcType="CHAR" property="user_pswd"/>
        <result column="user_name" jdbcType="VARCHAR" property="user_name"/>
        <result column="email" jdbcType="VARCHAR" property="email"/>
        <result column="create_time" jdbcType="CHAR" property="create_time"/>
    </resultMap>
    <select id="selectByLoginAcct" resultType="com.lyc.pojo.Admin">
        select * from t_admin where login_acct = #{login_acct}
    </select>
</mapper>

service层:AdminService

package com.lyc.service;

import com.lyc.pojo.Admin;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

public interface AdminService {
    Admin selectByLoginAcct(String login_acct);
}

service层:AdminServiceImpl.java

package com.lyc.service;

import com.lyc.mapper.AdminMapper;
import com.lyc.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
@Service("adminService")
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminMapper adminMapper;

    public void setAdminMapper(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }
    @Override
    public Admin selectByLoginAcct(String login_acct) {
        return adminMapper.selectByLoginAcct(login_acct);
    }
}

controller层:AdminController.java

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 登录
     * @param login_acct 账号
     * @param user_pswd 密码
     * @param request 传输信息
     * @param session 设置会话
     * @return
     */

    @RequestMapping("admin/do/login.html")
    public String doLogin (
            @RequestParam("login_acct") String login_acct,
            @RequestParam("user_pswd") String user_pswd,
            HttpSession session,
            HttpServletRequest request
    ){
        Admin admin = adminService.selectByLoginAcct(login_acct);
        if (admin == null){
            request.setAttribute("msg",CrowdConstant.MESSAGE_LOGIN_FAILED);
        return "system-error";
        }else {
            String Duser_pswd = admin.getUser_pswd();

            String Muser_pswd = CrowdUtil.md5(user_pswd);

            if (Muser_pswd.equals(Duser_pswd)){
                session.setAttribute("admin", admin);
                return "admin-main";
            }else {
                request.setAttribute("msg", CrowdConstant.MESSAGE_LOGIN_FAILED);
                return "system-error";
            }
        }
    }
}

jsp层:admin-login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="keys" content="">
    <meta name="author" content="">
    <base href="http://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/">
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/font-awesome.min.css">
    <link rel="stylesheet" href="css/login.css">
    <script src="jquery/jquery-2.1.1.min.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <style>

    </style>
    <title>后台登录页面</title>
</head>
<body>
<div class="container">

    <form action="admin/do/login.html" class="form-signin" role="form">
        <h2 class="form-signin-heading"><i class="glyphicon glyphicon-log-in"></i> 管理员登录</h2>
        <div class="form-group has-success has-feedback">
            <input type="text" name="login_acct" class="form-control"  placeholder="请输入登录账号" autofocus>
            <span class="glyphicon glyphicon-user form-control-feedback"></span>
        </div>
        <div class="form-group has-success has-feedback">
            <input type="password" name="user_pswd" class="form-control" placeholder="请输入登录密码" style="margin-top:10px;">
            <span class="glyphicon glyphicon-lock form-control-feedback"></span>
        </div>
        <button type="submit" class="btn btn-lg btn-success btn-block"> 登录</button>
    </form>
</div>
</body>
</html>


posted on 2021-05-14 15:47  liuyunche  阅读(975)  评论(0编辑  收藏  举报