流程:首先直接访问jsp,呈现登录页面,填好用户名、密码后,点击登录进行验证。
action验证时若没有登录码,则生成一个。并写进数据库。
页面验证到时候,取到action传递的参数,设置cookie。
java:
ServerUserBean userBean = loginService.login(shopNo, username, password);
if (null == userBean) {
request.setAttribute("flag", "0");
} else {
request.setAttribute("flag", "1");
request.setAttribute("key", key);
String logKey = userBean.getLogKey();
request.setAttribute("logKey", StringUtils.isBlank(logKey) ? "0" : "1");//是否存在登录码
if (StringUtils.isBlank(logKey)) {
String val = RandomUtil.getString(10);
request.setAttribute("val", val);
this.loginService.updLogKey(val, userBean.getShopId());
} else {
request.setAttribute("val", logKey);
}
ActionContext.getContext().getSession().put("shopId", userBean.getShopId());
ActionContext.getContext().getSession().put("shopName", userBean.getShopName());
}
jsp:
<li id="logMsg" style="padding-left: 60px"><font color="red">
<span id="errorMsg"></span>
<%
if("0".equals((String)request.getAttribute("flag"))){
out.print("工号、用户名或密码错误!");
} else if("1".equals((String)request.getAttribute("flag"))){
%>
<script type="text/javascript">
var key = '<%=request.getAttribute("key") %>';
var val = '<%=request.getAttribute("val") %>';
if('<%=request.getAttribute("logKey") %>' == '0'){
SetCookie(key, val);
}
if(val == GetCookie(key)){
window.location = prjContextPath + '/main.do';
} else {
$('errorMsg').innerHTML = '请向代理商索要授权!';
}
</script>
<%
} else if("2".equals((String)request.getAttribute("flag"))){
out.print("请先登录系统!");
}
%>
</font></li>
//**
* 获得Cookie的原始值
* @param name
* @return
*/
function GetCookie(name){
var arr = document.cookie.split(";");
for(var m = 0; m < arr.length; m++){
var temp = arr[m].split("=");
if(temp[0].replace(" ","") == name){
return unescape(temp[1]);
}
}
}
/**
* 设定Cookie值
* @param name
* @param value
* @return
*/
function SetCookie(name, value){
var expdate = new Date();
expdate.setTime(expdate.getTime() + (30 * 24 * 3600 * 1000));
document.cookie = name + "=" + escape(value) + ("; expires=" + expdate.toGMTString());
}
/**
* 删除Cookie
* @param name
* @return
*/
function DelCookie(name){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = GetCookie(name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
action验证时若没有登录码,则生成一个。并写进数据库。
页面验证到时候,取到action传递的参数,设置cookie。
java:
ServerUserBean userBean = loginService.login(shopNo, username, password);
if (null == userBean) {
request.setAttribute("flag", "0");
} else {
request.setAttribute("flag", "1");
request.setAttribute("key", key);
String logKey = userBean.getLogKey();
request.setAttribute("logKey", StringUtils.isBlank(logKey) ? "0" : "1");//是否存在登录码
if (StringUtils.isBlank(logKey)) {
String val = RandomUtil.getString(10);
request.setAttribute("val", val);
this.loginService.updLogKey(val, userBean.getShopId());
} else {
request.setAttribute("val", logKey);
}
ActionContext.getContext().getSession().put("shopId", userBean.getShopId());
ActionContext.getContext().getSession().put("shopName", userBean.getShopName());
}
jsp:
<li id="logMsg" style="padding-left: 60px"><font color="red">
<span id="errorMsg"></span>
<%
if("0".equals((String)request.getAttribute("flag"))){
out.print("工号、用户名或密码错误!");
} else if("1".equals((String)request.getAttribute("flag"))){
%>
<script type="text/javascript">
var key = '<%=request.getAttribute("key") %>';
var val = '<%=request.getAttribute("val") %>';
if('<%=request.getAttribute("logKey") %>' == '0'){
SetCookie(key, val);
}
if(val == GetCookie(key)){
window.location = prjContextPath + '/main.do';
} else {
$('errorMsg').innerHTML = '请向代理商索要授权!';
}
</script>
<%
} else if("2".equals((String)request.getAttribute("flag"))){
out.print("请先登录系统!");
}
%>
</font></li>
//**
* 获得Cookie的原始值
* @param name
* @return
*/
function GetCookie(name){
var arr = document.cookie.split(";");
for(var m = 0; m < arr.length; m++){
var temp = arr[m].split("=");
if(temp[0].replace(" ","") == name){
return unescape(temp[1]);
}
}
}
/**
* 设定Cookie值
* @param name
* @param value
* @return
*/
function SetCookie(name, value){
var expdate = new Date();
expdate.setTime(expdate.getTime() + (30 * 24 * 3600 * 1000));
document.cookie = name + "=" + escape(value) + ("; expires=" + expdate.toGMTString());
}
/**
* 删除Cookie
* @param name
* @return
*/
function DelCookie(name){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = GetCookie(name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}