您的位置:首页 > 其它

用户登录后的信息存取

2012-06-05 10:03 183 查看
这几天按照书上的案例对bbs的搭建多少学了点儿东西。其他的倒是没有什么,书上的编程规习惯很好,值得学习,类的划分和接口的定义对功能扩展提供了很大的方便。

在程序中,用到了验证拦截器 AuthenticationInterceptor 。具体代码如下:

import java.util.Map;

import org.model.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class AuthenticationInterceptor implements Interceptor  {

private static final long serialVersionUID = 1L;

private String message;

public static final String USER_SESSION_KEY = "UserSessionKey";

public void destroy() {}

public void init() {}

public String intercept(ActionInvocation actionInvocation) throws Exception {
//取得Session
Map session = actionInvocation.getInvocationContext().getSession();
//从Session里获得登录时保存进session的User类
User user = (User) session.get(USER_SESSION_KEY);
//如果用户为空,返回登录页面
if (user==null||!user.getGrade().equals("2")) {
this.setMessage("您没登陆或不是管理员");
return Action.INPUT;
}
return actionInvocation.invoke();//返回验证通过
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}


用户登录的 action 类代码如下:

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.dao.impl.UserDaoImpl;
import org.model.User;
import org.web.interceptor.AuthenticationInterceptor;

import com.opensymphony.xwork2.ActionSupport;

public class UserLoginAction extends ActionSupport implements SessionAware{
private UserDaoImpl dao = new UserDaoImpl();

private String username;
private String password;
private Map session;

@Override
public String execute() throws Exception {
User user = dao.isValidUser(username, password);
if (null != user) {
session.put(AuthenticationInterceptor.USER_SESSION_KEY, user);
return SUCCESS;
} else {
addActionError("用户名与密码不匹配");
return INPUT;
}
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Map getSession() {
return session;
}

public void setSession(Map session) {
this.session = session;
}
}


这样就把登录用户以 User 类的实例形式存储起来。每次要取出来使用的时候,只需实现 SessionAware 借口,并通过 Map session; session.get(...)的方式即可。如注销用户登录时的代码如下:

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.model.User;
import org.web.interceptor.AuthenticationInterceptor;

import com.opensymphony.xwork2.ActionSupport;

public class UserLogoutAction extends ActionSupport implements SessionAware{

private Map session;

public String execute()throws Exception{

//从session取出登陆用户
System.out.println("******************session====" + session);
User user =(User)session.get(AuthenticationInterceptor.USER_SESSION_KEY);
if(user!=null){
//从session删除用户
session.remove(AuthenticationInterceptor.USER_SESSION_KEY);
addActionError("已注销");
}
return INPUT;
}

public Map getSession() {
return session;
}

public void setSession(Map session) {
this.session = session;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: