Commit 92f3119d authored by 梁光意's avatar 梁光意

修改RestTemplateUtils通过RequestContextHolder获取token

parent ba944255
package com.syc.slm.common.core.util; package com.syc.slm.common.core.util;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.syc.slm.common.core.constant.SlmConstants; import com.syc.slm.common.core.constant.SlmConstants;
import com.syc.slm.slmbi.exception.SysException; import com.syc.slm.slmbi.exception.SysException;
...@@ -7,6 +9,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -7,6 +9,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Map; import java.util.Map;
...@@ -40,8 +43,19 @@ public final class RestTemplateUtils { ...@@ -40,8 +43,19 @@ public final class RestTemplateUtils {
// } // }
public static <TResult>TResult get(String url, String token, final Type resultType, Map<String, ?> uriVariables) { public static <TResult>TResult get(String url, final Type resultType, Map<String, ?> uriVariables) {
try { try {
// 获取token以及在请求头加上token
HttpServletRequest request = ServletUtils.getHttpServletRequest();
Map<String, String> headerMap = ServletUtils.getHeaderMap(request);
if(CollectionUtils.isEmpty(headerMap)){
throw new SysException("头部不能为空");
}
String token =request.getHeader(SlmConstants.TOKEN_PREFIX);
if(StringUtils.isBlank(token)){
throw new SysException("token不能为空");
}
Map<String, String> headMap = Maps.newHashMap(); Map<String, String> headMap = Maps.newHashMap();
headMap.put(SlmConstants.TOKEN_PREFIX, token); headMap.put(SlmConstants.TOKEN_PREFIX, token);
ResponseEntity<String> response = get(url, headMap, String.class, uriVariables); ResponseEntity<String> response = get(url, headMap, String.class, uriVariables);
......
...@@ -44,7 +44,7 @@ public class SysMetaObjectHandler implements MetaObjectHandler { ...@@ -44,7 +44,7 @@ public class SysMetaObjectHandler implements MetaObjectHandler {
if(CollectionUtils.isEmpty(headerMap)){ if(CollectionUtils.isEmpty(headerMap)){
throw new SysException("头部不能为空"); throw new SysException("头部不能为空");
} }
String token =request.getHeader("X-Token"); String token =request.getHeader(SlmConstants.TOKEN_PREFIX);
if(StringUtils.isBlank(token)){ if(StringUtils.isBlank(token)){
throw new SysException("token不能为空"); throw new SysException("token不能为空");
} }
......
...@@ -83,7 +83,7 @@ public class BaseRestController { ...@@ -83,7 +83,7 @@ public class BaseRestController {
Map<String,String> uriVariables =Maps.newHashMap(); Map<String,String> uriVariables =Maps.newHashMap();
uriVariables.put("appId",map.get("appId")+""); uriVariables.put("appId",map.get("appId")+"");
String getUrl =dcClientHost+"/{appId}/users/user-info"; String getUrl =dcClientHost+"/{appId}/users/user-info";
R<CurrentUser> result = RestTemplateUtils.get(getUrl, token, new TypeToken<R<CurrentUser>>() { R<CurrentUser> result = RestTemplateUtils.get(getUrl, new TypeToken<R<CurrentUser>>() {
}.getType(), uriVariables); }.getType(), uriVariables);
request.getSession().setAttribute(token, SlmConstants.gson.toJson(result.detach())); request.getSession().setAttribute(token, SlmConstants.gson.toJson(result.detach()));
} }
......
...@@ -33,7 +33,7 @@ public class DeptServiceImpl implements DeptService { ...@@ -33,7 +33,7 @@ public class DeptServiceImpl implements DeptService {
uriVariables.put("deptName",deptName); uriVariables.put("deptName",deptName);
uriVariables.put("appId",appId); uriVariables.put("appId",appId);
R<List<DeptVo>> result = RestTemplateUtils.get(uri, token, new TypeToken<R<List<DeptVo>>>() {}.getType(), uriVariables); R<List<DeptVo>> result = RestTemplateUtils.get(uri, new TypeToken<R<List<DeptVo>>>() {}.getType(), uriVariables);
return result.detach(); return result.detach();
} }
} }
...@@ -417,7 +417,7 @@ public class InstrumentPanelTreeServiceImpl extends ServiceImpl<InstrumentPanelT ...@@ -417,7 +417,7 @@ public class InstrumentPanelTreeServiceImpl extends ServiceImpl<InstrumentPanelT
private R<Map<String,String>> setDeptAndRole(CurrentUser user,String uri){ private R<Map<String,String>> setDeptAndRole(CurrentUser user,String uri){
Map<String,String> uriVariables =Maps.newHashMap(); Map<String,String> uriVariables =Maps.newHashMap();
uriVariables.put("appId",user.getAppId()); uriVariables.put("appId",user.getAppId());
return RestTemplateUtils.get(uri, user.getToken(), new TypeToken<R<Map<String,String>>>() {}.getType(), uriVariables); return RestTemplateUtils.get(uri, new TypeToken<R<Map<String,String>>>() {}.getType(), uriVariables);
} }
} }
...@@ -31,7 +31,7 @@ public class PositionServiceImpl implements PositionService { ...@@ -31,7 +31,7 @@ public class PositionServiceImpl implements PositionService {
uriVariables.put("positionName",name); uriVariables.put("positionName",name);
uriVariables.put("appId",appId); uriVariables.put("appId",appId);
R<List<PositionVo>> result = RestTemplateUtils.get(uri, token, new TypeToken<R<List<PositionVo>>>() {}.getType(), uriVariables); R<List<PositionVo>> result = RestTemplateUtils.get(uri, new TypeToken<R<List<PositionVo>>>() {}.getType(), uriVariables);
return result.detach(); return result.detach();
} }
} }
...@@ -29,7 +29,7 @@ public class UserServiceImpl implements UserService { ...@@ -29,7 +29,7 @@ public class UserServiceImpl implements UserService {
Map<String,String> uriVariables =Maps.newHashMap(); Map<String,String> uriVariables =Maps.newHashMap();
uriVariables.put("userName",userName); uriVariables.put("userName",userName);
uriVariables.put("appId",appId); uriVariables.put("appId",appId);
R<List<UserTreeVo>> result = RestTemplateUtils.get(uri, token, new TypeToken<R<List<UserTreeVo>>>() {}.getType(), uriVariables); R<List<UserTreeVo>> result = RestTemplateUtils.get(uri,new TypeToken<R<List<UserTreeVo>>>() {}.getType(), uriVariables);
return result.detach(); return result.detach();
} }
} }
...@@ -52,7 +52,7 @@ public class VariableServiceImpl implements VariableService { ...@@ -52,7 +52,7 @@ public class VariableServiceImpl implements VariableService {
p.put("current",variableDTO.getCurrent()+""); p.put("current",variableDTO.getCurrent()+"");
p.put("size",variableDTO.getSize()+""); p.put("size",variableDTO.getSize()+"");
R<Map<String, Object>> result = RestTemplateUtils.get(uri, token, new TypeToken<R<Map<String, Object>>>() { R<Map<String, Object>> result = RestTemplateUtils.get(uri, new TypeToken<R<Map<String, Object>>>() {
}.getType(), p); }.getType(), p);
Map<String, Object> detach = result.detach(); Map<String, Object> detach = result.detach();
if (CollectionUtils.isNotEmpty(detach)) { if (CollectionUtils.isNotEmpty(detach)) {
...@@ -100,7 +100,7 @@ public class VariableServiceImpl implements VariableService { ...@@ -100,7 +100,7 @@ public class VariableServiceImpl implements VariableService {
p.put("warranty_status", null); p.put("warranty_status", null);
p.put("page_num", queryPageDTO.getCurrent() + ""); p.put("page_num", queryPageDTO.getCurrent() + "");
p.put("page_size", queryPageDTO.getSize() + ""); p.put("page_size", queryPageDTO.getSize() + "");
R<Map<String, Object>> result = RestTemplateUtils.get(uri, token, new TypeToken<R<Map<String, Object>>>() { R<Map<String, Object>> result = RestTemplateUtils.get(uri, new TypeToken<R<Map<String, Object>>>() {
}.getType(), p); }.getType(), p);
Map<String, Object> detach = result.detach(); Map<String, Object> detach = result.detach();
if (CollectionUtils.isNotEmpty(detach)) { if (CollectionUtils.isNotEmpty(detach)) {
...@@ -189,11 +189,9 @@ public class VariableServiceImpl implements VariableService { ...@@ -189,11 +189,9 @@ public class VariableServiceImpl implements VariableService {
} }
private R<Map<String,String>> setDeptAndRole(CurrentUser user,String uri){ private R<Map<String,String>> setDeptAndRole(CurrentUser user,String uri){
Map<String,String> heads = Maps.newHashMap();
heads.put("X-Token",user.getToken());
Map<String,String> uriVariables =Maps.newHashMap(); Map<String,String> uriVariables =Maps.newHashMap();
uriVariables.put("appId",user.getAppId()); uriVariables.put("appId",user.getAppId());
return RestTemplateUtils.get(uri, heads, new TypeToken<R<Map<String,String>>>() {}.getType(), uriVariables); return RestTemplateUtils.get(uri, new TypeToken<R<Map<String,String>>>() {}.getType(), uriVariables);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment