登入功能實現程式碼
阿新 • • 發佈:2019-01-04
簡單的登入程式碼實現
第一步,頁面提交登入請求
//form表單提交請求
<form id="loginFrom"> <div> <h2>管理登入</h2> <span><label>使用者名稱</label></span> <span><input type="text" id="sysloginname" name="sysloginname" onkeypress="if(event.keyCode==13){ doLogin()};" class="validate[require] textbox" maxlength="15"/></span> </div> <div> <span><label>密碼</label></span> <span><input type="password" id="sysloginpass" name="sysloginpass" onkeypress="if(event.keyCode==13){ doLogin()};" class="validate[require] password " maxlength="15"/></span> </div> <div class="sign"> <input type="button" value="登入" class="submit" onclick="doLogin();" /> </div> </form>
第二步,action中處理請求//登入JS function doLogin() { if ($("#loginFrom").validationEngine('validate')) { var submitOptions = { target : '#loginFrom', url : "${pageContext.request.contextPath}/dologin.htm", type : 'post', dataType : 'html', cache : false, async : false, error : function(xhr) { }, success : function(data) { var r = eval('(' + data + ')'); if (r.flag) { window.location.href = "${pageContext.request.contextPath}/toappmain.htm"; } else { alert(r.msg); } } } $('#loginFrom').ajaxSubmit(submitOptions); } }
@RequestMapping(value = "dologin.htm") public void doLogin(HttpSession httpsession, HttpServletRequest req,HttpServletResponse res, ModelMap model,String sysloginname,String sysloginpass) throws Exception{ if(checkStr(sysloginname)){ this.renderText("{'flag':false,'msg':'登入名錯誤!'}", res); } else if(checkStr(sysloginpass)){ this.renderText("{'flag':false,'msg':'登入密碼錯誤!'}", res); }else{ Map<String, Object> map = new HashMap<String, Object>(); map.put("sysloginname", sysloginname); map.put("sysloginpass", MD5.convert(sysloginpass)); MgrSysAccount appsysaccount = this.mgrSysAccountService.getLoginInfoByMap(map); if(appsysaccount!=null){ MgrSysAccount updatedate = new MgrSysAccount();//用於修改最後一次登入時間 updatedate.setId(appsysaccount.getId()); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); updatedate.setLastlogintime(simpleDateFormat.format(new Date())); mgrSysAccountService.updateEntity(updatedate); httpsession.setAttribute("syslogininfo", appsysaccount); this.renderText("{'flag':true}", res); }else{ this.renderText("{'flag':false,'msg':'登入失敗!'}", res); } } }
/**
* 校驗非法字元
* @param 校驗的字串
* @return
*/
public boolean checkStr(String str) {
if (StringUtils.isBlank(str.trim())) {
return true;
}
String inj_str = "‘,*,%,;,/,+,&,|,(,),$,!,[,]";
String[] inj_stra = inj_str.split(",");
for (int i = 0; i < inj_stra.length; i++) {
if (str.indexOf(inj_stra[i]) >= 0) {
return true;
}
}
return false;
}
/**
* 輸出Text型別
*
* @param response
* @param text
*/
protected void renderText(String text, HttpServletResponse res) {
render(text, "text/plain;charset=UTF-8", res);
}
/**
* 封裝response用於json輸出
*
* @param text
* @param contentType
*/
protected void render(String text, String contentType,
HttpServletResponse res) {
try {
res.setContentType(contentType);
res.getWriter().write(text);
res.getWriter().flush();
res.getWriter().close();
} catch (IOException e) {
System.out.println(e);
}
}