1 import com.diyfintech.constant.Constant.SuperAdmin;
2 import com.diyfintech.pojo.SysUser;
3 import org.apache.log4j.Logger;
4
5 import javax.servlet.http.HttpServletRequest;
6 import java.util.List;
7 import java.util.Map;
8
9 /**
10 *
11 * Session 工具类
12 *
13 */
14 public final class SessionUtils {
15
16 protected static final Logger logger = Logger.getLogger(SessionUtils.class);
17
18 private static final String SESSION_USER = "session_user";
19
20 private static final String SESSION_VALIDATECODE = "session_validatecode";//验证码
21
22
23 private static final String SESSION_ACCESS_URLS = "session_access_urls"; //系统能够访问的URL
24
25
26 private static final String SESSION_MENUBTN_MAP = "session_menubtn_map"; //系统菜单按钮
27
28
29 /**
30 * 设置session的值
31 * @param request
32 * @param key
33 * @param value
34 */
35 public static void setAttr(HttpServletRequest request,String key,Object value){
36 request.getSession(true).setAttribute(key, value);
37 }
38
39
40 /**
41 * 获取session的值
42 * @param request
43 * @param key
44 */
45 public static Object getAttr(HttpServletRequest request,String key){
46 return request.getSession(true).getAttribute(key);
47 }
48
49 /**
50 * 删除Session值
51 * @param request
52 * @param key
53 */
54 public static void removeAttr(HttpServletRequest request,String key){
55 request.getSession(true).removeAttribute(key);
56 }
57
58 /**
59 * 设置用户信息 到session
60 * @param request
61 * @param user
62 */
63 public static void setUser(HttpServletRequest request,SysUser user){
64 request.getSession(true).setAttribute(SESSION_USER, user);
65 }
66
67
68 /**
69 * 从session中获取用户信息
70 * @param request
71 * @return SysUser
72 */
73 public static SysUser getUser(HttpServletRequest request){
74 return (SysUser)request.getSession(true).getAttribute(SESSION_USER);
75 }
76
77
78 /**
79 * 从session中获取用户信息
80 * @param request
81 * @return SysUser
82 */
83 public static void removeUser(HttpServletRequest request){
84 removeAttr(request, SESSION_USER);
85 }
86
87
88 /**
89 * 设置验证码 到session
90 * @param request
91 * @param validateCode
92 */
93 public static void setValidateCode(HttpServletRequest request,String validateCode){
94 request.getSession(true).setAttribute(SESSION_VALIDATECODE, validateCode);
95 }
96
97
98 /**
99 * 从session中获取验证码
100 * @param request
101 * @return SysUser
102 */
103 public static String getValidateCode(HttpServletRequest request){
104 return (String)request.getSession(true).getAttribute(SESSION_VALIDATECODE);
105 }
106
107
108 /**
109 * 从session中获删除验证码
110 * @param request
111 * @return SysUser
112 */
113 public static void removeValidateCode(HttpServletRequest request){
114 removeAttr(request, SESSION_VALIDATECODE);
115 }
116
117 /**
118 * 判断当前登录用户是否超级管理员
119 * @param request
120 * @return
121 */
122 public static boolean isAdmin(HttpServletRequest request){ //判断登录用户是否超级管理员
123 SysUser user = getUser(request);
124 if(user == null || !SuperAdmin.YES.key.endsWith(user.getIsSuperAdmin())){
125 return false;
126 }
127 return true;
128 }
129
130 /**
131 *设置当前用户可以访问的urls
132 * @param request
133 * @param accessUrls
134 */
135 public static void setAccessUrl(HttpServletRequest request,List<String> accessUrls){
136 setAttr(request, SESSION_ACCESS_URLS, accessUrls);
137 }
138
139
140
141 /**
142 * 判断URL是否可访问
143 * @param request
144 * @return
145 */
146 public static boolean isAccessUrl(HttpServletRequest request,String url){
147 List<String> accessUrls = (List)getAttr(request, SESSION_ACCESS_URLS);
148 if(accessUrls == null ||accessUrls.isEmpty() || !accessUrls.contains(url)){
149 return false;
150 }
151 return true;
152 }
153
154
155 /**
156 * 设置菜单按钮
157 * @param request
158 * @param btnMap
159 */
160 public static void setMemuBtnMap(HttpServletRequest request,Map<String,Object> btnMap){ //判断登录用户是否超级管理员
161 setAttr(request, SESSION_MENUBTN_MAP, btnMap);
162 }
163
164 /**
165 * 获取菜单按钮
166 * @param request
167 * @param menuUri
168 */
169 public static List<String> getMemuBtnListVal(HttpServletRequest request,String menuUri){
170 Map btnMap = (Map)getAttr(request, SESSION_MENUBTN_MAP);
171 if(btnMap == null || btnMap.isEmpty()){
172 return null;
173 }
174 return (List<String>)btnMap.get(menuUri);
175 }
176
177 }