- 属性定义所需的session的key值
- 定义set方法存放值进session的中
- 定义get方法获取对应的session的value值
- 调用了getSession方法,是springmvc提供的方法(copy进来的),可以直接获取当前session对象
代码演示
public class UserContext {
// 抽取常量session中使用的key值
public static final String USER_IN_SESSION = "USER_IN_SESSION";
public static final String PREMISSION_IN_SESSION = "PREMISSION_IN_SESSION";
public static HttpSession getSession() {
ServletRequestAttributes attrs =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
return attrs.getRequest().getSession();
}
public static void setCurrentUser(Employee employee) {
//把用户信息存到session
getSession().setAttribute(UserContext.USER_IN_SESSION, employee);
}
public static void setPremissions(List<String> list) {
//把权限信息存到session
getSession().setAttribute(UserContext.PREMISSION_IN_SESSION, list);
}
public static Employee getCurrentUser() {
// session中取用户信息
return (Employee) getSession().getAttribute(USER_IN_SESSION);
}
public static List<String> getPermissions() {
// 从session中取权限信息
return (List<String>) getSession().getAttribute(PREMISSION_IN_SESSION);
}
}
调用演示