Struts2保存session 实现自动登陆
2013-06-07 16:13 这个男人来自地球 阅读(556) 评论(0) 编辑 收藏 举报action:
public static final String USER_SESSION = "user.session";
private HttpServletResponse response;
private HttpServletRequest request;
private Map<String, Object> session;
private CookieUtil cookieUtil = new CookieUtil();
private UserDAO userDao = new UserDAOImpl();
// private String username;
// private String password;
private boolean userCookie;
public String login() {
System.out.println("-------------------------ddddddddddddddddddddddddddd a ");
if (cookieUtil.getCookie(request, userDao))
{
System.out.println("-------------------------ddddddddddddddddddddddddd b ");
return SUCCESS;
}
else
return "login";
}
@Override
// 正常登录
public String execute() throws Exception {
String username = user.getUserName();
String password=user.getPassword();
System.out.println(" password "+password);
if (username != null && !"".equals(username) && password!= null
&& !"".equals(password)) {
User user = userDao.checkUser(username,password);
// System.out.println(user);
if (user != null) {
// 判断是否要添加到cookie中
if (userCookie) {
Cookie cookie = cookieUtil.addCookie(user);
System.out.println("coooi ssssssssssssssssssssssssssssssss"+cookie);
response.addCookie(cookie);// 添加cookie到response中
// response.addCookie(cookie);
System.out.println("coooi ooooooooooooooooooooooooooooooooooooo");
}
// 2.将user 设置到session中
// session = ActionContext.getContext().getSession()
session=ActionContext.getContext().getSession();
session.put(USER_SESSION, user);
System.out.println("session "+ServletActionContext.getRequest().getSession());
return SUCCESS;
}
}
// this.addFieldError("username", "用户名或密码错误!");
return "login";
}
// 用户退出
public String logout() {
HttpSession session = ServletActionContext.getRequest().getSession(false);
if (session != null)
session.removeAttribute(USER_SESSION);
Cookie cookie = cookieUtil.delCookie(request);
if (cookie != null)
response.addCookie(cookie);
return "login";
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
// public HttpServletRequest getRequest() {
// return request;
// }
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public boolean isUserCookie() {
return userCookie;
}
public void setUserCookie(boolean userCookie) {
this.userCookie = userCookie;
}
// public Map<String, Object> getSession() {
// return session;
// }
public void setSession(Map<String, Object> session) {
this.session = session;
}
public void setServletResponse(HttpServletResponse arg0) {
this.response=arg0;
}
CookieUtil类
package com.gcf.common;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import com.gcf.action.UserAction;
import com.gcf.bean.User;
import com.gcf.dao.UserDAO;
/**
* cookie的增加、删除、查询
*/
public class CookieUtil {
public static final String USER_COOKIE = "user.cookie";
// 添加一个cookie
public Cookie addCookie(User user) {
Cookie cookie = new Cookie(USER_COOKIE, user.getUserName() + ","
+ user.getPassword());
System.out.println("添加cookie");
System.out.println("添加cookie---"+cookie);
cookie.setMaxAge(60 * 60 * 24 * 14);// cookie保存两周
return cookie;
}
// 得到cookie
public boolean getCookie(HttpServletRequest request, UserDAO userDAO) {
System.out.println("------------------------------s ccc");
// Cookie[] cookies = request.getCookies();
Cookie[] cookies=ServletActionContext.getRequest().getCookies();
System.out.println("------------------------------o ccc");
System.out.println("cookies: " + cookies);
if (cookies != null) {
for (Cookie cookie : cookies) {
System.out.println("cookie: " + cookie.getName());
if (CookieUtil.USER_COOKIE.equals(cookie.getName())) {
String value = cookie.getValue();
if (StringUtils.isNotBlank(value)) {
String[] split = value.split(",");
String username = split[0];
String password = split[1];
User user = userDAO.checkUser(username, password);
if (user != null) {
// HttpSession session =request.getSession();
HttpSession session=ServletActionContext.getRequest().getSession();
System.out.println("cook session "+session);
// session.setAttribute(UserAction.USER_SESSION, user);// 添加用户到session中
return true;
}
}
}
}
}
return false;
}
// 删除cookie
public Cookie delCookie(HttpServletRequest request) {
Cookie[] cookies = ServletActionContext.getRequest().getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (USER_COOKIE.equals(cookie.getName())) {
cookie.setValue("");
cookie.setMaxAge(0);
return cookie;
}
}
}
return null;
}
}
配置文件:
<action name="login" class="com.gcf.action.UserAction" >
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="token"></interceptor-ref>
<result name="success" type="redirect">/user/success.jsp</result>
<result name="login">/user/login.jsp</result>
<result name="invalid.token">/user/login.jsp</result>
</action>
登陆页面:login.jsp
<s:form action="login" namespace="/user">
<s:textfield label="姓名:" name="user.userName"></s:textfield>
<s:password label="密码" name="user.password"></s:password>
<s:checkbox label="自动登录" name="userCookie" value="true"></s:checkbox>
<s:token></s:token>
<s:submit label="提交" value="提交"></s:submit>
</s:form>
首页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
response.sendRedirect(basePath+"user/login!login.action");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
</body>
</html>
成功页面
<h1>欢迎您的到来!</h1><br>
<s:a action="login!logout.action" namespace="/user"> 安全退出</s:a>