springboot双登录

package com.bwie.controller;

import com.bwie.pojo.User;
import com.bwie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //正常登录
    @RequestMapping("/tologin")
    public  String tologin(){
        return "login";
    }
    //登录
    @RequestMapping("/login")
    @ResponseBody
    public Integer login(User user){
        return userService.login(user);
    }
    //先发送验证码发送成功后才可以登录
    @RequestMapping("/code")
    @ResponseBody
    public boolean code(String phone){
        userService.code(phone);
        return true;
    }
    //手机号登录
    @RequestMapping("/tophone")
    public String tophone(){
        return "phone";
    }
    @RequestMapping("/phone")
    @ResponseBody
    public boolean phone(String phone,String code){
       boolean b=userService.phone(phone,code);
       return b;
    }
}
package com.bwie.service;

import com.bwie.pojo.User;

public interface UserService {
    Integer login(User user);

    void code(String phone);

    boolean phone(String phone, String code);

}
package com.bwie.service.impl;

import com.bwie.dao.UserDAO;
import com.bwie.mq.Proucer;
import com.bwie.util.MsgUtil;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceimpl implements UserService {

    @Autowired
    Proucer proucer;
    @Autowired
    private UserDAO userDAO;
    @Autowired
    HttpSession session;
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Override
    public Integer login(User user) {
        String s = MD5Util.md5(user.getUpwd());
        user.setUpwd(s);
        User u=userDAO.login(user.getUname());
     if (u==null){
         return 0;
     }
     if (!u.getUpwd().equals(user.getUpwd())){
         return 1;
     }
     session.setAttribute("user",u);
     return 2;
    }

    @Override
    public void code(String phone) {
        proucer.pro1(phone);
    }

    @Override
    public boolean phone(String phone, String code) {
        //我们通过redis键值对的形式通过刚刚存的手机号来获取值得验证码
       String s=redisTemplate.boundValueOps(phone).get();
       //通过前台穿过来的code验证码和我们reids里面的验证码进行对比
        //如果等于我们写一个验证手机号是否正确的方法
        if (code.equals(s)){
        User u=userDAO.findphone(phone);
        //让后把我们的u以session存到session
        session.setAttribute("user",u);
        return true;
        }
        return false;
    }
}
<?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.bwie.dao.UserDAO">


    <!--    当我们验证码正确候,让后验证手机号-->
<!--    <select id="findphone" resultType="com.bwie.pojo.User">-->
<!--        select *   from t_user where phone=#{phone}-->
<!--    </select>-->
    <select id="login" resultType="com.bwie.pojo.User">
        select *  from t_user where uname=#{uname}
    </select>
    <select id="findphone" resultType="com.bwie.pojo.User">
          select *  from t_user where phone=#{phone}
    </select>

</mapper>

普通登录

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../static/css/css.css">
    <script src="../static/js/jquery-1.8.2.min.js"></script>
</head>
<body>
<form action="">
    <table>
        <tr>
            <td>用户名</td>
            <td>
                <input type="text" name="uname">
            </td>
        </tr>
        <tr>
            <td>密码</td>
            <td>
                <input type="text" name="upwd">
            </td>
        </tr>
    </table>
    <input type="button" value="登录" onclick="login()">
    <input type="button" value="使用短信验证登录" onclick="dx()">
</form>
</body>
<script>
function login() {
    $.ajax({
        type:"post",
        url:"/user/login",
        data:$('form').serialize(),
        dataType:"json",
        success(a){
            if (a==0){
                alert("登录失败账号错误")
                location.reload();
            }else if (a==1){
                alert("登录失败密吗错误")
                location.reload();
            }else{
                alert("登录成功")
                window.location="/stu/list"
            }
        }
    })
}
function dx() {
    location="/user/tophone"
}
</script>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../static/css/css.css">
    <script src="../static/js/jquery-1.8.2.min.js"></script>
</head>
<body>
<form action="">
    <table>
        <tr>
            <td>请输入手机号</td>
            <td> <input type="text" name="phone"></td>
        </tr>
        <tr>
            <td>
                <input type="text" name="code">
            </td>
            <td>
                <input type="button" value="发送验证码" onclick="yzm()">
            </td>
        </tr>
    </table>
    <input type="button" value="登录" onclick="deng()">
</form>
</body>
<script>
    //先发送验证码
    function yzm() {
        //通过name属性来获取值
        var phone=$("[name=phone]").val();
        $.ajax({
            type:"post",
            url:"/user/code",
            data:{"phone":phone},
            datatype:"josn",
            success(a){
                alert("验证码已发送,请在60秒内进行完成验证")
            }
        })
    }
    //在进行手机号验证码登录
    function deng() {
        $.ajax({
            type:"post",
            url:"/user/tophone",
            data:$('form').serialize(),
            datatype:"json",
            success(a) {
                if (a){
                    alert("验证成功")
                    window.location="/stu/list"
                }else{
                    alert("验证失败")
                }
            }
        })
    }
</script>
</html>
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务