This commit is contained in:
wangkang
2017-07-29 10:18:34 +08:00
parent e21971373e
commit 21bc2b2a47
6 changed files with 266 additions and 89 deletions
@@ -7,7 +7,6 @@
package com.egridcloud.udf.core;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@@ -59,17 +58,6 @@ public class BaseApplication {
return new RestTemplate();
}
/**
* 描述 : restTemplate
*
* @return restTemplate
*/
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
/**
* 描述 : 分布式ID生成器
*
@@ -49,6 +49,11 @@ public class Constant {
*/
public static final String PURVIEW_DISABLED = "disabled";
/**
* 描述 : http头
*/
public static final String HTTP = "http://";
/**
* 描述 : 私有化构造函数
*
@@ -0,0 +1,148 @@
/**
* Rms.java
* Created at 2017-04-24
* Created by Administrator
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.rms;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.egridcloud.udf.core.RestResponse;
import com.egridcloud.udf.core.exception.SystemRuntimeException;
import com.egridcloud.udf.rms.mate.AuthMate;
import com.egridcloud.udf.rms.mate.PathMate;
/**
* 描述 : 远程服务
*
* @author Administrator
*
*/
@Component
public class Rms {
/**
* 描述 : 日志
*/
private static final Logger LOGGER = LoggerFactory.getLogger(Rms.class);
/**
* 描述 : 应用名称
*/
@Value("${spring.application.name}")
private String springApplicationName;
/**
* 描述 : restTemplate
*/
@Autowired
private RestTemplate restTemplate;
/**
* 描述 : 配置
*/
@Autowired
private RmsProperties rmsProperties;
/**
* 描述 : 远程服务调用
*
* @param serviceCode 服务代码
* @param input 输入参数
* @param uriParam uri参数
* @param uriVariables rest参数
* @param <I> 输入类型
* @param <O> 输出类型
* @return 服务结果
*/
public <I, O> RestResponse<O> call(String serviceCode, I input, String uriParam,
Object... uriVariables) {
//构建请求路径
String path = getRmsUrl(serviceCode);
//获得请求方法
String method = getRmsMethod(serviceCode);
//拼装路径参数
if (StringUtils.isNotBlank(uriParam)) {
path += uriParam;
}
//构建请求头
HttpHeaders httpHeaders = buildSystemTagHeaders(serviceCode);
//构建请求消息体
HttpEntity<I> requestEntity = new HttpEntity<>(input, httpHeaders);
//请求并且返回
LOGGER.info("rms url : {} , method : {} ", path, method);
return restTemplate.exchange(path, HttpMethod.resolve(method), requestEntity,
new ParameterizedTypeReference<RestResponse<O>>() {
}, uriVariables).getBody();
}
/**
* 描述 : 构建请求头
*
* @param serviceCode 服务代码
* @return 请求头
*/
private HttpHeaders buildSystemTagHeaders(String serviceCode) {
String secret = rmsProperties.getAuthorization().get(springApplicationName).getSecret();
HttpHeaders headers = new HttpHeaders();
headers.add(Constant.HEADER_RMS_APPLICATION_NAME_CODE, springApplicationName);
headers.add(Constant.HEADER_RMS_SIGN_CODE, Constant.sign(springApplicationName, secret));
headers.add(Constant.HEADER_SERVICE_CODE_CODE, serviceCode);
return headers;
}
/**
* 描述 : 获得请求方法
*
* @param serviceCode 服务代码
* @return 请求方法
*/
private String getRmsMethod(String serviceCode) {
return rmsProperties.getService().get(serviceCode).getMethod();
}
/**
* 描述 : 构造url;
*
* @param serviceCode 服务代码
* @return url
*/
private String getRmsUrl(String serviceCode) {
//获得认证元数据
AuthMate authMate = rmsProperties.getAuthorization().get(springApplicationName);
//获取路径元数据
PathMate pathMate = rmsProperties.getService().get(springApplicationName);
//判断路径访问权限
if (authMate.getPurview() != null) {
String[] purviewArray = authMate.getPurview().split(",");
List<String> purviewList = Arrays.asList(purviewArray);
if (purviewList.contains(Constant.PURVIEW_DISABLED)) {
throw new SystemRuntimeException(springApplicationName + " is disabled");
}
if (!purviewList.contains(Constant.PURVIEW_ALL) && !purviewList.contains(serviceCode)) {
throw new SystemRuntimeException(serviceCode + " no access");
}
} else {
throw new SystemRuntimeException(springApplicationName + " purview is null");
}
StringBuilder url = new StringBuilder(Constant.HTTP);
url.append(rmsProperties.getService().get(pathMate.getApplicationName()));
url.append(pathMate.getUri());
return url.toString();
}
}
@@ -45,7 +45,7 @@ public class RmsAuthHandlerInterceptor implements HandlerInterceptor {
* 描述 : 配置
*/
@Autowired
private RmsConfig rmsConfig;
private RmsProperties rmsProperties;
/**
* 描述 : 环境变量
@@ -73,9 +73,9 @@ public class RmsAuthHandlerInterceptor implements HandlerInterceptor {
//判断环境(开发环境无需校验)
if (!DEV_PROFILES.equals(env.getProperty("spring.profiles.active"))) {
//判断systemTag是否有效
if (this.rmsConfig.getAuthorization().containsKey(rmsApplicationName)) {
if (this.rmsProperties.getAuthorization().containsKey(rmsApplicationName)) {
//获得secret
String secret = this.rmsConfig.getAuthorization().get(rmsApplicationName).getSecret();
String secret = this.rmsProperties.getAuthorization().get(rmsApplicationName).getSecret();
//计算sign
String sign = Constant.sign(rmsApplicationName, secret);
//比较sign
@@ -83,9 +83,9 @@ public class RmsAuthHandlerInterceptor implements HandlerInterceptor {
throw new AuthException("sign Validation failed");
}
//比较接口编号的有效性
if (rmsConfig.getService().containsKey(rmsServiceCode)) {
if (rmsProperties.getService().containsKey(rmsServiceCode)) {
//获得服务元数据
PathMate pathMate = rmsConfig.getService().get(rmsServiceCode);
PathMate pathMate = rmsProperties.getService().get(rmsServiceCode);
//比较url和method
if (!pathMate.getUri().equals(url) || !pathMate.getMethod().equals(method)) {
throw new AuthException("url and method verification error");
@@ -94,7 +94,7 @@ public class RmsAuthHandlerInterceptor implements HandlerInterceptor {
throw new AuthException("service code not exist");
}
//比较是否有调用接口的权限
AuthMate authMate = rmsConfig.getAuthorization().get(rmsApplicationName);
AuthMate authMate = rmsProperties.getAuthorization().get(rmsApplicationName);
if (authMate.getPurview().indexOf(Constant.PURVIEW_ALL) == -1) {
if (authMate.getPurview().indexOf(Constant.PURVIEW_DISABLED) != -1 //NOSONAR
|| authMate.getPurview().indexOf(rmsServiceCode) == -1) {
@@ -6,13 +6,11 @@
*/
package com.egridcloud.udf.rms;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import com.egridcloud.udf.rms.mate.AuthMate;
import com.egridcloud.udf.rms.mate.PathMate;
import org.springframework.web.client.RestTemplate;
/**
* 描述 : Config
@@ -21,76 +19,18 @@ import com.egridcloud.udf.rms.mate.PathMate;
*
*/
@Component
@ConfigurationProperties(prefix = "com.egridcloud.rms.config")
@Configuration
public class RmsConfig {
/**
* 描述 : 应用清单(应用名称 : 应用地址)
*/
private Map<String, String> application;
/**
* 描述 : 服务路径(服务编号 : 服务元数据)
*/
private Map<String, PathMate> service;
/**
* 描述 : 认证(应用名称 : 权限元数据)
*/
private Map<String, AuthMate> authorization;
/**
* 描述 : 获取application
* 描述 : restTemplate
*
* @return the application
* @return restTemplate
*/
public Map<String, String> getApplication() {
return application;
}
/**
* 描述 : 设置application
*
* @param application the application to set
*/
public void setApplication(Map<String, String> application) {
this.application = application;
}
/**
* 描述 : 获取service
*
* @return the service
*/
public Map<String, PathMate> getService() {
return service;
}
/**
* 描述 : 设置service
*
* @param service the service to set
*/
public void setService(Map<String, PathMate> service) {
this.service = service;
}
/**
* 描述 : 获取authorization
*
* @return the authorization
*/
public Map<String, AuthMate> getAuthorization() {
return authorization;
}
/**
* 描述 : 设置authorization
*
* @param authorization the authorization to set
*/
public void setAuthorization(Map<String, AuthMate> authorization) {
this.authorization = authorization;
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@@ -0,0 +1,96 @@
/**
* Config.java
* Created at 2016-11-19
* Created by Administrator
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.rms;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import com.egridcloud.udf.rms.mate.AuthMate;
import com.egridcloud.udf.rms.mate.PathMate;
/**
* 描述 : Config
*
* @author Administrator
*
*/
@Component
@ConfigurationProperties(prefix = "com.egridcloud.rms.config")
public class RmsProperties {
/**
* 描述 : 应用清单(应用名称 : 应用地址)
*/
private Map<String, String> application;
/**
* 描述 : 服务路径(服务编号 : 服务元数据)
*/
private Map<String, PathMate> service;
/**
* 描述 : 认证(应用名称 : 权限元数据)
*/
private Map<String, AuthMate> authorization;
/**
* 描述 : 获取application
*
* @return the application
*/
public Map<String, String> getApplication() {
return application;
}
/**
* 描述 : 设置application
*
* @param application the application to set
*/
public void setApplication(Map<String, String> application) {
this.application = application;
}
/**
* 描述 : 获取service
*
* @return the service
*/
public Map<String, PathMate> getService() {
return service;
}
/**
* 描述 : 设置service
*
* @param service the service to set
*/
public void setService(Map<String, PathMate> service) {
this.service = service;
}
/**
* 描述 : 获取authorization
*
* @return the authorization
*/
public Map<String, AuthMate> getAuthorization() {
return authorization;
}
/**
* 描述 : 设置authorization
*
* @param authorization the authorization to set
*/
public void setAuthorization(Map<String, AuthMate> authorization) {
this.authorization = authorization;
}
}