Explorar o código

Merge branch 'master' of http://git.homecommunity.cn/supervip/MicroCommunity

java110 %!s(int64=5) %!d(string=hai) anos
pai
achega
f00f54dc33

+ 10 - 0
java110-service/src/main/java/com/java110/service/configuration/CustomMVCConfiguration.java

@@ -36,4 +36,14 @@ public class CustomMVCConfiguration extends WebMvcConfigurerAdapter {
             ContentNegotiationConfigurer configurer) {
         configurer.favorPathExtension(false);
     }
+
+    /**
+     * +对于header中的中文字进行解码
+     *
+     * @return 转换结果
+     */
+    @Bean
+    public StringDecoderForHeaderConverter stringHeaderConverter() {
+        return new StringDecoderForHeaderConverter(Charset.forName("UTF-8"));
+    }
 }

+ 121 - 0
java110-service/src/main/java/com/java110/service/configuration/StringDecoderForHeaderConverter.java

@@ -0,0 +1,121 @@
+package com.java110.service.configuration;
+
+
+import brave.internal.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.convert.TypeDescriptor;
+import org.springframework.core.convert.converter.GenericConverter;
+import org.springframework.util.ObjectUtils;
+import org.springframework.web.bind.annotation.RequestHeader;
+
+import java.net.URLDecoder;
+import java.nio.charset.Charset;
+import java.util.Collections;
+import java.util.Set;
+
+public class StringDecoderForHeaderConverter implements GenericConverter {
+    private Logger logger = LoggerFactory.getLogger(StringDecoderForHeaderConverter.class);
+
+    private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
+    private static final String NO_NAME = "NO_NAME";
+
+    private Charset charset;
+
+    public StringDecoderForHeaderConverter(@Nullable Charset charset) {
+        this.charset = charset;
+        if (this.charset == null) {
+            this.charset = DEFAULT_CHARSET;
+        }
+    }
+
+    /**
+     * +返回编码值
+     * @return charset
+     */
+    public Charset getCharset() {
+        return charset;
+    }
+
+    /**
+     * +设置编码值
+     * @param charset 编码值
+     */
+    public void setCharset(Charset charset) {
+        this.charset = charset;
+
+        if (this.charset == null) {
+            this.charset = DEFAULT_CHARSET;
+        }
+    }
+
+    @Override
+    public Set<ConvertiblePair> getConvertibleTypes() {
+        return Collections.singleton(new ConvertiblePair(String.class, String.class));
+    }
+
+    @Override
+    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
+        if (ObjectUtils.isEmpty(source)) {
+            return source;
+        }
+
+        String name = needDecoder(source, targetType);
+        if (name != null) {
+            return convert(source.toString(), name);
+        }
+
+        return source;
+    }
+
+    /**
+     * +是否需要解码
+     * @param source 待处理的值
+     * @param targetType 类型
+     * @return 非null:需要解码;null:无需解码
+     */
+    private String needDecoder(Object source, TypeDescriptor targetType) {
+        RequestHeader requestHeader = targetType.getAnnotation(RequestHeader.class);
+        Class<?> type = targetType.getType();
+        if (requestHeader != null && type == String.class) {
+            if (source.toString().indexOf("%") >= 0) {
+                String name = requestHeader.name();
+                if (name == null || name.equals("")) {
+                    name = requestHeader.value();
+                }
+                if (name == null || name.equals("")) {
+                    name = NO_NAME;
+                }
+
+                return name;
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * +结果解码
+     * @param source 待解码的结果
+     * @param name 参数名称
+     * @return 解码后的结果
+     */
+    private String convert(final String source, final String name) {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Begin convert[" + source + "] for RequestHeader[" + name + "].");
+        }
+        String _result = null;
+        try {
+            _result = URLDecoder.decode(source, this.charset.name());
+            if (logger.isDebugEnabled()) {
+                logger.debug("Success convert[" + source + ", " + _result + "] for RequestHeader[" + name + "].");
+            }
+
+            return _result;
+        } catch(Exception e) {
+            logger.warn("Fail convert[" + source + "] for RequestHeader[" + name + "]!", e);
+        }
+
+        return source;
+    }
+}

+ 2 - 1
service-front/src/main/java/com/java110/front/smo/api/IApiSMO.java

@@ -3,9 +3,10 @@ package com.java110.front.smo.api;
 import org.springframework.http.ResponseEntity;
 
 import javax.servlet.http.HttpServletRequest;
+import java.io.UnsupportedEncodingException;
 import java.util.Map;
 
 public interface IApiSMO {
 
-    public ResponseEntity<String> doApi(String body, Map<String, String> headers, HttpServletRequest request);
+    public ResponseEntity<String> doApi(String body, Map<String, String> headers, HttpServletRequest request) throws UnsupportedEncodingException;
 }

+ 11 - 4
service-front/src/main/java/com/java110/front/smo/api/impl/ApiSMOImpl.java

@@ -12,12 +12,18 @@ import com.java110.utils.util.StringUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.*;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.HttpStatusCodeException;
 import org.springframework.web.client.RestTemplate;
 
 import javax.servlet.http.HttpServletRequest;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
 import java.util.Map;
 
 @Service("apiSMOImpl")
@@ -47,7 +53,7 @@ public class ApiSMOImpl extends BaseComponentSMO implements IApiSMO {
         }
 
         JSONObject storeInfo = JSONObject.parseObject(responseEntity.getBody().toString());
-        if(!storeInfo.containsKey("storeId")){
+        if (!storeInfo.containsKey("storeId")) {
             return new ComponentValidateResult("", "", "", pd.getUserId(), pd.getUserName());
         }
 
@@ -68,15 +74,16 @@ public class ApiSMOImpl extends BaseComponentSMO implements IApiSMO {
     }
 
     @Override
-    public ResponseEntity<String> doApi(String body, Map<String, String> headers, HttpServletRequest request) {
+    public ResponseEntity<String> doApi(String body, Map<String, String> headers, HttpServletRequest request) throws UnsupportedEncodingException {
         HttpHeaders header = new HttpHeaders();
         IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
 
         ComponentValidateResult result = this.validateStoreStaffCommunityRelationship(pd, restTemplate);
         if (!StringUtil.isEmpty(result.getUserId())) {
             header.add("user-id", result.getUserId());
-            header.add("user-name", result.getUserName());
+            header.add("user-name", URLEncoder.encode(result.getUserName(), "UTF-8"));
         }
+
         header.add("store-id", result.getStoreId());
         logger.debug("api请求头" + headers + ";请求内容:" + body);
         HttpMethod method = null;