tanghc 6 years ago
parent 46a546f1da
commit 5b32524a83
  1. 4
      README.md
  2. 4
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/configuration/BaseZuulController.java
  3. 5
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/filter/BaseZuulFilter.java
  4. 3
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ApiMetaChangeListener.java
  5. 4
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultApiMetaContext.java
  6. 4
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorEnum.java
  7. 3
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorImpl.java
  8. 2
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/session/ApiHttpSession.java
  9. 5
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/alipay/StreamUtil.java
  10. 12
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/alipay/StringUtils.java
  11. 18
      sop-server-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceContext.java
  12. 5
      sop-server-common/src/main/java/com/gitee/sop/servercommon/configuration/BaseServerConfiguration.java
  13. 3
      sop-server-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java
  14. 20
      sop-server-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingRequestCondition.java
  15. 3
      sop-server-common/src/main/java/com/gitee/sop/servercommon/message/ServiceErrorImpl.java
  16. 5
      sop-test/src/main/java/com/gitee/sop/alipay/StreamUtil.java
  17. 20
      sop-test/src/main/java/com/gitee/sop/alipay/StringUtils.java

@ -9,11 +9,11 @@ SOP封装了开放平台大部分功能包括:签名验证、统一异常处
## 项目特点
- 接入方式简单,与老项目不冲突,老项目注册到注册中心,然后方法上加上注解即可。
- 接入方式简单,与老项目不冲突,老项目注册到注册中心,然后方法上加上注解即可。
- 架构松耦合,业务代码实现在各自微服务上,SOP不参与业务实现,这也是Spring Cloud微服务体系带来的好处。
- 扩展简单,开放平台对应的功能各自独立,可以自定义实现自己的需求,如:更改参数,更改签名规则等。
## 谁使用这个项目
## 谁可以使用这个项目
- 有现成的项目,想改造成开放平台供他人调用
- 有现成的项目,想暴露其中几个接口并通过开放平台供他人调用

