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();
|
}
|
}
|