提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.sys.core.auth.util; |
懒 |
2 |
|
|
3 |
import cn.stylefeng.roses.core.util.HttpContext; |
|
4 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
5 |
|
|
6 |
import javax.servlet.http.Cookie; |
|
7 |
import javax.servlet.http.HttpServletRequest; |
|
8 |
|
|
9 |
import static cn.stylefeng.guns.base.consts.ConstantsContext.getTokenHeaderName; |
|
10 |
|
|
11 |
/** |
|
12 |
* 获取token的封装 |
|
13 |
* |
|
14 |
* @author fengshuonan |
|
15 |
* @Date 2020/2/16 22:51 |
|
16 |
*/ |
|
17 |
public class TokenUtil { |
|
18 |
|
|
19 |
/** |
|
20 |
* 获取token的两种方法 |
|
21 |
* |
|
22 |
* @author fengshuonan |
|
23 |
* @Date 2020/2/16 22:51 |
|
24 |
*/ |
|
25 |
public static String getToken() { |
|
26 |
|
|
27 |
String authToken = null; |
|
28 |
HttpServletRequest request = HttpContext.getRequest(); |
|
29 |
|
|
30 |
//权限校验的头部 |
|
31 |
String tokenHeader = getTokenHeaderName(); |
|
32 |
authToken = request.getHeader(tokenHeader); |
|
33 |
|
|
34 |
//header中没有的话去cookie拿值,以header为准 |
|
35 |
if (ToolUtil.isEmpty(authToken)) { |
|
36 |
Cookie[] cookies = request.getCookies(); |
|
37 |
if (cookies != null) { |
|
38 |
for (Cookie cookie : cookies) { |
|
39 |
if (tokenHeader.equals(cookie.getName())) { |
|
40 |
authToken = cookie.getValue(); |
|
41 |
} |
|
42 |
} |
|
43 |
} |
|
44 |
} |
|
45 |
|
|
46 |
return authToken; |
|
47 |
} |
|
48 |
} |