欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于C2C不能注册的问题解决方法

程序员文章站 2022-06-01 15:45:22
...

1.启动服务器,进入index.html首页

关于C2C不能注册的问题解决方法

2.点击注册按钮即跳转至login_page.html

关于C2C不能注册的问题解决方法
关于C2C不能注册的问题解决方法

关于C2C不能注册的问题解决方法

我们找到包了 如下
关于C2C不能注册的问题解决方法

于是开始分析代码。。。。前面的几个if语句应该只是判断一**册的字符是否符合正则表达式,关键的一步还是如下代码片段(造成注册不了)

关于C2C不能注册的问题解决方法

调用当前类的一个产生随机四位数的方法,继续跟,去到那个方法如下:

关于C2C不能注册的问题解决方法

这里填写注册信息:
关于C2C不能注册的问题解决方法

点击获取验证码后的效果如下:
关于C2C不能注册的问题解决方法

手机上的显示如下:
关于C2C不能注册的问题解决方法

接下来填写验证码进行注册(输入验证码与后台产生的4位数一致后跳转的页面):
关于C2C不能注册的问题解决方法

注册成功后直接跳转至首页
关于C2C不能注册的问题解决方法

所以有一个问题是在输入了验证码后,点击那个下一步的按钮时发生了什么?还是回到源码上,如下:
关于C2C不能注册的问题解决方法

一葫芦画瓢:去到JS看当前的class按钮调用了哪个url,如下:
关于C2C不能注册的问题解决方法

在后台控制器代码中找到与url checkCode一致的注解,于是找到了
关于C2C不能注册的问题解决方法

关于C2C不能注册的问题解决方法
上面的checkCodePhone是我改过后的,通过上生成随机数的控制器产生的4位数字符串,把这串数放到session里,然后又在当前控制器中得到这个字符串。拿到这个字符串后再与前台传过来的code进行对比,对比一致后成功!!

关于C2C不能注册的问题解决方法
数据库两张表也会随之各自会插入一条数据
关于C2C不能注册的问题解决方法
关于C2C不能注册的问题解决方法

以下是我更改后的相关类

package com.wsk.controller;

import com.wsk.pojo.UserInformation;
import com.wsk.response.BaseResponse;
import com.wsk.service.UserInformationService;
import com.wsk.tool.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;

/**
 * Created by wsk1103 on 2017/4/30.
 */
@Controller
public class SendEmailController {

    @Resource
    private UserInformationService userInformationService;
    private static final Logger log = LoggerFactory.getLogger(SendEmailController.class);

    //send the Email to the phone
    @RequestMapping(value = "sendCode.do", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public BaseResponse sendEmail(HttpServletRequest req, HttpServletResponse res,
                                  @RequestParam String phone, @RequestParam String action,
                                  @RequestParam String token,@RequestParam String name) {
        res.setContentType("text/html;charset=UTF-8");
        //token,防止重复提交
        System.out.println("phone:"+phone+"---"+"action:"+action+"----"+"token:"+token+"---"+name+"---");
        String sendCodeToken = (String) req.getSession().getAttribute("token");
        if (StringUtils.getInstance().isNullOrEmpty(sendCodeToken) || !sendCodeToken.equals(token)) {
            return BaseResponse.fail();
        }
        //判断手机号码是否为正确
        if (!StringUtils.getInstance().isPhone(phone)) {
            return BaseResponse.fail();
        }
        //如果是忘记密码提交的发送短信
        if ("forget".equals(action)) {
            if (!isUserPhoneExists(phone)) {
                //失败
                return BaseResponse.fail();
            }
        } else if ("register".equals(action)) {
            //失败
            if (isUserPhoneExists(phone)) {
                return BaseResponse.fail();
            }
        }
        //get the random num to phone which should check the phone to judge the phone is belong user
        req.getSession().setAttribute("phone1",phone);
        req.getSession().setAttribute("name1",name);
        getRandomForCodePhone(req);
        String ra = (String) req.getSession().getAttribute("codePhone");
        String text1 = "【WSK的验证码】您的验证码是:";
        String text2 = ",请保护好自己的验证码。";
        String text = text1 + ra + text2;
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.139.com");
        prop.setProperty("mail.transport.protocol", "smtp");
        prop.setProperty("mail.smtp.auth", "true");
        prop.setProperty("mail.smtp.port", "25");
        try {
            String realPhone = phone;
//            phone += "@139.com";
//            message.setRecipient(Message.RecipientType.TO, new InternetAddress(phone));
//            message.setSubject("来自WSK的验证码");
//            message.setContent(text, "text/html;charset=UTF-8");
            //这里先不发生信息,以后要开启的
//            ts.sendMessage(message, message.getAllRecipients());
//            ts.close();
            req.getSession().setAttribute("phone", realPhone);
            return BaseResponse.success();
        } catch (Exception me) {
            me.printStackTrace();
            return BaseResponse.fail();
        }
    }

    // get the random phone`s code
    private void getRandomForCodePhone(HttpServletRequest req) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            sb.append(random.nextInt(10));
        }
        System.out.println(sb.toString());
        req.getSession().setAttribute("codePhone", sb.toString());

    }

//    //检验验证码
//    private boolean checkPhoto(String photo, HttpServletRequest request) {
//        photo = photo.toLowerCase();
//        String true_photo = (String) request.getSession().getAttribute("rand");
//        return true_photo.equals(photo);
//    }

    //To determine whether the user's mobile phone number exists
    private boolean isUserPhoneExists(String phone) {
        boolean result = false;
        try {
            int id = userInformationService.selectIdByPhone(phone);
            if (id == 0) {
                return result;
            }
            UserInformation userInformation = userInformationService.selectByPrimaryKey(id);

            if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
                return false;
            }
            String userPhone = userInformation.getPhone();
            result = !userPhone.equals("");
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }


}

