java客户端对cookie的操作

来源:互联网 发布:经验模态分解算法 编辑:程序博客网 时间:2024/06/05 00:19
客户端代码如下:
public String login() throws IOException {UsernamePasswordToken token = new UsernamePasswordToken(username,password);Subject subject = SecurityUtils.getSubject();String error = null;if (subject.isAuthenticated()) {return SUCCESS;} else {try {subject.login(token);Session session = subject.getSession();// 记住用户名密码if (null != rememberPassword && rememberPassword) {byte[] username_key = new BASE64Decoder().decodeBuffer(DES3Utils.USERNAME_KEY);byte[] username_data = username.getBytes("UTF-8");byte[] username__str = DES3Utils.des3EncodeECB(username_key, username_data);// 加密String desUsername = new BASE64Encoder().encode(username__str);byte[] password_key = new BASE64Decoder().decodeBuffer(DES3Utils.PASSWORD_KEY);byte[] password_data = password.getBytes("UTF-8");byte[] password_str = DES3Utils.des3EncodeECB(password_key,password_data);// 加密String desPassword = new BASE64Encoder().encode(password_str);Cookie cookie = new Cookie("LOGIN_USERNAME", desUsername);Cookie cookie1 = new Cookie("LOGIN_PASSWORD", desPassword);Cookie cookie2 = new Cookie("LOGIN_REMEMBER",rememberPassword + "");cookie.setMaxAge(180 * 24 * 60 * 60);cookie1.setMaxAge(180 * 24 * 60 * 60);cookie2.setMaxAge(180 * 24 * 60 * 60);ServletActionContext.getResponse().addCookie(cookie);ServletActionContext.getResponse().addCookie(cookie1);ServletActionContext.getResponse().addCookie(cookie2);} else {// 删除cookieCookie cookie = new Cookie("LOGIN_USERNAME", "");Cookie cookie1 = new Cookie("LOGIN_PASSWORD", "");Cookie cookie2 = new Cookie("LOGIN_REMEMBER",rememberPassword + "");cookie.setMaxAge(0);cookie1.setMaxAge(0);cookie2.setMaxAge(0);ServletActionContext.getResponse().addCookie(cookie);ServletActionContext.getResponse().addCookie(cookie1);ServletActionContext.getResponse().addCookie(cookie2);}return SUCCESS;} catch (UnknownSessionException use) {ActionContext.getContext().getValueStack().set("userName", username);error = "异常会话";} catch (UnknownAccountException ex) {ActionContext.getContext().getValueStack().set("userName", username);error = "账号错误";} catch (IncorrectCredentialsException ice) {ActionContext.getContext().getValueStack().set("userName", username);error = "密码错误";} catch (LockedAccountException lae) {ActionContext.getContext().getValueStack().set("userName", username);error = "账号已被锁定,请与系统管理员联系";} catch (AuthenticationException ae) {ActionContext.getContext().getValueStack().set("userName", username);error = "您没有授权";} catch (Exception e) {ActionContext.getContext().getValueStack().set("userName", username);error = "出现未知异常,请与系统管理员联系";}ActionContext.getContext().getValueStack().set("error", error);return ERROR;}} 
页面获取cookie代码如下:
<%Cookie[] c = request.getCookies();if(null!=c){//request.getRequestDispatcher("/login").forward(request, response);for(int i = 0 ;i<c.length;i++){String key = c[i].getName();if(key.equals("LOGIN_USERNAME")){String username = c[i].getValue();byte[] username_data = DES3Utils.des3DecodeECB(new BASE64Decoder().decodeBuffer(DES3Utils.USERNAME_KEY),new BASE64Decoder().decodeBuffer(username));// 解密String desUsename = new String(username_data);request.setAttribute("cookie_username", desUsename);}else if(key.equals("LOGIN_PASSWORD")){String password = c[i].getValue();byte[] password_data = DES3Utils.des3DecodeECB(new BASE64Decoder().decodeBuffer(DES3Utils.PASSWORD_KEY),new BASE64Decoder().decodeBuffer(password));// 解密String desPassword = new String(password_data);request.setAttribute("cookie_password", desPassword);}else if(key.equals("LOGIN_REMEMBER")){String remeber = c[i].getValue();request.setAttribute("cookie_remember", remeber);}}} %>
<table ><tr><td >账    号</td><td height="40px" colspan="2" align="right"><input type="text"    style="width:220px;height:30px;line-height:30px;border-color:#5b97db;border-width: 1px;border-style: solid;" name="username" value="${userName==null?cookie_username:userName}"/></td></tr><tr><td >密    码</td><td height="40px" colspan="2" align="right"><input type="password" style="width:220px;height:30px;line-height:30px;border-color:#5b97db;border-width: 1px;border-style: solid;" name="password" value="${cookie_password}" /></td></tr><tr><td></td><td align="left"><input  type="checkbox" style="width:14px;height:14px;" <c:if test="${cookie_remember}">checked</c:if> name="rememberPassword" value="true" /><label style="font-size: 12px;">记住密码</label></td><td align="right"><input class="login"  type="submit" style="width:110px;height:33px;border:0" value="" /></td></tr></table>



 
原创粉丝点击