com.gccloud.starter.common.utils.TokenUtils Maven / Gradle / Ivy
package com.gccloud.starter.common.utils;
import com.gccloud.starter.common.constant.GlobalConst;
import com.gccloud.starter.common.config.bean.Jwt;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Set;
/**
* @author liuchengbiao
* @date 2020-07-16 16:51
*/
public class TokenUtils {
public TokenUtils() {
throw new IllegalStateException("不允许创建");
}
public static String getToken(HttpServletRequest request, Jwt jwt) {
Set allowTakeMethod = jwt.getAllowTakeMethod();
if (allowTakeMethod.contains(GlobalConst.Jwt.AllowTakeMethod.PARAMETER)) {
String token = request.getParameter(jwt.getTokenKey());
if (StringUtils.isNotBlank(token)) {
return token;
}
}
if (allowTakeMethod.contains(GlobalConst.Jwt.AllowTakeMethod.HEADER)) {
String token = request.getHeader(jwt.getTokenKey());
if (StringUtils.isNotBlank(token)) {
return token;
}
}
if (allowTakeMethod.contains(GlobalConst.Jwt.AllowTakeMethod.COOKIES)) {
String token = CookieUtils.getValue(request, jwt.getTokenKey());
if (StringUtils.isNotBlank(token)) {
return token;
}
}
return null;
}
}