获取请求token方法移至权限工具类

pull/31/head
RuoYi 4 years ago
parent 3a9d45a7dd
commit 91a2f7b16b

@ -31,4 +31,9 @@ public class CacheConstants
* *
*/ */
public static final String DETAILS_USERNAME = "username"; public static final String DETAILS_USERNAME = "username";
/**
*
*/
public static final String AUTHORIZATION_HEADER = "authorization";
} }

@ -1,5 +1,6 @@
package com.ruoyi.common.core.utils; package com.ruoyi.common.core.utils;
import javax.servlet.http.HttpServletRequest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.text.Convert; import com.ruoyi.common.core.text.Convert;
@ -27,6 +28,27 @@ public class SecurityUtils
return Convert.toLong(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USER_ID)); return Convert.toLong(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USER_ID));
} }
/**
* token
*/
public static String getToken()
{
return getToken(ServletUtils.getRequest());
}
/**
* requesttoken
*/
public static String getToken(HttpServletRequest request)
{
String token = ServletUtils.getRequest().getHeader(CacheConstants.HEADER);
if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX))
{
token = token.replace(CacheConstants.TOKEN_PREFIX, "");
}
return token;
}
/** /**
* *
* *

@ -35,6 +35,11 @@ public class FeignRequestInterceptor implements RequestInterceptor
{ {
requestTemplate.header(CacheConstants.DETAILS_USERNAME, userName); requestTemplate.header(CacheConstants.DETAILS_USERNAME, userName);
} }
String authentication = headers.get(CacheConstants.AUTHORIZATION_HEADER);
if (StringUtils.isNotEmpty(authentication))
{
requestTemplate.header(CacheConstants.AUTHORIZATION_HEADER, authentication);
}
} }
} }
} }

@ -9,6 +9,7 @@ import org.springframework.stereotype.Component;
import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.utils.IdUtils; import com.ruoyi.common.core.utils.IdUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.ServletUtils; import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.ip.IpUtils; import com.ruoyi.common.core.utils.ip.IpUtils;
@ -71,7 +72,7 @@ public class TokenService
public LoginUser getLoginUser(HttpServletRequest request) public LoginUser getLoginUser(HttpServletRequest request)
{ {
// 获取请求携带的令牌 // 获取请求携带的令牌
String token = getToken(request); String token = SecurityUtils.getToken(request);
if (StringUtils.isNotEmpty(token)) if (StringUtils.isNotEmpty(token))
{ {
String userKey = getTokenKey(token); String userKey = getTokenKey(token);
@ -119,17 +120,4 @@ public class TokenService
{ {
return ACCESS_TOKEN + token; return ACCESS_TOKEN + token;
} }
/**
* token
*/
private String getToken(HttpServletRequest request)
{
String token = request.getHeader(CacheConstants.HEADER);
if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX))
{
token = token.replace(CacheConstants.TOKEN_PREFIX, "");
}
return token;
}
} }
Loading…
Cancel
Save