Layui+ssm完成登录注册修改密码退出登录

使用layui+ssm完成登录注册修改密码和退出登录

使用ssm完成登录注册

Controller层

package cn.kgc.ssm.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.crypto.SecureUtil;
import cn.kgc.ssm.entity.Vip;
import cn.kgc.ssm.service.pjxLoginService;
import cn.kgc.ssm.util.LayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("user")
public class GUserController {
    @Autowired
    private pjxLoginService pjxLoginService1;

    // 验证码
    @RequestMapping("captcha")
//    @ResponseBody
    public void Captcha(HttpSession session, HttpServletResponse resp) {
        LineCaptcha lineCaptcha = new LineCaptcha(260, 100, 4, 5);

        // 获取生成的验证码 以便后续判断
        String code = lineCaptcha.getCode();
        System.out.println("code = " + code);
        session.setAttribute("captcha", code);

        // 写出验证码
        //获取输出流 将图像验证码 写出到登录页
        ServletOutputStream outputStream = null;
        try {
            outputStream = resp.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        lineCaptcha.write(outputStream);
        //释放资源
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 登录
    @RequestMapping("login")
    @ResponseBody
    public Object login(String username, String password, String code, HttpSession session, HttpServletResponse resp) {
        System.out.println("登录");
        LayUtil layUtils;
        Object captcha = session.getAttribute("captcha");
        System.out.println("captcha = " + captcha);
        Vip vip = Vip.builder().username(username).password(SecureUtil.md5(password)).build();
        if (captcha.equals(code)) {//验证码
            System.out.println("1");
            layUtils = pjxLoginService1.selectUser(vip);
            // 将用户信息放入session 用于权限的判定
            if (layUtils.getData() != null) {
                Vip vip1 = (Vip) layUtils.getData();
                session.setAttribute("vip1", vip1);
            }
        } else {// 提示用户验证码不正确
            System.out.println("2");
            layUtils = new LayUtil();
            layUtils.setCode(500);
            layUtils.setMsg("验证码错误!!");
        }
        return layUtils;
    }
//    @RequestMapping("login1")
//    public String login1(){
//        return "WEB-INF/index";
//    }

    // 注册登录
    @ResponseBody
    @RequestMapping("register")
    public Map register(String username, String password) {
        HashMap<Object, Object> map = new HashMap<>();
        Vip vip = Vip.builder().username(username).password(SecureUtil.md5(password)).build();
        Vip vip1 = pjxLoginService1.selectByName(username);
        if (vip1 == null) {
            boolean b = pjxLoginService1.addVip(vip);
            if (b) {// 注册成功
                map.put("code", 0);
                map.put("msg", "注册成功");
            } else {// 注册失败
                map.put("code", 500);
                map.put("msg", "注册失败");
            }
        } else {
            map.put("code", 500);
            map.put("msg", "用户名已存在");
        }

        return map;
    }
    //    修改密码
    @RequestMapping("resetpassword")
    @ResponseBody
    public Map resetpassword(String oldpwd, String newpwd, HttpSession session) {


        // 获取当前的用户密码
        Vip vip1 = (Vip)session.getAttribute("vip1");
        String password = vip1.getPassword();
        // 获取用户的id
        long id = vip1.getId();
        HashMap<String, Object> map = new HashMap<>();
        if (SecureUtil.md5(oldpwd).equals(password)) {
            // 执行修改密码操作
            boolean b = pjxLoginService1.resetPasswordById(id, SecureUtil.md5(newpwd));
            if (b) {
                map.put("code", 0);
                map.put("msg", "密码修改成功");
            } else {
                map.put("code", 500);
                map.put("msg", "密码修改失败");
            }

        } else {// 修改失败
            map.put("code", 500);
            map.put("msg", "原始密码不正确");
        }
        return map;
    }

    //    退出登录
    @RequestMapping("logout")
    @ResponseBody
    public Map logout(HttpSession session) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            session.invalidate();
            map.put("code", 0);
            map.put("msg", "退出登录成功");
        } catch (Exception throwables) {
            throwables.printStackTrace();
            map.put("code", 500);
            map.put("msg", "退出登录失败");
        }
        return map;

    }

}

Service层

package cn.kgc.ssm.service.impl;

import cn.kgc.ssm.entity.Vip;
import cn.kgc.ssm.mapper.pjxLoginMapper;
import cn.kgc.ssm.service.pjxLoginService;
import cn.kgc.ssm.util.LayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/*
@author peng
@date13/7/2022
*/
@Service
public class pjxLoginServiceImpl implements pjxLoginService {
    @Autowired
    private pjxLoginMapper pjxLoginMapper1;
    @Override
    public LayUtil selectUser(Vip vip) {
        System.out.println("ser");
        // TODO 根据用户名查询的结果
        System.out.println("vip = " + vip);
        Vip vip1 = pjxLoginMapper1.selectByUserName(vip.getUsername());
        System.out.println("vip1 = " + vip1);
        LayUtil layUtils = new LayUtil();
        // 1. TODO 判定user是否为空 true 账号不存在 请注册账号
        if (vip1 == null) {
            layUtils.setCode(500);
            layUtils.setMsg("账户不存在");
        } else {
            // 2. TODO 判定密码 false 提示用户密码不对 true 密码对
            String password1 = vip.getPassword();// 前端密码
            String password2 = vip1.getPassword();// 数据库密码
            if (password1.equals(password2)) {
                layUtils.setCode(0);
                layUtils.setData(vip1);
                layUtils.setMsg("登陆成功");
            } else {
                layUtils.setCode(500);
                layUtils.setMsg("密码错误");
            }
        }
        return layUtils;
    }

    @Override
    public boolean addVip(Vip vip) {
        int line= pjxLoginMapper1.addVip(vip);
        return line>0;
    }

    @Override
    public Vip selectByName(String username) {
        Vip vip = pjxLoginMapper1.selectByUserName(username);
        return vip;
    }

    @Override
    public boolean resetPasswordById(long id, String md5) {
        int line=pjxLoginMapper1.resetPassword(id,md5);
        return line>0;

    }


}

Mapper

<?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="cn.kgc.ssm.mapper.pjxLoginMapper">
    <insert id="addVip">
        insert into vip(username,password)values (#{username},#{password})
    </insert>
    <update id="resetPassword">
        update vip set password=#{md5} where id=#{id}
    </update>

    <select id="selectByUserName" resultType="cn.kgc.ssm.entity.Vip">
        select * from vip where username=#{username}

    </select>
    <select id="selectOldPwd" resultType="vip">
        select password from vip where id=#{id}
    </select>
</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值