liuxiaolong
2019-05-06 c15226e1b58f255dbebf1bdca8d4e53b9277249c
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
package com.basic.analy.utils;
 
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
 
/**
 * @author lp
 */
public class RequestUtil {
    public static final String ACCESS_TOKEN = "access_token";
    public static final String AUTH = "Authorization";
    public static final String BEARER = "Bearer";
    public static String getTokenByParam() {
        String token = null;
        HttpServletRequest request=getRequest();
        if (request.getParameter(ACCESS_TOKEN) != null) {
            token = request.getParameter(ACCESS_TOKEN).toString();
        }
        // 头部的Authorization值以Bearer开头
        String auth = request.getHeader(AUTH);
        if (auth != null) {
            if (auth.startsWith(BEARER) || auth.startsWith(BEARER.toLowerCase())) {
                token = auth .substring(7,auth.length());
            }
        }
        return token;
    }
    public static HttpServletRequest getRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
}