用户登录后的信息存取
2012-06-05 10:03
183 查看
这几天按照书上的案例对bbs的搭建多少学了点儿东西。其他的倒是没有什么,书上的编程规习惯很好,值得学习,类的划分和接口的定义对功能扩展提供了很大的方便。
在程序中,用到了验证拦截器 AuthenticationInterceptor 。具体代码如下:
用户登录的 action 类代码如下:
这样就把登录用户以 User 类的实例形式存储起来。每次要取出来使用的时候,只需实现 SessionAware 借口,并通过 Map session; session.get(...)的方式即可。如注销用户登录时的代码如下:
在程序中,用到了验证拦截器 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; } }
相关文章推荐
- 用Wireshark从http数据包中得到用户的登录信息
- android---数据保存之SQLite(实现登录用户信息保存)
- 批量查看域用户登录计算机信息
- 数据库之设计表:用户表信息以及第三方登录信息如何同步?
- 使用HttpClient 4.3.4 自动登录并抓取中国联通用户基本信息和账单数据,GET/POST/Cookie
- oracle获取当前登录用户的所有表,视图,sequence的相关信息
- Linux如何查看系统当前登录用户信息,linux su命令参数及用法详解(linux切换用户命令)
- Cas 3.5.2 登录成功后,如何返回用户更多信息?
- 超强获取计算机信息类(可获取cpu序列号,网卡地址,ip地址,硬盘id,登录系统用户,计算机名等等)
- ASP.NET 关于session和cookies保存用户登录信息
- Spring Cloud云架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)
- 通过cookie保存并读取用户登录信息
- Spring security 获取当前用户的登录信息
- 使用组策略获取AD中所有计算机当前登录用户信息
- 在用户登录期间显示有关登录以前的信息
- 基于xml制作登录注册修改用户信息
- Android采用SharedPreferences保存用户登录信息
- php实现简单的用户注册,登录,修改个人信息接口
- 如何知道所有在线的用户 (保存所有在线登录人信息,此处为后台人员)