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 new file mode 100644 index 0000000..3923df6 --- /dev/null +++ b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/Constant.java @@ -0,0 +1,75 @@ +/** + * Constant.java + * Created at 2017-04-24 + * Created by Administrator + * Copyright (C) 2016 egridcloud.com, All rights reserved. + */ +package com.egridcloud.udf.rms; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.apache.commons.codec.digest.DigestUtils; + +/** + * 描述 : 常量 + * + * @author Administrator + * + */ +public class Constant { + + /** + * 描述 : 头信息(rms应用名称) + */ + public static final String HEADER_RMS_APPLICATION_NAME_CODE = "rmsApplicationName"; + + /** + * 描述 : 头信息(rms认证秘钥) + */ + public static final String HEADER_RMS_SIGN_CODE = "rmsSign"; + + /** + * 描述 : 头信息(rms接口编号) + */ + public static final String HEADER_SERVICE_CODE_CODE = "rmsServiceCode"; + + /** + * 描述 : 日期格式 + */ + 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"; + + /** + * 描述 : 私有化构造函数 + * + */ + private Constant() { + + } + + /** + * 描述 : 计算sign + * + * @param rmsApplicationName 系统标志 + * @param secret 系统私钥 + * @return sign + */ + public static String sign(String rmsApplicationName, String secret) { + final String split = "_"; + StringBuilder sb = new StringBuilder(); + sb.append(rmsApplicationName).append(split).append(secret).append(split) + .append(new SimpleDateFormat(DATA_FORMAT).format(new Date())); + return DigestUtils.md5Hex(sb.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 new file mode 100644 index 0000000..8db1820 --- /dev/null +++ b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsAuthHandlerInterceptor.java @@ -0,0 +1,125 @@ +/** + * SystemTagAuthHandlerInterceptor.java + * Created at 2016-11-16 + * Created by wangkang + * Copyright (C) 2016 egridcloud.com, All rights reserved. + */ +package com.egridcloud.udf.rms; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.HandlerMapping; +import org.springframework.web.servlet.ModelAndView; + +import com.egridcloud.udf.core.exception.AuthException; +import com.egridcloud.udf.core.exception.PermissionException; +import com.egridcloud.udf.rms.mate.AuthMate; +import com.egridcloud.udf.rms.mate.PathMate; + +/** + * 描述 : SystemTagAuthHandlerInterceptor + * + * @author wangkang + * + */ +public class RmsAuthHandlerInterceptor implements HandlerInterceptor { + + /** + * 描述 : 日志 + */ + private static final Logger LOGGER = LoggerFactory.getLogger(RmsAuthHandlerInterceptor.class); + + /** + * 描述 : 环境标识 + */ + private static final String DEV_PROFILES = "dev"; + + /** + * 描述 : 配置 + */ + @Autowired + private RmsConfig rmsConfig; + + /** + * 描述 : 环境变量 + */ + @Autowired + private Environment env; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, //NOSONAR + Object handler) { + //获取认证信息 + String rmsApplicationName = request.getHeader(Constant.HEADER_RMS_APPLICATION_NAME_CODE); + String rmsSign = request.getHeader(Constant.HEADER_RMS_SIGN_CODE); + 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); + //判断是否缺少认证信息 + if (StringUtils.isBlank(rmsApplicationName) || StringUtils.isBlank(rmsSign) + || StringUtils.isBlank(rmsServiceCode)) { + throw new AuthException( + "missing required authentication parameters (rmsApplicationName , rmsSign)"); + } + //判断环境(开发环境无需校验) + if (!DEV_PROFILES.equals(env.getProperty("spring.profiles.active"))) { + //判断systemTag是否有效 + if (this.rmsConfig.getAuthorization().containsKey(rmsApplicationName)) { + //获得secret + String secret = this.rmsConfig.getAuthorization().get(rmsApplicationName).getSecret(); + //计算sign + String sign = Constant.sign(rmsApplicationName, secret); + //比较sign + if (!rmsSign.equals(sign)) { + throw new AuthException("sign Validation failed"); + } + //比较接口编号的有效性 + if (rmsConfig.getService().containsKey(rmsServiceCode)) { + //获得服务元数据 + PathMate pathMate = rmsConfig.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 = rmsConfig.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); + } + } + } else { + throw new AuthException("unrecognized systemTag:" + rmsApplicationName); + } + } + //返回 + return true; + } + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, + ModelAndView modelAndView) throws Exception { + LOGGER.debug("SystemTagAuthHandlerInterceptor.postHandle"); + } + + @Override + 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/RmsConfig.java b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsConfig.java new file mode 100644 index 0000000..b110a9a --- /dev/null +++ b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsConfig.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 RmsConfig { + + /** + * 描述 : 应用清单(应用名称 : 应用地址) + */ + 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; + } + +} 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 new file mode 100644 index 0000000..447b711 --- /dev/null +++ b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/mate/AuthMate.java @@ -0,0 +1,63 @@ +/** + * AuthMate.java + * Created at 2016-12-22 + * Created by Administrator + * Copyright (C) 2016 egridcloud.com, All rights reserved. + */ +package com.egridcloud.udf.rms.mate; + +/** + * 描述 : Auth元数据 + * + * @author Administrator + * + */ +public class AuthMate { + + /** + * 描述 : 私钥 + */ + private String secret; + + /** + * 描述 : 权限 + */ + private String purview; + + /** + * 描述 : 获取secret + * + * @return the secret + */ + public String getSecret() { + return secret; + } + + /** + * 描述 : 设置secret + * + * @param secret the secret to set + */ + public void setSecret(String secret) { + this.secret = secret; + } + + /** + * 描述 : 获取purview + * + * @return the purview + */ + public String getPurview() { + return purview; + } + + /** + * 描述 : 设置purview + * + * @param purview the purview to set + */ + public void setPurview(String purview) { + this.purview = purview; + } + +} diff --git a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/mate/PathMate.java b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/mate/PathMate.java new file mode 100644 index 0000000..516eab0 --- /dev/null +++ b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/mate/PathMate.java @@ -0,0 +1,86 @@ +/** + * PathMate.java + * Created at 2016-12-22 + * Created by Administrator + * Copyright (C) 2016 egridcloud.com, All rights reserved. + */ +package com.egridcloud.udf.rms.mate; + +/** + * 描述 : path元数据 + * + * @author Administrator + * + */ +public class PathMate { + + /** + * 描述 : 应用名称 + */ + private String applicationName; + + /** + * 描述 : 地址 + */ + private String uri; + + /** + * 描述 : 服务方法 + */ + private String method; + + /** + * 描述 : 获取applicationName + * + * @return the applicationName + */ + public String getApplicationName() { + return applicationName; + } + + /** + * 描述 : 设置applicationName + * + * @param applicationName the applicationName to set + */ + public void setApplicationName(String applicationName) { + this.applicationName = applicationName; + } + + /** + * 描述 : 获取uri + * + * @return the uri + */ + public String getUri() { + return uri; + } + + /** + * 描述 : 设置uri + * + * @param uri the uri to set + */ + public void setUri(String uri) { + this.uri = uri; + } + + /** + * 描述 : 获取method + * + * @return the method + */ + public String getMethod() { + return method; + } + + /** + * 描述 : 设置method + * + * @param method the method to set + */ + public void setMethod(String method) { + this.method = method; + } + +}