You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Used-Trading-Platform.txt/src/main/java/com/wsk/controller/ForgetController.java

154 lines
10 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
package //;
import //使
javax.servlet.http.HttpServletRequest;
org.springframework.web.bind.annotation.RequestMapping;
org.springframework.web.bind.annotation.RequestMethod;
org.springframework.web.bind.annotation.RequestParam;
org.springframework.web.servlet.Model;
java.util.HashMap;
java.util.Map;
java.util.Date;
//假设存在对应的自定义工具类、实体类、响应类等的导入,例如
import com.example.utils.StringUtils;
import com.example.entity.UserPassword;
import com.example.entity.UserInformation;
import com.example.service.UserPasswordService;
import com.example.service.UserInformationService;
import com.example.response.BaseResponse;
//定义ForgetController类通常用于处理与忘记密码相关的业务逻辑比如验证手机号验证码、更新密码等操作
//在Spring MVC框架中一般作为一个控制器类来接收和处理对应的HTTP请求
public class ForgetController {
// 使用@Resource注解注入UserPasswordService用于处理用户密码相关业务逻辑由Spring框架自动装配
// 例如密码的查询、更新等操作会依赖这个服务类
@Resource
private UserPasswordService userPasswordService;
// 使用@Resource注解注入UserInformationService用于处理用户信息相关业务逻辑由Spring框架自动装配
// 像通过手机号查询用户ID等操作会通过这个服务类来完成
@Resource
private UserInformationService userInformationService;
// 处理验证手机号验证码的请求该方法可以接收POST和GET两种请求方式访问路径为"checkCode.do"
// 用于验证用户输入的验证码是否正确以及相关的验证逻辑根据验证结果返回包含验证结果状态的Map
@RequestMapping(value = "checkCode.do", method = {RequestMethod.POST, RequestMethod.GET})
public Map checkPhone(HttpServletRequest request, Model model,
@RequestParam String code, @RequestParam String token) {
// 创建一个Map对象用于存储验证结果相关信息这里以"result"作为键存储验证结果的整数值0表示失败1表示成功等情况
Map<String, Integer> map = new HashMap<>();
// 从HttpServletRequest中获取名为"name"的参数值,可能是用户相关的名称信息(具体看业务逻辑)
String name = request.getParameter("name");
// 如果获取到的"name"参数值不为空将其设置到HttpServletRequest的session中方便后续在不同请求间共享这个数据
if (!StringUtils.getInstance().isNullOrEmpty(name)) {
request.getSession().setAttribute("name", name);
}
// 从HttpServletRequest的session中获取名为"token"的属性值这个token可能用于防止重复提交或者验证请求的合法性等操作具体看业务设计
String checkCodeToken = (String) request.getSession().getAttribute("token");
// 如果获取到的token为空或者与传入的token不一致说明请求可能不合法或者存在重复提交等问题将验证结果设置为0表示失败并返回这个包含验证结果的Map
if (StringUtils.getInstance().isNullOrEmpty(checkCodeToken) ||!checkCodeToken.equals(token)) {
map.put("result", 0);
return map;
}
// 调用checkCodePhone方法验证用户输入的验证码code参数是否正确这里传入用户输入的验证码和HttpServletRequest对象
// 如果验证失败即验证码不正确将验证结果设置为0表示失败并返回这个包含验证结果的Map
if (!checkCodePhone(code, request)) {
map.put("result", 0);
return map;
}
// 如果前面的验证都通过说明验证码正确将验证结果设置为1表示成功并返回这个包含验证结果的Map
map.put("result", 1);
return map;
}
// 处理更新密码的请求,访问路径为"updatePassword.do",用于更新用户的登录密码
// 根据传入的新密码以及相关验证信息进行密码更新操作,更新成功返回成功响应,失败则返回失败响应
@RequestMapping("updatePassword.do")
public BaseResponse updatePassword(HttpServletRequest request, Model model,
@RequestParam String password, @RequestParam String token) {
// 防止重复提交从HttpServletRequest的session中获取名为"token"的属性值这个token用于验证当前请求是否重复
// 如果获取到的token为空或者与传入的token不一致说明可能是重复提交的请求直接返回表示失败的BaseResponse对象
String updatePasswordToken = (String) request.getSession().getAttribute("token");
if (StringUtils.getInstance().isNullOrEmpty(updatePasswordToken) ||!updatePasswordToken.equals(token)) {
return BaseResponse.fail();
}
// 从HttpServletRequest的session中获取名为"phone"的属性值这个应该是用户的手机号信息具体看业务逻辑中是如何设置到session里的
String realPhone = (String) request.getSession().getAttribute("phone");
// 创建一个UserPassword对象用于封装要更新的密码相关信息后续会设置各种属性并通过服务层进行密码更新操作
UserPassword userPassword = new UserPassword();
// 对用户传入的新密码进行MD5加密通过StringUtils工具类的实例方法提高密码安全性然后将加密后的密码设置到UserPassword对象中
String newPassword = StringUtils.getInstance().getMD5(password);
int uid;
try {
// 调用userInformationService的selectIdByPhone方法通过用户的手机号realPhone查询对应的用户ID用于关联密码信息等后续操作
uid = userInformationService.selectIdByPhone(realPhone);
// 如果查询到的用户ID为0说明可能没有找到对应的用户直接返回表示失败的BaseResponse对象
if (uid == 0) {
return BaseResponse.fail();
}
} catch (Exception e) {
// 如果在查询用户ID的过程中出现异常打印异常栈信息方便排查问题然后返回表示失败的BaseResponse对象
e.printStackTrace();
return BaseResponse.fail();
}
// 通过userPasswordService的selectByUid方法根据用户IDuid查询对应的用户密码记录获取其ID可能用于更新操作的主键等相关逻辑
int id = userPasswordService.selectByUid(uid).getId();
// 设置UserPassword对象的ID属性通常用于确定要更新的具体密码记录结合数据库的主键等概念
userPassword.setId(id);
// 设置UserPassword对象的用户ID属性关联到对应的用户
userPassword.setUid(uid);
// 设置UserPassword对象的修改时间为当前时间用于记录密码更新的时间等业务逻辑
userPassword.setModified(new Date());
// 设置UserPassword对象的密码为加密后的新密码
userPassword.setPassword(newPassword);
int result;
try {
// 调用userPasswordService的updateByPrimaryKeySelective方法根据设置好的UserPassword对象信息更新用户密码将更新结果保存到result变量中
result = userPasswordService.updateByPrimaryKeySelective(userPassword);
} catch (Exception e) {
// 如果在更新密码过程中出现异常直接返回表示失败的BaseResponse对象
return BaseResponse.fail();
}
// 如果更新结果不等于1通常1表示更新成功具体看服务层实现逻辑说明密码更新失败返回表示失败的BaseResponse对象
if (result!= 1) {
return BaseResponse.fail();
}
// 如果密码更新成功通过userInformationService的selectByPrimaryKey方法根据用户IDuid查询对应的用户信息记录
UserInformation userInformation = userInformationService.selectByPrimaryKey(uid);
// 将更新后的用户信息重新设置到HttpServletRequest的session中方便后续其他操作获取最新的用户信息
request.getSession().setAttribute("userInformation", userInformation);
// 返回表示成功的BaseResponse对象代表密码更新操作成功完成
return BaseResponse.success();
}
// 私有方法,用于检查用户输入的手机号验证码是否正确
// 参数codePhone是用户输入的验证码request是HttpServletRequest对象可能在更复杂的验证逻辑中会用到请求相关信息目前代码中只是简单对比固定值
private boolean checkCodePhone(String codePhone, HttpServletRequest request) {
// 定义一个固定的正确验证码值(这里只是示例,实际业务中应该是从数据库、缓存或者短信服务等获取正确验证码),目前设置为"12251103"
String trueCodePhone = "12251103";
// 通过比较用户输入的验证码codePhone和正确的验证码trueCodePhone是否相等返回验证结果相等则返回true表示正确不相等返回false表示错误
return codePhone.equals(trueCodePhone);
}
}