package com.wsk.controller;

import com.wsk.pojo.UserInformation;
import com.wsk.pojo.UserPassword;
import com.wsk.response.BaseResponse;
import com.wsk.service.UserInformationService;
import com.wsk.service.UserPasswordService;
import com.wsk.tool.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wsk1103 on 2017/5/9.
 * 注册中心
 */
@Controller
public class RegisterController {
    @Resource
    private UserPasswordService userPasswordService;

    @Resource
    private UserInformationService userInformationService;

    //开始注册用户
    @RequestMapping("/insertUser.do")
    @ResponseBody
    public BaseResponse insertUser(HttpServletRequest request,
                                   @RequestParam String password, @RequestParam String token) {
        //存储与session中的手机号码
        String realPhone = (String) request.getSession().getAttribute("phone");
        //token,唯一标识
        String insertUserToken = (String) request.getSession().getAttribute("token");
        //防止重复提交
        if (StringUtils.getInstance().isNullOrEmpty(insertUserToken) || !insertUserToken.equals(token)) {
            return BaseResponse.fail();
        }
        //该手机号码已经存在
        int uid = userInformationService.selectIdByPhone(realPhone);
        if (uid != 0) {
            return BaseResponse.fail();
        }

        //用户信息
        UserInformation userInformation = new UserInformation();
        userInformation.setPhone(realPhone);
        userInformation.setCreatetime(new Date());
        String username = (String) request.getSession().getAttribute("name");
        userInformation.setUsername(username);
        userInformation.setModified(new Date());
        int result;
        result = userInformationService.insertSelective(userInformation);
        //如果用户基本信息写入成功
        if (result == 1) {
            uid = userInformationService.selectIdByPhone(realPhone);
            String newPassword = StringUtils.getInstance().getMD5(password);
            UserPassword userPassword = new UserPassword();
            userPassword.setModified(new Date());
            userPassword.setUid(uid);
            userPassword.setPassword(newPassword);
            result = userPasswordService.insertSelective(userPassword);
            //密码写入失败
            if (result != 1) {
                userInformationService.deleteByPrimaryKey(uid);
                return BaseResponse.fail();
            } else {
                //注册成功
                userInformation = userInformationService.selectByPrimaryKey(uid);
                request.getSession().setAttribute("userInformation", userInformation);
                return BaseResponse.success();
            }
        }
        return BaseResponse.fail();
    }
}

package com.wsk.controller;

import com.wsk.pojo.UserInformation;
import com.wsk.pojo.UserPassword;
import com.wsk.response.BaseResponse;
import com.wsk.service.UserInformationService;
import com.wsk.service.UserPasswordService;
import com.wsk.tool.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wsk1103 on 2017/5/9.
 */
@RestController
public class ForgetController {

    @Resource
    private UserPasswordService userPasswordService;
    @Resource
    private UserInformationService userInformationService;

    @RequestMapping(value = "checkCode.do", method = {RequestMethod.POST, RequestMethod.GET})
    public Map checkPhone(HttpServletRequest request, Model model,
                          @RequestParam String code, @RequestParam String token) {
        Map<String, Integer> map = new HashMap();
        String name = request.getParameter("name");
        if (!StringUtils.getInstance().isNullOrEmpty(name)) {
            request.getSession().setAttribute("name", name);
        }
        String checkCodeToken = (String) request.getSession().getAttribute("token");
        if (StringUtils.getInstance().isNullOrEmpty(checkCodeToken) || !checkCodeToken.equals(token)) {
            map.put("result", 0);
            return map;
        }
        //验证码错误
        if (!checkCodePhone(code, request)) {
            map.put("result", 0);
            return map;
        }
        map.put("result", 1);
        return map;
    }

    //更新密码
    @RequestMapping("updatePassword.do")
    public BaseResponse updatePassword(HttpServletRequest request, Model model,
                                       @RequestParam String password, @RequestParam String token) {
        //防止重复提交
        String updatePasswordToken = (String) request.getSession().getAttribute("token");
        if (StringUtils.getInstance().isNullOrEmpty(updatePasswordToken) || !updatePasswordToken.equals(token)) {
            return BaseResponse.fail();
        }
        String realPhone = (String) request.getSession().getAttribute("phone");
        UserPassword userPassword = new UserPassword();
        String newPassword = StringUtils.getInstance().getMD5(password);
        int uid;
        try {
            uid = userInformationService.selectIdByPhone(realPhone);
            if (uid == 0) {
                return BaseResponse.fail();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.fail();
        }
        int id = userPasswordService.selectByUid(uid).getId();
        userPassword.setId(id);
        userPassword.setUid(uid);
        userPassword.setModified(new Date());
        userPassword.setPassword(newPassword);
        int result;
        try {
            result = userPasswordService.updateByPrimaryKeySelective(userPassword);
        } catch (Exception e) {
            return BaseResponse.fail();
        }
        //更新失败
        if (result != 1) {
            return BaseResponse.fail();
        }
        UserInformation userInformation = userInformationService.selectByPrimaryKey(uid);
        request.getSession().setAttribute("userInformation", userInformation);
        return BaseResponse.success();
    }

    //check the phone`s code
    private boolean checkCodePhone(String codePhone, HttpServletRequest request) {
        String trueCodePhone =(String) request.getSession().getAttribute("codePhone");
        return codePhone.equals(trueCodePhone);
    }
}