Merge pull request 'fix(Feign优化): 修复Feign调用时请求头丢失userKey的问题' (#569) from otto/ruoyi-gitlink:master into master

This commit is contained in:
otto 2024-05-06 12:50:38 +08:00
commit 301b494eec
3 changed files with 33 additions and 28 deletions

View File

@ -8,13 +8,11 @@ import com.ruoyi.system.api.domain.SysUserDeptRole;
import com.ruoyi.system.api.factory.RemoteUserFallbackFactory;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;
@Component
@FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.SYSTEM_SERVICE, fallbackFactory = RemoteUserFallbackFactory.class)
public interface RemoteUserService {
/**

View File

@ -24,39 +24,44 @@ public class FeignRequestInterceptor implements RequestInterceptor
public void apply(RequestTemplate requestTemplate)
{
HttpServletRequest httpServletRequest = ServletUtils.getRequest();
Map<String, String> headers = null;
if (StringUtils.isNotNull(httpServletRequest))
{
Map<String, String> headers = ServletUtils.getHeaders(httpServletRequest);
// 传递用户信息请求头防止丢失
setHeader(requestTemplate
, SecurityConstants.DETAILS_USER_ID
, headers.get(SecurityConstants.DETAILS_USER_ID)
, String.valueOf(SecurityUtils.getUserId()));
setHeader(requestTemplate
, SecurityConstants.USER_KEY
, headers.get(SecurityConstants.USER_KEY)
, SecurityUtils.getUserKey());
setHeader(requestTemplate
, SecurityConstants.DETAILS_USERNAME
, headers.get(SecurityConstants.DETAILS_USERNAME)
, SecurityUtils.getUsername());
setHeader(requestTemplate
, SecurityConstants.AUTHORIZATION_HEADER
, headers.get(SecurityConstants.AUTHORIZATION_HEADER)
, null);
setHeader(requestTemplate
, SecurityConstants.DEPT_ID
, headers.get(SecurityConstants.DEPT_ID)
, String.valueOf(SecurityUtils.getDeptId()));
headers = ServletUtils.getHeaders(httpServletRequest);
// 配置客户端IP
requestTemplate.header("X-Forwarded-For", IpUtils.getIpAddr());
}
// 传递用户信息请求头防止丢失
setHeader(requestTemplate
, headers
, SecurityConstants.DETAILS_USER_ID
, String.valueOf(SecurityUtils.getUserId()));
setHeader(requestTemplate
, headers
, SecurityConstants.USER_KEY
, SecurityUtils.getUserKey());
setHeader(requestTemplate
, headers
, SecurityConstants.DETAILS_USERNAME
, SecurityUtils.getUsername());
setHeader(requestTemplate
, headers
, SecurityConstants.AUTHORIZATION_HEADER
, null);
setHeader(requestTemplate
, headers
, SecurityConstants.DEPT_ID
, String.valueOf(SecurityUtils.getDeptId()));
}
private void setHeader(RequestTemplate requestTemplate, String key, String headerValue, String cacheValue) {
if (StringUtils.isNotEmpty(headerValue)) {
requestTemplate.header(key, headerValue);
private void setHeader(RequestTemplate requestTemplate, Map<String, String> headers, String key, String cacheValue) {
if (headers != null) {
String headerValue = headers.get(key);
if (StringUtils.isNotEmpty(headerValue)) {
requestTemplate.header(key, headerValue);
} else if (StringUtils.isNotEmpty(cacheValue)) {
requestTemplate.header(key, cacheValue);
}
} else if (StringUtils.isNotEmpty(cacheValue)) {
requestTemplate.header(key, cacheValue);
}

View File

@ -156,6 +156,8 @@ public class ZoneMemberServiceImpl implements IZoneMemberService {
if (sysUserDeptRoleList != null) {
hasRoleInZone = sysUserDeptRoleList.stream().anyMatch(x -> zoneDetail.getDeptId().equals(x.getDeptId()));
}
String userKey = SecurityUtils.getUserKey();
System.out.println(userKey);
// 当用户在专区下没有其他角色时才需要赋予会员身份
if (!hasRoleInZone) {
boolean addUserIdentify = Boolean.TRUE.equals(FeignUtils.getReturnData(