java客户端对cookie的操作


客户端代码如下:
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 {
					// 删除cookie
					Cookie 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>





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值