js、Java后台设置cookie
概述:小开心下,又学到了点东西
JS设置cookie
//添加时间函数
function SetCookie(name, value){
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString(); //又学的一个重点
}
//获取时间函数
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;
}
本文出自 “小博客” 博客,请务必保留此出处http://9686567.blog.51cto.com/9676567/1606197
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。