From 20f57fba7e5e17bbaf19a677c4aa4e6ec0a94d85 Mon Sep 17 00:00:00 2001 From: wangkang Date: Thu, 4 May 2017 13:52:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9rms=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/egridcloud/udf/rms/Constant.java | 10 ---- .../main/java/com/egridcloud/udf/rms/Rms.java | 16 ++----- .../udf/rms/RmsAuthHandlerInterceptor.java | 48 ++++++++++--------- .../com/egridcloud/udf/rms/mate/AuthMate.java | 46 ++++++++++++++++++ 4 files changed, 77 insertions(+), 43 deletions(-) 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 76653a3..b81cac7 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 @@ -39,16 +39,6 @@ public class Constant { */ public static final String DATA_FORMAT = "yyyy-MM-dd'T'HH"; - /** - * 描述 : 所有权限 - */ - public static final String PURVIEW_ALL = "all"; - - /** - * 描述 : 禁用权限 - */ - public static final String PURVIEW_DISABLED = "disabled"; - /** * 描述 : 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 index 1493879..29baed4 100644 --- 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 @@ -6,9 +6,6 @@ */ 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; @@ -69,8 +66,7 @@ public class Rms { * @param 输出类型 * @return 服务结果 */ - public RestResponse call(String serviceCode, I input, String uriParam, - Object... uriVariables) { + public RestResponse call(String serviceCode, I input, String uriParam, Object... uriVariables) { //构建请求路径 String path = getRmsUrl(serviceCode); //获得请求方法 @@ -85,8 +81,8 @@ public class Rms { HttpEntity requestEntity = new HttpEntity<>(input, httpHeaders); //请求并且返回 LOGGER.info("rms url : {} , method : {} ", path, method); - return restTemplate.exchange(path, HttpMethod.resolve(method), requestEntity, - new ParameterizedTypeReference>() { + return restTemplate + .exchange(path, HttpMethod.resolve(method), requestEntity, new ParameterizedTypeReference>() { }, uriVariables).getBody(); } @@ -128,12 +124,10 @@ public class Rms { 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)) { + if (authMate.getDisabled()) { throw new SystemRuntimeException(springApplicationName + " is disabled"); } - if (!purviewList.contains(Constant.PURVIEW_ALL) && !purviewList.contains(serviceCode)) { + if (!authMate.getAll() && authMate.getPurview().indexOf(serviceCode) == -1) { throw new SystemRuntimeException(serviceCode + " no access"); } } else { 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 57104bd..9f4e0e4 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 @@ -62,13 +62,12 @@ public class RmsAuthHandlerInterceptor implements HandlerInterceptor { String rmsServiceCode = request.getHeader(Constant.HEADER_SERVICE_CODE_CODE); String url = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString(); String method = request.getMethod(); - LOGGER.info("rmsApplicationName:{},rmsSign:{},rmsServiceCode:{},url:{},method:{}", - rmsApplicationName, rmsSign, rmsServiceCode, url, method); + LOGGER.info("rmsApplicationName:{},rmsSign:{},rmsServiceCode:{},url:{},method:{}", rmsApplicationName, rmsSign, + rmsServiceCode, url, method); //判断是否缺少认证信息 if (StringUtils.isBlank(rmsApplicationName) || StringUtils.isBlank(rmsSign) || StringUtils.isBlank(rmsServiceCode)) { - throw new AuthException( - "missing required authentication parameters (rmsApplicationName , rmsSign)"); + throw new AuthException("missing required authentication parameters (rmsApplicationName , rmsSign)"); } //判断环境(开发环境无需校验) if (!DEV_PROFILES.equals(env.getProperty("spring.profiles.active"))) { @@ -82,24 +81,29 @@ public class RmsAuthHandlerInterceptor implements HandlerInterceptor { if (!rmsSign.equals(sign)) { throw new AuthException("sign Validation failed"); } - //比较接口编号的有效性 - if (rmsProperties.getService().containsKey(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"); - } - } else { - throw new AuthException("service code not exist"); - } //比较是否有调用接口的权限 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) { - throw new PermissionException( - "no access to this url : " + url + ", method : " + method); + //判断是否有调用所有服务的权限 + if (!authMate.getAll()) { + //判断是否禁止调用所有服务权限 + if (authMate.getDisabled()) { + throw new PermissionException("all services are disabled"); + } + //判断是否有调用该服务的权限 + if (authMate.getPurview().indexOf(rmsServiceCode) != -1) { + //比较接口编号和服务设定的有效性 + if (rmsProperties.getService().containsKey(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"); + } + } else { + throw new AuthException("service code not exist"); + } + } else { + throw new PermissionException("no access to this url : " + url + ", method : " + method); } } } else { @@ -117,8 +121,8 @@ public class RmsAuthHandlerInterceptor implements HandlerInterceptor { } @Override - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, - Object handler, Exception ex) throws Exception { + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) + throws Exception { LOGGER.debug("SystemTagAuthHandlerInterceptor.afterCompletion"); } diff --git a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/mate/AuthMate.java b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/mate/AuthMate.java index 447b711..12366ef 100644 --- a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/mate/AuthMate.java +++ b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/mate/AuthMate.java @@ -24,6 +24,52 @@ public class AuthMate { */ private String purview; + /** + * 描述 : 所有服务的调用权限(优先判定) + */ + private Boolean all = false; + + /** + * 描述 : 禁止服务调用 + */ + private Boolean disabled = false; + + /** + * 描述 : 获取all + * + * @return the all + */ + public Boolean getAll() { + return all; + } + + /** + * 描述 : 设置all + * + * @param all the all to set + */ + public void setAll(Boolean all) { + this.all = all; + } + + /** + * 描述 : 获取disabled + * + * @return the disabled + */ + public Boolean getDisabled() { + return disabled; + } + + /** + * 描述 : 设置disabled + * + * @param disabled the disabled to set + */ + public void setDisabled(Boolean disabled) { + this.disabled = disabled; + } + /** * 描述 : 获取secret *