Helius
2021-01-06 8eb03554c07de7e268cf230249caf10922c6b47d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package com.matrix.system.hive.plugin.util;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
import com.matrix.core.tools.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
/**
 * session工具,获取对象,转换对象
 * @author Ron
 * @createTime 2014.08.30
 */
public class SessionUtils {
 
    /**
     * 从session取值
     * 
     * @param request
     * @param key
     * @return
     */
    public static Object getSessionValue(HttpServletRequest request, String key) {
 
        if (StringUtils.isBlank(key) || request == null) {
            return null;
        }
 
        Object obj = request.getSession().getAttribute(key);
 
        return obj;
    }
 
    /**
     * 从session取值
     * 
     * @param request
     * @param key
     * @return
     */
    public static Object getSessionValue(HttpServletRequest request, String key, Object defaultValue) {
 
        if (StringUtils.isBlank(key) || request == null) {
            return defaultValue;
        }
 
        Object obj = request.getSession().getAttribute(key);
 
        return obj;
    }
 
    /**
     * 存放值到session
     * 
     * @param request
     * @param key
     * @param value
     */
    public static void setSessionValue(HttpServletRequest request, String key, Object value) {
 
        if (StringUtils.isBlank(key) || request == null || value == null) {
            return;
        }
 
        request.getSession(true).setAttribute(key, value);
    }
 
    /**
     * 把session中Object对象转换成为bean
     * 
     * @param <T>
     * @param request
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getSessionObject(HttpServletRequest request, String key) {
 
        Object obj = getSessionValue(request, key);
        if (obj == null) {
            return null;
        } else {
            return (T) obj;
        }
    }
 
    /**
     * 把session中Object对象转换成为bean
     * 
     * @param <T>
     * @param request
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getSessionObject(HttpServletRequest request, String key, Object defaultValue) {
 
        Object obj = getSessionValue(request, key);
        if (obj == null) {
            obj = defaultValue;
        }
        return (T) obj;
    }
 
    /**
     * 把session中Object对象转换成为bean
     * 
     * @param <T>
     * @param request
     * @param key
     * @return
     */
    public static Long getSessionLong(HttpServletRequest request, String key) {
 
        Object obj = getSessionValue(request, key);
        if (obj == null) {
            return Long.valueOf(0);
        } else {
            return Long.valueOf(obj.toString());
        }
    }
 
    /**
     * 把session中Object对象转换成为bean
     * 
     * @param <T>
     * @param request
     * @param key
     * @return
     */
    public static Long getSessionLong(HttpServletRequest request, String key, Long defaultValue) {
 
        Object obj = getSessionValue(request, key);
        if (obj == null) {
            return defaultValue;
        }
        return Long.valueOf(obj.toString());
    }
 
    /**
     * 把session中key所对应的对象移除
     * @param request
     * @param key
     */
    public static void removeSessionObject(HttpServletRequest request, String key) {
 
        if (StringUtils.isNotBlank(key)) {
            request.getSession().removeAttribute(key);
        }
    }
 
    /**
     * 获取session对象
     */
    public static HttpSession getSession() {
 
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getSession();
    }
 
    /**
     * 把session中Object对象转换成为bean
     * 
     * @param <T>
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getSessionObject(String key) {
 
        Object obj = getSession().getAttribute(key);
        if (obj == null) {
            return null;
        }
        return (T) obj;
    }
 
    /**
     * 把session中Object对象转换成为bean
     * 
     * @param <T>
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getSessionObject(String key, Object defaultValue) {
 
        Object obj = getSession().getAttribute(key);
        if (obj == null) {
            obj = defaultValue;
        }
        return (T) obj;
    }
 
    /**
     * 把session中Object对象转换成为bean
     * 
     * @param <T>
     * @param key
     * @return
     */
    public static String getSessionString(String key) {
 
        Object obj = getSession().getAttribute(key);
        if (obj == null) {
            return null;
        }
        return (String) obj;
    }
 
    /**
     * 把session中Object对象转换成为bean
     * 
     * @param <T>
     * @param key
     * @return
     */
    public static String getSessionString(String key, String defaultValue) {
 
        Object obj = getSession().getAttribute(key);
        if (obj == null) {
            return defaultValue;
        }
        return (String) obj;
    }
 
    /**
     * 把session中Object对象转换成为bean
     * 
     * @param <T>
     * @param key
     * @return
     */
    public static Long getSessionLong(String key) {
 
        Object obj = getSession().getAttribute(key);
        if (obj == null) {
            return Long.valueOf(0);
        }
        return Long.valueOf(obj.toString());
    }
 
    /**
     * 把session中Object对象转换成为bean
     * 
     * @param <T>
     * @param key
     * @return
     */
    public static Long getSessionLong(String key, Long defaultValue) {
 
        Object obj = getSession().getAttribute(key);
        if (obj == null) {
            return defaultValue;
        }
        return Long.valueOf(obj.toString());
    }
 
    /**
     * session失效
     */
    public static void invalidate(HttpServletRequest request) {
 
        request.getSession().invalidate();
    }
    
}