@ -23,7 +23,9 @@ public class BaseZuulController implements ErrorController {
public static final String ERROR_PATH = "/error";
// 错误最终会到这里来
/**
* 错误最终会到这里来
*/
@RequestMapping(ERROR_PATH)
@ResponseBody
public Object error(HttpServletRequest request, HttpServletResponse response) {

@ -76,10 +76,15 @@ public abstract class BaseZuulFilter extends ZuulFilter {
* Any filterType made be created or added and doRun by calling FilterProcessor.runFilters(type)
*/
public enum FilterType {
/** zuul过滤器pre类型 */
PRE("pre"),
/** zuul过滤器route类型 */
ROUTE("route"),
/** zuul过滤器post类型 */
POST("post"),
/** zuul过滤器error类型 */
ERROR("error"),
/** zuul过滤器static类型 */
STATIC("static"),
;

@ -5,6 +5,9 @@ import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* @author tanghc
*/
public class ApiMetaChangeListener implements MessageListener {
private ApiMetaManager apiMetaManager;

@ -16,10 +16,10 @@ import java.util.Map;
@Slf4j
public class DefaultApiMetaContext implements ApiMetaContext {
// key:nameVersion
/** key:nameVersion */
private Map<String, Route> nameVersionServiceIdMap = new HashMap<>(128);
// key: serviceId , value: md5
/** key: serviceId , value: md5 */
private Map<String, String> serviceIdMd5Map = new HashMap<>(16);
@Override

@ -56,8 +56,8 @@ public enum ErrorEnum {
;
private ErrorMeta errorMeta;
ErrorEnum(String code, String sub_code) {
this.errorMeta = new ErrorMeta("open.error_", code, sub_code);
ErrorEnum(String code, String subCode) {
this.errorMeta = new ErrorMeta("open.error_", code, subCode);
}
public ErrorMeta getErrorMeta() {

@ -2,6 +2,9 @@ package com.gitee.sop.gatewaycommon.message;
import lombok.Data;
/**
* @author thc
*/
@Data
public class ErrorImpl implements Error {
private String code;

@ -207,7 +207,7 @@ public class ApiHttpSession implements HttpSession, Serializable {
* @return a representation of this session's serialized state
*/
public Serializable serializeState() {
HashMap<String, Serializable> state = new HashMap<String, Serializable>();
HashMap<String, Serializable> state = new HashMap<String, Serializable>(16);
for (Iterator<Map.Entry<String, Object>> it = this.attributes.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Object> entry = it.next();
String name = entry.getKey();

@ -101,30 +101,35 @@ public class StreamUtil {
this.lock = lock;
}
@Override
public void write(int datum) throws IOException {
synchronized (lock) {
out.write(datum);
}
}
@Override
public void write(byte[] data) throws IOException {
synchronized (lock) {
out.write(data);
}
}
@Override
public void write(byte[] data, int offset, int length) throws IOException {
synchronized (lock) {
out.write(data, offset, length);
}
}
@Override
public void flush() throws IOException {
synchronized (lock) {
out.flush();
}
}
@Override
public void close() throws IOException {
synchronized (lock) {
out.close();

@ -44,12 +44,14 @@ public abstract class StringUtils {
}
char[] chars = obj.toString().toCharArray();
int length = chars.length;
if(length < 1)
if(length < 1) {
return false;
}
int i = 0;
if(length > 1 && chars[0] == '-')
if(length > 1 && chars[0] == '-') {
i = 1;
}
for (; i < length; i++) {
if (!Character.isDigit(chars[i])) {
@ -91,8 +93,9 @@ public abstract class StringUtils {
* 过滤不可见字符
*/
public static String stripNonValidXMLCharacters(String input) {
if (input == null || ("".equals(input)))
if (input == null || ("".equals(input))) {
return "";
}
StringBuilder out = new StringBuilder();
char current;
for (int i = 0; i < input.length(); i++) {
@ -100,8 +103,9 @@ public abstract class StringUtils {
if ((current == 0x9) || (current == 0xA) || (current == 0xD)
|| ((current >= 0x20) && (current <= 0xD7FF))
|| ((current >= 0xE000) && (current <= 0xFFFD))
|| ((current >= 0x10000) && (current <= 0x10FFFF)))
|| ((current >= 0x10000) && (current <= 0x10FFFF))) {
out.append(current);
}
}
return out.toString();
}

@ -5,13 +5,16 @@ import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author tanghc
*/
public class ServiceContext extends ConcurrentHashMap<String, Object> {
public static final String REQUEST_KEY = "request";
public static final String RESPONSE_KEY = "response";
protected static Class<? extends ServiceContext> contextClass = ServiceContext.class;
protected static final ThreadLocal<? extends ServiceContext> threadLocal = new ThreadLocal<ServiceContext>() {
protected static final ThreadLocal<? extends ServiceContext> THREAD_LOCAL = new ThreadLocal<ServiceContext>() {
@Override
protected ServiceContext initialValue() {
try {
@ -47,7 +50,7 @@ public class ServiceContext extends ConcurrentHashMap<String, Object> {
* @return the current ServiceContext
*/
public static ServiceContext getCurrentContext() {
return threadLocal.get();
return THREAD_LOCAL.get();
}
/**
@ -91,8 +94,11 @@ public class ServiceContext extends ConcurrentHashMap<String, Object> {
* @param value
*/
public void set(String key, Object value) {
if (value != null) put(key, value);
else remove(key);
if (value != null) {
put(key, value);
} else {
remove(key);
}
}
/**
@ -129,10 +135,10 @@ public class ServiceContext extends ConcurrentHashMap<String, Object> {
/**
* unsets the threadLocal context. Done at the end of the request.
* unsets the THREAD_LOCAL context. Done at the end of the request.
*/
public void unset() {
threadLocal.remove();
THREAD_LOCAL.remove();
}

@ -49,7 +49,10 @@ public class BaseServerConfiguration extends WebMvcConfigurationSupport {
super.addInterceptors(registry);
}
// 自定义Mapping,详见@ApiMapping
/**
* 自定义Mapping详见@ApiMapping
* @return
*/
@Override
protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
ApiMetaManager apiMetaManager = new RedisApiMetaManager(redisTemplate);

@ -13,6 +13,9 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
import java.lang.reflect.Method;
/**
* @author tanghc
*/
public class ApiMappingHandlerMapping extends RequestMappingHandlerMapping implements PriorityOrdered {
private static StringValueResolver stringValueResolver = new ApiMappingStringValueResolver();

@ -8,6 +8,9 @@ import org.springframework.web.servlet.mvc.condition.RequestCondition;
import javax.servlet.http.HttpServletRequest;
/**
* @author tanghc
*/
@Getter
@Slf4j
public class ApiMappingRequestCondition implements RequestCondition<ApiMappingRequestCondition> {
@ -26,8 +29,12 @@ public class ApiMappingRequestCondition implements RequestCondition<ApiMappingRe
return this;
}
// 如果版本号跟当前对象中的版本号匹配,则表是命中的对应的方法
// 否则返回null,表示不匹配
/**
* 如果版本号跟当前对象中的版本号匹配则表是命中的对应的方法
* 否则返回null表示不匹配
* @param request
* @return
*/
@Override
public ApiMappingRequestCondition getMatchingCondition(HttpServletRequest request) {
String version = this.getVersion(request);
@ -43,8 +50,13 @@ public class ApiMappingRequestCondition implements RequestCondition<ApiMappingRe
}
// 对两个RequestCondition对象进行比较,这里主要是如果存在两个注册的一样的Mapping,那么就会对
// 这两个Mapping进行排序,以判断哪个Mapping更适合处理当前request请求
/**
* 对两个RequestCondition对象进行比较这里主要是如果存在两个注册的一样的Mapping
* 那么就会对这两个Mapping进行排序以判断哪个Mapping更适合处理当前request请求
* @param other
* @param request
* @return
*/
@Override
public int compareTo(ApiMappingRequestCondition other, HttpServletRequest request) {
return null != apiMappingInfo && null == other.apiMappingInfo ? 1

@ -2,6 +2,9 @@ package com.gitee.sop.servercommon.message;
import lombok.Data;
/**
* @author tanghc
*/
@Data
public class ServiceErrorImpl implements ServiceError {
private String sub_code;

@ -101,30 +101,35 @@ public class StreamUtil {
this.lock = lock;
}
@Override
public void write(int datum) throws IOException {
synchronized (lock) {
out.write(datum);
}
}
@Override
public void write(byte[] data) throws IOException {
synchronized (lock) {
out.write(data);
}
}
@Override
public void write(byte[] data, int offset, int length) throws IOException {
synchronized (lock) {
out.write(data, offset, length);
}
}
@Override
public void flush() throws IOException {
synchronized (lock) {
out.flush();
}
}
@Override
public void close() throws IOException {
synchronized (lock) {
out.close();

@ -44,12 +44,14 @@ public abstract class StringUtils {
}
char[] chars = obj.toString().toCharArray();
int length = chars.length;
if(length < 1)
return false;
if(length < 1) {
return false;
}
int i = 0;
if(length > 1 && chars[0] == '-')
i = 1;
if(length > 1 && chars[0] == '-') {
i = 1;
}
for (; i < length; i++) {
if (!Character.isDigit(chars[i])) {
@ -91,8 +93,9 @@ public abstract class StringUtils {
* 过滤不可见字符
*/
public static String stripNonValidXMLCharacters(String input) {
if (input == null || ("".equals(input)))
return "";
if (input == null || ("".equals(input))) {
return "";
}
StringBuilder out = new StringBuilder();
char current;
for (int i = 0; i < input.length(); i++) {
@ -100,8 +103,9 @@ public abstract class StringUtils {
if ((current == 0x9) || (current == 0xA) || (current == 0xD)
|| ((current >= 0x20) && (current <= 0xD7FF))
|| ((current >= 0xE000) && (current <= 0xFFFD))
|| ((current >= 0x10000) && (current <= 0x10FFFF)))
out.append(current);
|| ((current >= 0x10000) && (current <= 0x10FFFF))) {
out.append(current);
}
}
return out.toString();
}

Loading…
Cancel
Save