概述:小開心下,又學到了點東西數據庫
JS設置cookie數組
//添加時間函數瀏覽器
function SetCookie(name, value){cookie
var Days = 30;session
var exp = new Date();app
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);ide
document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString(); //又學的一個重點函數
}ui
//獲取時間函數spa
function GetCookie(name){
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr != null) return unescape(arr[2]); return null;
}
//刪除時間函數
function DelCookie(name){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = GetCookie(name);
if (cval != null) document.cookie = name + "=" + cval + ";path=/;expires=" + exp.toGMTString();
}
Java後臺設置COOKIE和取值COOKIE
/**
* 名片登陸請求
* @param modelMap
* @param session
* @param response
* @param account
* @param password
* @param autoLogin
* @return
*/
@RequestMapping(value = "crm_micro_business_card_login_card.do")
public String login(ModelMap modelMap,HttpSession session,HttpServletResponse response,@RequestParam(value = "account",required=false) String account,
@RequestParam(value = "password",required=false) String password,@RequestParam(value = "autoLogin",required=false) String autoLogin){
String forward = "redirect:/crm_micro_business_card_businessCardManager_card.do";
try {
CrmMicroBusinessCardVO crmMicroBusinessCardVO = mpBasInfoService.doLogin(account,password);
Cookie ckUsername, ckSessionId;
if(autoLogin==null){
session.setAttribute("crmMicroBusinessCardVO", crmMicroBusinessCardVO);
forward = forward+"?id="+crmMicroBusinessCardVO.getId();
}else if(autoLogin.equals("on")){
ckUsername = new Cookie("autoLoginUser",account);
ckUsername.setMaxAge(60*60*24*30);
response.addCookie(ckUsername); //保存登陸用戶名到COOKIE
String sessionId = session.getId();
ckSessionId = new Cookie("sessionId",sessionId);
ckSessionId.setMaxAge(60*60*24*30);
response.addCookie(ckSessionId); //保存登陸sessionId到COOKIE
MpAccountSessionId mpAccountSessionId = new MpAccountSessionId();
mpAccountSessionId.setAccount(account);
mpAccountSessionId.setSessionId(sessionId);
mpBasInfoService.saveSessionId(mpAccountSessionId); //保存登陸用戶名和sessionId到數據庫
session.setAttribute("crmMicroBusinessCardVO", crmMicroBusinessCardVO);
forward = forward+"?id="+crmMicroBusinessCardVO.getId();
}
} catch (BusinessCardException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
modelMap.put("errorMsg",e.getMessage());
modelMap.put("account",account);
modelMap.put("password",password);
forward = "/tools/businessCardLogin";
}
return forward;
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//To change body of implemented methods use File | Settings | File Templates.
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpSession session = request.getSession(true);
String account = "";
String sessionId = ""; // 此sessionid是上次用戶登陸時保存於用戶端的識別碼,用於用戶後續訪問的自動登陸。不是本次訪問的session id。
Cookie[] cookies;
CookieManager cm = new CookieManager(); // CookieManager是一個自定義的類,用於從Cookie數組中查找並返回指定名稱的Cookie值。
boolean isAutoLogin;
// 若是session中沒有user對象,則建立一個。
CrmMicroBusinessCardVO crmMicroBusinessCardVO = (CrmMicroBusinessCardVO) session.getAttribute("crmMicroBusinessCardVO");
// 若是user對象的username爲"",表示用戶未登陸。則執行自動登陸過程。
// 不然不自動登陸。
if (crmMicroBusinessCardVO == null) {
crmMicroBusinessCardVO = new CrmMicroBusinessCardVO();
// 檢查用戶瀏覽器是否發送了上次登陸的用戶名和sessionid,
// 若是是,則爲用戶自動登錄。
cookies = request.getCookies();
if(cookies!=null&&cookies.length>0){
account = cm.getCookieValue(cookies, "autoLoginUser");
sessionId = cm.getCookieValue(cookies, "sessionId");
account = "".equals(account)?null:account;
sessionId = "".equals(sessionId)?null:sessionId;
Map<String,Object> map = new HashMap<String,Object>();
map.put("account", account);
map.put("sessionId",sessionId);
boolean isExist = basDao.isExist(MpAccountSessionId.class,map);
if (isExist) {
MpBasInfo userVO = new MpBasInfo();
userVO = basDao.queryOne(MpBasInfo.class,"account",account);
if(userVO!=null){
CrmMicroBusinessCardVO crmMicroBusinessCardVO1 = new CrmMicroBusinessCardVO();
UtilBean.copyBeanAllSimpleProperties(crmMicroBusinessCardVO1,userVO,false);
session.setAttribute("crmMicroBusinessCardVO", crmMicroBusinessCardVO1); // 將user bean添加到session中。
}
}
}
}
filterChain.doFilter(servletRequest, servletResponse);
}
public class CookieManager {
public static String getCookieValue(Cookie cookies[],String cookieName){
String cookieValue = "";
for(int i=0;i<cookies.length;i++){
Cookie cookie = cookies[i];
if(cookie.getName().equals(cookieName)){
cookieValue = cookie.getValue().toString();
break;
}
}
return cookieValue;
}