public String reg(Request req){ String phone = req.params.get("phone"); String pwd = req.params.get("pwd"); return new BaseDao() .upd("insert into user_zz values(null,?,?)",phone,pwd); }
时间: 2024-04-11 12:32:31 浏览: 99
你好!根据你提供的代码,这是一个用于用户注册的方法。它接收一个请求对象 `req`,从中获取手机号码和密码。然后,它使用 `BaseDao` 类的 `upd` 方法执行插入语句,将手机号码和密码插入到 `user_zz` 表中,并返回插入操作的结果。插入语句中的 `null` 表示自动递增的主键值。
需要注意的是,这段代码可能存在一些潜在的问题或安全隐患。例如,没有对输入进行验证和过滤,可能会导致 SQL 注入攻击。另外,存储密码时应该使用哈希函数进行加密,以增加安全性。
相关问题
@PostMapping("/studentreg") @ResponseBody public JsonDto studentreg(HttpServletRequest req){ String studentname = req.getParameter("name"); String studenttel = req.getParameter("tel"); String studentpwd = req.getParameter("pwd"); String studentcard = req.getParameter("card"); String studentemail = req.getParameter("email"); String StudentPwd = DigestUtils.md5Hex(studentpwd); int studentCount = studentService.getStudentCount(studenttel); int studentcount = studentService.getStudentcount(studentcard); if (studentCount > 0 || studentcount > 0){ return new JsonDto(10009, "err", null); } else { StudentInfo studentInfo = new StudentInfo(-1, studentname, studenttel, StudentPwd, studentcard, studentemail, new Date()); int regRs = studentService.studentReg(studentInfo); if (regRs > 0) { JsonDto jsonDto = new JsonDto(200, "success", "page?p=studentLogin"); System.out.println("studentreg..success"); return jsonDto; }else { return new JsonDto(10009, "err", null); } } }
这段代码也是一个使用Spring框架编写的Java后端接口。它的作用是从HTTP请求中获取学生注册信息,包括姓名、电话、密码、身份证号码和电子邮件地址,然后将这些信息存储到数据库中。
接口先使用MD5算法对密码进行加密,然后检查电话号码和身份证号码是否已经存在于数据库中。如果存在,则返回一个状态码为10009,消息为"err",data为null的JsonDto对象;否则,将学生信息插入到数据库中,并返回一个状态码为200,消息为"success",重定向页面为"page?p=studentLogin"的JsonDto对象。
此接口的实际作用是实现学生注册功能。它会根据学生提供的注册信息,创建一个新的学生账号。如果注册成功,会将用户重定向到登录页面;否则,会提示用户注册失败。
router.post("/login", (req, res) => { const username = req.body.user; const password = req.body.pwd; //查询数据库 // let {username,password} = req.body // console.log(username) // console.log(password) //请求数据库 let sql = "select * from userinfo where username=? and password=?"; let arr = [username,password] console.log(sql,arr); sqlFn(sql,arr,result => { if(result.length > 0){ let token = jwt.sign({ username:result[0].username, id:result[0].id }, config.jwtSecert, { expiresIn: 60 *60*1 } ); res.send({ status:200, data:token, }) }else{ return res.send({ status:404, msg:'用户不存在或密码错误', data:null }) } }) /* if (username === 'admin') {//超级管理员 res.send(adminLogin) } else { res.send(vipLogin)//普通VIP用户 } */ })这两段代码,怎么写到一块
你可以把两段代码合并成一个,如下所示:
```js
router.post("/login", (req, res) => {
const username = req.body.user;
const password = req.body.pwd;
let sql = "select * from userinfo where username=? and password=?";
let arr = [username, password];
console.log(sql, arr);
sqlFn(sql, arr, (result) => {
if (result.length > 0) {
let token = jwt.sign(
{
username: result[0].username,
id: result[0].id,
},
config.jwtSecert,
{
expiresIn: 60 * 60 * 1,
}
);
res.send({
status: 200,
data: token,
});
} else {
return res.send({
status: 404,
msg: "用户不存在或密码错误",
data: null,
});
}
});
});
```
在合并代码时,需要注意以下几点:
1. 把两段代码的函数体合并成一个。
2. 把 `let {username,password} = req.body` 这行代码删除,因为已经不需要了。
3. 把之前注释掉的代码段也删除,因为它已经不需要了。
4. 将 `sql` 和 `arr` 定义在函数内部,因为它们只在这个函数中使用,不需要全局定义。
5. 把 `res.send()` 中的数据格式保持一致,这样可以方便前端处理。
阅读全文
相关推荐
















