本人初级小菜鸟一枚,在最近的学习中,练习套模板,在登录验证中有所感悟,所以写下来,写的不好的话请见谅 其中user是我的用户信息表
Controller中的代码
@RequestMapping(value
= "dologin")
@ResponseBody
public JSONObject
dologin(HttpServletRequest request
,Model model
) {
HttpSession session
= request
.getSession();
JSONObject json
= new JSONObject();
String uname
= request
.getParameter("uname");
String pwd
= request
.getParameter("pwd");
String code
= request
.getParameter("code").toLowerCase();
if (!code
.equals(session
.getAttribute("verCode"))) {
json
.put("succes", 3);
json
.put("info", "验证码不正确!");
return json
;
}
User user
= userService
.findLogin(uname
, pwd
);
if (user
== null
||user
.equals("") ) {
json
.put("succes", 2);
json
.put("info", "用户名或者验证码错误!");
} else {
json
.put("succes", 1);
session
.setAttribute("name", uname
);
json
.put("info", "登录成功!");
}
return json
;
}
@RequestMapping(value
= "login")
public String
login(HttpServletRequest request
,Model model
) {
return "pages/admin/login";
}
@RequestMapping(value
= "yzm")
public void yzm(HttpServletRequest request
,HttpServletResponse response
) throws IOException
{
response
.setHeader("Pragma", "No-cache");
response
.setHeader("Cache-Control", "no-cache");
response
.setDateHeader("Expires", 0);
response
.setContentType("image/jpeg");
String verifyCode
= VerifyCodeUtils
.generateVerifyCode(4);
HttpSession session
= request
.getSession(true);
session
.removeAttribute("verCode");
session
.removeAttribute("codeTime");
session
.setAttribute("verCode", verifyCode
.toLowerCase());
session
.setAttribute("codeTime", LocalDateTime
.now());
int w
= 100, h
= 30;
OutputStream out
= response
.getOutputStream();
VerifyCodeUtils
.outputImage(w
, h
, out
, verifyCode
);
}
上面代码,看注解 双引号中的name与拦截器的name是同一变量,变量名需要一致,拦截器在其他文章
前端页面代码
在前端页面中,需要在用户名和密码还有输入验证的信息中的中加上v-model进行双向绑定 在登录按钮上进行login方法的调用
<script>
var vm
= new Vue({
el
:"#app",
data
:{
uname
:"",
pwd
:"",
code
:"",
},
mounted(){
},
methods
:{
login
:function(){
$
.post("/user/dologin",{uname
:this.uname
,pwd
:this.pwd
,code
:this.code
},function(json
){
if (json
.succes
==1) {
alert(json
.info
);
window
.location
.href
="/indexs/index";
}else {
alert(json
.info
);
}
});
},
}
});
</script
>