最近在编写论坛系统的实现,其中就涉及到用户登陆后保持会话直到浏览器关闭,同时可以使用cookie保存登陆信息以便在下次可以自动登陆。使用struts2框架实现。 下面是在action里登陆和注销的函数Java代码
public String logout() {
this.getSession().clear();
Cookie idCookie = new Cookie("id", "");
Cookie passwordCookie = new Cookie("password", "");
idCookie.setMaxAge(0); //使cookie失效
passwordCookie.setMaxAge(0);
idCookie.setPath("/"); //这个不能少
passwordCookie.setPath("/");
servletResponse.addCookie(idCookie);
servletResponse.addCookie(passwordCookie);
return SUCCESS;
}
//www.heatpress123.net
public String login() {
User user = userService.getUser(id, password);
if (user != null) {
this.getSession().put("user", id);
//如果选择保存登陆信息
if (saveLogin != null) {
Cookie idCookie = new Cookie("id", id); //可以使用md5或着自己的加密算法保存
Cookie passwordCookie = new Cookie("password", password);
idCookie.setPath("/webappName/"); //cookie路径问题,在我的其他文章里有专门的讲解
idCookie.setMaxAge(MAX_AGE);
passwordCookie.setPath("/webappName/");
passwordCookie.setMaxAge(MAX_AGE);
servletResponse.addCookie(idCookie);
servletResponse.addCookie(passwordCookie);
saveLogin = null;
}
return SUCCESS;
} else {
this.setInfo("用户名或密码错误");
return INFO;
}
}
在保存完cookie后要保证以后用户每次访问该系统的任意页面系统都必须先访问用户的cookie查看是否有登陆信息,这个使用servlet的filter实现最为方便。下面就是filter的代码
Java代码
private void doBeforeProcessing(ServletRequest request) {
HttpSession session = ((HttpServletRequest) request).getSession(true);
//首先检查session,若已经登陆则直接忽略一下代码
if (session.getAttribute("user") != null) {
return;
}
Cookie[] cookies = ((HttpServletRequest) request).getCookies();
String id = null;
String password = null;
if (cookies != null) {
for (Cookie c : cookies) {
if (c.getName().equals("id")) {
id = c.getValue();
}
if (c.getName().equals("password")) {
password = c.getValue();
}
}
}
UserService userService = (UserService)this.getApplicationContext().getBean("userService");
if (userService.isUser(id, password)) {
session = ((HttpServletRequest) request).getSession(true);
session.setAttribute("user", id);
}
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
doBeforeProcessing(request);
chain.doFilter(request, response);
}
其中涉及的在filter中访问spring的上下文
public String logout() {
this.getSession().clear();
Cookie idCookie = new Cookie("id", "");
Cookie passwordCookie = new Cookie("password", "");
idCookie.setMaxAge(0); //使cookie失效
passwordCookie.setMaxAge(0);
idCookie.setPath("/"); //这个不能少
passwordCookie.setPath("/");
servletResponse.addCookie(idCookie);
servletResponse.addCookie(passwordCookie);
return SUCCESS;
}
//www.heatpress123.net
public String login() {
User user = userService.getUser(id, password);
if (user != null) {
this.getSession().put("user", id);
//如果选择保存登陆信息
if (saveLogin != null) {
Cookie idCookie = new Cookie("id", id); //可以使用md5或着自己的加密算法保存
Cookie passwordCookie = new Cookie("password", password);
idCookie.setPath("/webappName/"); //cookie路径问题,在我的其他文章里有专门的讲解
idCookie.setMaxAge(MAX_AGE);
passwordCookie.setPath("/webappName/");
passwordCookie.setMaxAge(MAX_AGE);
servletResponse.addCookie(idCookie);
servletResponse.addCookie(passwordCookie);
saveLogin = null;
}
return SUCCESS;
} else {
this.setInfo("用户名或密码错误");
return INFO;
}
}
在保存完cookie后要保证以后用户每次访问该系统的任意页面系统都必须先访问用户的cookie查看是否有登陆信息,这个使用servlet的filter实现最为方便。下面就是filter的代码
Java代码
private void doBeforeProcessing(ServletRequest request) {
HttpSession session = ((HttpServletRequest) request).getSession(true);
//首先检查session,若已经登陆则直接忽略一下代码
if (session.getAttribute("user") != null) {
return;
}
Cookie[] cookies = ((HttpServletRequest) request).getCookies();
String id = null;
String password = null;
if (cookies != null) {
for (Cookie c : cookies) {
if (c.getName().equals("id")) {
id = c.getValue();
}
if (c.getName().equals("password")) {
password = c.getValue();
}
}
}
UserService userService = (UserService)this.getApplicationContext().getBean("userService");
if (userService.isUser(id, password)) {
session = ((HttpServletRequest) request).getSession(true);
session.setAttribute("user", id);
}
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
doBeforeProcessing(request);
chain.doFilter(request, response);
}
其中涉及的在filter中访问spring的上下文