mirror of
https://gitee.com/wangkang/udf.git
synced 2026-10-26 18:13:40 +00:00
优化获得token的逻辑
This commit is contained in:
@@ -2,6 +2,7 @@ package org.itkk.udf.api.common;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.itkk.udf.api.common.dto.UserDto;
|
||||
import org.itkk.udf.starter.core.exception.AuthException;
|
||||
|
||||
@@ -9,7 +10,6 @@ import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 工具类
|
||||
@@ -38,27 +38,26 @@ public class CommonUtil {
|
||||
* @return String
|
||||
*/
|
||||
public static String getToken(HttpServletRequest request) {
|
||||
Object tokenObj = request.getAttribute(CommonConstant.PARAMETER_NAME_TOKEN);
|
||||
if (tokenObj == null) {
|
||||
throw new AuthException("token信息不存在");
|
||||
}
|
||||
return (String) tokenObj;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从cookie中获得token
|
||||
*
|
||||
* @param request request
|
||||
* @return String
|
||||
*/
|
||||
public static String getTokenByCookie(HttpServletRequest request) {
|
||||
if (ArrayUtils.isNotEmpty(request.getCookies())) {
|
||||
Optional<Cookie> cookie = Arrays.stream(request.getCookies()).filter(a -> a.getName().equals(CommonConstant.PARAMETER_NAME_TOKEN)).findFirst();
|
||||
if (cookie.isPresent()) {
|
||||
return cookie.get().getValue();
|
||||
//从url参数获得
|
||||
String token = request.getParameter(CommonConstant.PARAMETER_NAME_TOKEN);
|
||||
if (StringUtils.isBlank(token)) {
|
||||
//从header获得
|
||||
token = request.getHeader(CommonConstant.PARAMETER_NAME_TOKEN);
|
||||
//从作用域中获得
|
||||
if (StringUtils.isBlank(token)) {
|
||||
token = request.getAttribute(CommonConstant.PARAMETER_NAME_TOKEN) == null ? null : request.getAttribute(CommonConstant.PARAMETER_NAME_TOKEN).toString();
|
||||
//从cookie中获得
|
||||
if (StringUtils.isBlank(token)) {
|
||||
token = ArrayUtils.isEmpty(request.getCookies()) ? null : Arrays.stream(request.getCookies()).filter(a -> a.getName().equals(CommonConstant.PARAMETER_NAME_TOKEN)).map(Cookie::getValue).findFirst().orElse(null);
|
||||
//从其他地方获得
|
||||
if (StringUtils.isBlank(token)) {
|
||||
//从其他地方获得
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
//返回
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,4 +80,9 @@ public class CommonUtil {
|
||||
private CommonUtil() {
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String[] a = null;
|
||||
Arrays.stream(a);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -28,11 +28,11 @@ public class CommonHandlerInterceptor implements HandlerInterceptor {
|
||||
//判空
|
||||
if (modelAndView != null) {
|
||||
//获得用户信息
|
||||
UserDto userDto = iUserService.infoByToken(CommonUtil.getTokenByCookie(request));
|
||||
UserDto userDto = iUserService.infoByToken(CommonUtil.getToken(request));
|
||||
//判断是否登陆
|
||||
if (userDto != null) {
|
||||
//放入token
|
||||
modelAndView.addObject(CommonConstant.PARAMETER_NAME_TOKEN, CommonUtil.getTokenByCookie(request));
|
||||
modelAndView.addObject(CommonConstant.PARAMETER_NAME_TOKEN, CommonUtil.getToken(request));
|
||||
//放入用户信息
|
||||
modelAndView.addObject(CommonConstant.PARAMETER_NAME_TOKEN_INFO, userDto);
|
||||
}
|
||||
|
||||
+2
-2
@@ -4,10 +4,10 @@ package org.itkk.udf.api.rbac.auth;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.itkk.udf.api.common.CommonConstant;
|
||||
import org.itkk.udf.api.common.CommonProperties;
|
||||
import org.itkk.udf.api.common.CommonUtil;
|
||||
import org.itkk.udf.api.common.dto.UserDto;
|
||||
import org.itkk.udf.api.common.service.IUserService;
|
||||
import org.itkk.udf.starter.cache.db.service.DbCacheService;
|
||||
import org.itkk.udf.starter.core.CoreUtil;
|
||||
import org.itkk.udf.starter.core.exception.AuthException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -43,7 +43,7 @@ public class WebHandlerInterceptor implements HandlerInterceptor {
|
||||
//排除options
|
||||
if (!HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
|
||||
//获得token
|
||||
final String token = CoreUtil.getParameter(request, CommonConstant.PARAMETER_NAME_TOKEN);
|
||||
final String token = CommonUtil.getToken(request);
|
||||
//获得tokenInfo
|
||||
UserDto userDto = iUserService.infoByToken(token);
|
||||
//判空
|
||||
|
||||
@@ -433,33 +433,6 @@ public class CoreUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得参数(先从问号后面取,如果为空,再从header里取,如果为空,从作用域中取,如果都为空,则返回null)
|
||||
*
|
||||
* @param request request
|
||||
* @param parameterName parameterName
|
||||
* @return String
|
||||
*/
|
||||
public static String getParameter(HttpServletRequest request, String parameterName) {
|
||||
//获得token
|
||||
String token = request.getParameter(parameterName);
|
||||
if (StringUtils.isBlank(token)) {
|
||||
token = request.getHeader(parameterName);
|
||||
if (StringUtils.isBlank(token)) {
|
||||
Object tokenObj = request.getAttribute(parameterName);
|
||||
if (tokenObj != null) {
|
||||
token = tokenObj.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
//返回
|
||||
return token;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws UnknownHostException {
|
||||
System.out.println(getLocalHostLANAddress().getHostAddress());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得局域网IP地址
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user