From 21bc2b2a4760f69ec79bd5b3da156cde567d43ce Mon Sep 17 00:00:00 2001 From: wangkang Date: Mon, 24 Apr 2017 18:24:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../egridcloud/udf/core/BaseApplication.java | 12 -- .../java/com/egridcloud/udf/rms/Constant.java | 5 + .../main/java/com/egridcloud/udf/rms/Rms.java | 148 ++++++++++++++++++ .../udf/rms/RmsAuthHandlerInterceptor.java | 12 +- .../com/egridcloud/udf/rms/RmsConfig.java | 82 ++-------- .../com/egridcloud/udf/rms/RmsProperties.java | 96 ++++++++++++ 6 files changed, 266 insertions(+), 89 deletions(-) create mode 100644 udf-starter-rms/src/main/java/com/egridcloud/udf/rms/Rms.java create mode 100644 udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsProperties.java diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/BaseApplication.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/BaseApplication.java index 37359de..6c3cb71 100644 --- a/udf-starter-core/src/main/java/com/egridcloud/udf/core/BaseApplication.java +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/BaseApplication.java @@ -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生成器 * diff --git a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/Constant.java b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/Constant.java index 3923df6..76653a3 100644 --- a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/Constant.java +++ b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/Constant.java @@ -49,6 +49,11 @@ public class Constant { */ public static final String PURVIEW_DISABLED = "disabled"; + /** + * 描述 : http头 + */ + public static final String HTTP = "http://"; + /** * 描述 : 私有化构造函数 * diff --git a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/Rms.java b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/Rms.java new file mode 100644 index 0000000..1493879 --- /dev/null +++ b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/Rms.java @@ -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 输入类型 + * @param 输出类型 + * @return 服务结果 + */ + public RestResponse 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 requestEntity = new HttpEntity<>(input, httpHeaders); + //请求并且返回 + LOGGER.info("rms url : {} , method : {} ", path, method); + return restTemplate.exchange(path, HttpMethod.resolve(method), requestEntity, + new ParameterizedTypeReference>() { + }, 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 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(); + } + +} diff --git a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsAuthHandlerInterceptor.java b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsAuthHandlerInterceptor.java index 8db1820..57104bd 100644 --- a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsAuthHandlerInterceptor.java +++ b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsAuthHandlerInterceptor.java @@ -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) { diff --git a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsConfig.java b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsConfig.java index b110a9a..224a0cc 100644 --- a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsConfig.java +++ b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsConfig.java @@ -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 application; - - /** - * 描述 : 服务路径(服务编号 : 服务元数据) - */ - private Map service; - - /** - * 描述 : 认证(应用名称 : 权限元数据) - */ - private Map authorization; - - /** - * 描述 : 获取application + * 描述 : restTemplate * - * @return the application + * @return restTemplate */ - public Map getApplication() { - return application; - } - - /** - * 描述 : 设置application - * - * @param application the application to set - */ - public void setApplication(Map application) { - this.application = application; - } - - /** - * 描述 : 获取service - * - * @return the service - */ - public Map getService() { - return service; - } - - /** - * 描述 : 设置service - * - * @param service the service to set - */ - public void setService(Map service) { - this.service = service; - } - - /** - * 描述 : 获取authorization - * - * @return the authorization - */ - public Map getAuthorization() { - return authorization; - } - - /** - * 描述 : 设置authorization - * - * @param authorization the authorization to set - */ - public void setAuthorization(Map authorization) { - this.authorization = authorization; + @Bean + @LoadBalanced + RestTemplate restTemplate() { + return new RestTemplate(); } } diff --git a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsProperties.java b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsProperties.java new file mode 100644 index 0000000..a1b0710 --- /dev/null +++ b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsProperties.java @@ -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 application; + + /** + * 描述 : 服务路径(服务编号 : 服务元数据) + */ + private Map service; + + /** + * 描述 : 认证(应用名称 : 权限元数据) + */ + private Map authorization; + + /** + * 描述 : 获取application + * + * @return the application + */ + public Map getApplication() { + return application; + } + + /** + * 描述 : 设置application + * + * @param application the application to set + */ + public void setApplication(Map application) { + this.application = application; + } + + /** + * 描述 : 获取service + * + * @return the service + */ + public Map getService() { + return service; + } + + /** + * 描述 : 设置service + * + * @param service the service to set + */ + public void setService(Map service) { + this.service = service; + } + + /** + * 描述 : 获取authorization + * + * @return the authorization + */ + public Map getAuthorization() { + return authorization; + } + + /** + * 描述 : 设置authorization + * + * @param authorization the authorization to set + */ + public void setAuthorization(Map authorization) { + this.authorization = authorization; + } + +}