From b5dada173a7cb0aa412b0028d045028515f1a57c Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Fri, 2 Aug 2019 10:42:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EAPP=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E5=99=A8=EF=BC=8C=E5=8A=A0=E5=AF=86=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cm/common/base/AbstractController.java | 12 +- .../com/cm/common/base/AbstractService.java | 2 +- .../com/cm/common/config/FilterConfig.java | 31 +++ .../cm/common/constants/ISystemConstant.java | 32 ++- .../java/com/cm/common/utils/AesUtil.java | 188 +++++++++++------- .../java/com/cm/common/utils/HashMapUtil.java | 22 ++ pom.xml | 8 +- 7 files changed, 204 insertions(+), 91 deletions(-) create mode 100644 cloud-common/src/main/java/com/cm/common/config/FilterConfig.java diff --git a/cloud-common/src/main/java/com/cm/common/base/AbstractController.java b/cloud-common/src/main/java/com/cm/common/base/AbstractController.java index 52161f9..734a9ab 100644 --- a/cloud-common/src/main/java/com/cm/common/base/AbstractController.java +++ b/cloud-common/src/main/java/com/cm/common/base/AbstractController.java @@ -1,5 +1,6 @@ package com.cm.common.base; +import com.cm.common.utils.HashMapUtil; import com.cm.common.utils.RequestUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,16 +54,7 @@ public abstract class AbstractController { * @return */ protected Map requestParams(HttpServletRequest request) { - Enumeration requestNames = request.getParameterNames(); - Map params = getParams(); - while (requestNames.hasMoreElements()) { - String name = requestNames.nextElement(); - String value = request.getParameter(name); - if (value.isEmpty()) { - continue; - } - params.put(name, value); - } + Map params = HashMapUtil.requestParamsToMap(request); params.put("requestUri", request.getRequestURI()); params.put("requestHost", request.getRemoteHost()); params.put("requestPort", request.getLocalPort()); diff --git a/cloud-common/src/main/java/com/cm/common/base/AbstractService.java b/cloud-common/src/main/java/com/cm/common/base/AbstractService.java index e5059b4..666bfd1 100644 --- a/cloud-common/src/main/java/com/cm/common/base/AbstractService.java +++ b/cloud-common/src/main/java/com/cm/common/base/AbstractService.java @@ -63,7 +63,7 @@ public abstract class AbstractService { * @param subCount */ protected void setZTreeInfo(ZTreeDTO zTreeDTO, Integer subCount) { - zTreeDTO.setUrl("javascript:;"); + zTreeDTO.setUrl("javascript:void(0);"); if (null != subCount && 0 < subCount) { zTreeDTO.setIsParent(true); } else { diff --git a/cloud-common/src/main/java/com/cm/common/config/FilterConfig.java b/cloud-common/src/main/java/com/cm/common/config/FilterConfig.java new file mode 100644 index 0000000..1ea38f2 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/config/FilterConfig.java @@ -0,0 +1,31 @@ +package com.cm.common.config; + +import com.cm.common.filter.AppFilter; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: FilterConfig + * @Description: 过滤器控制 + * @Author: WangGeng + * @Date: 2019-08-01 14:08 + * @Version: 1.0 + **/ +@Configuration +public class FilterConfig { + + @Bean + public FilterRegistrationBean appFilterRegister() { + FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); + filterRegistrationBean.setFilter(new AppFilter()); + filterRegistrationBean.addUrlPatterns("/app/*"); + filterRegistrationBean.setName("appFilter"); + filterRegistrationBean.setOrder(1); + return filterRegistrationBean; + } + +} diff --git a/cloud-common/src/main/java/com/cm/common/constants/ISystemConstant.java b/cloud-common/src/main/java/com/cm/common/constants/ISystemConstant.java index 94060a0..7a2aeba 100644 --- a/cloud-common/src/main/java/com/cm/common/constants/ISystemConstant.java +++ b/cloud-common/src/main/java/com/cm/common/constants/ISystemConstant.java @@ -8,45 +8,61 @@ package com.cm.common.constants; * @Version: 1.0 **/ public interface ISystemConstant { - /** * 系统接口标签前缀 */ String API_TAGS_SYSTEM_PREFIX = "系统接口-"; - /** * 资源接口标签前缀 */ String API_TAGS_RESOURCE_PREFIX = "资源接口-"; - + /** + * APP接口前缀 + */ + String API_TAGS_APP_PREFIX = "APP接口-"; /** * api前缀 */ String API_PREFIX = "/api"; - /** * 路由前缀 */ String ROUTE_PREFIX = "/route"; - /** * 资源前缀 */ String RESOURCE_PREFIX = "/resource"; - + /** + * APP前缀 + */ + String APP_PREFIX = "/app"; /** * true */ String IS_TRUE = "true"; - /** * false */ String IS_FALSE = "false"; - /** * UTF-8编码 */ String CHARSET_UTF8 = "UTF-8"; + /** + * token + */ + String TOKEN = "token"; + /** + * app的token加密秘钥 + */ + String APP_TOKEN_AES_KEY = "CMXX_TOKEN_INFOS"; + /** + * app的token标识 + */ + String APP_TOKEN_SIGN = "tokenSign"; + /** + * app的token标识前缀 + */ + String APP_TOKEN_VERIFY = "CM_Token_"; } diff --git a/cloud-common/src/main/java/com/cm/common/utils/AesUtil.java b/cloud-common/src/main/java/com/cm/common/utils/AesUtil.java index a99f991..33aa721 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/AesUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/AesUtil.java @@ -1,71 +1,117 @@ -package com.cm.common.utils; - -import org.apache.commons.codec.binary.Base64; - -import javax.crypto.Cipher; -import javax.crypto.KeyGenerator; -import javax.crypto.SecretKey; -import javax.crypto.spec.SecretKeySpec; -import java.security.SecureRandom; - -public class AesUtil { - /** - * AES加密 - * - * @param encodeRule - * @param content - * @return - * @throws Exception - */ - public static String aesEncoder(String encodeRule, String content) throws Exception { - // 1.构造秘钥生成器 - KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); - // 2.根据编码规则构建秘钥生成器 - SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); - random.setSeed(encodeRule.getBytes()); - keyGenerator.init(128, random); - // 3.产生堆成秘钥 - SecretKey secretKey = keyGenerator.generateKey(); - // 4.对称秘钥原始数组 - byte[] secretKeyByte = secretKey.getEncoded(); - // 5.生成AES秘钥 - SecretKey key = new SecretKeySpec(secretKeyByte, "AES"); - // 6.初始化密码器 - Cipher cipher = Cipher.getInstance("AES"); - cipher.init(Cipher.ENCRYPT_MODE, key); - // 8.获取加密内容字节数组 - byte[] contentByte = content.getBytes("UTF-8"); - // 9.加密 - return new String(Base64.encodeBase64(cipher.doFinal(contentByte))); - } - - /** - * AES解密 - * - * @param encodeRule - * @param content - * @return - * @throws Exception - */ - public static String aesDecoder(String encodeRule, String content) throws Exception { - // 1.构建秘钥生成器 - KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); - // 2.根据编码规则构建秘钥生成器 - SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); - random.setSeed(encodeRule.getBytes()); - keyGenerator.init(128, random); - // 3.生成对称解密秘钥 - SecretKey secretKey = keyGenerator.generateKey(); - // 4.对称秘钥原始数组 - byte[] secretKeyByte = secretKey.getEncoded(); - // 5.生成AES秘钥 - SecretKey key = new SecretKeySpec(secretKeyByte, "AES"); - // 6.初始化密码器 - Cipher cipher = Cipher.getInstance("AES"); - cipher.init(Cipher.DECRYPT_MODE, key); - // 7.要解密内容的字节数组 - byte[] contentByte = Base64.decodeBase64(content); - // 8.解密 - return new String(cipher.doFinal(contentByte), "UTF-8"); - } -} +package com.cm.common.utils; + +import org.apache.commons.codec.binary.Base64; + +import javax.crypto.*; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; +import java.io.UnsupportedEncodingException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +public class AesUtil { + + private static final String IV_STRING = "16-Bytes--String"; + + /** + * AES加密 + * + * @param encodeRule + * @param content + * @return + * @throws Exception + */ + public static String aesEncoder(String encodeRule, String content) throws Exception { + // 1.构造秘钥生成器 + KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); + // 2.根据编码规则构建秘钥生成器 + SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); + random.setSeed(encodeRule.getBytes()); + keyGenerator.init(128, random); + // 3.产生堆成秘钥 + SecretKey secretKey = keyGenerator.generateKey(); + // 4.对称秘钥原始数组 + byte[] secretKeyByte = secretKey.getEncoded(); + // 5.生成AES秘钥 + SecretKey key = new SecretKeySpec(secretKeyByte, "AES"); + // 6.初始化密码器 + Cipher cipher = Cipher.getInstance("AES"); + cipher.init(Cipher.ENCRYPT_MODE, key); + // 8.获取加密内容字节数组 + byte[] contentByte = content.getBytes("UTF-8"); + // 9.加密 + return new String(Base64.encodeBase64(cipher.doFinal(contentByte))); + } + + /** + * AES解密 + * + * @param encodeRule + * @param content + * @return + * @throws Exception + */ + public static String aesDecoder(String encodeRule, String content) throws Exception { + // 1.构建秘钥生成器 + KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); + // 2.根据编码规则构建秘钥生成器 + SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); + random.setSeed(encodeRule.getBytes()); + keyGenerator.init(128, random); + // 3.生成对称解密秘钥 + SecretKey secretKey = keyGenerator.generateKey(); + // 4.对称秘钥原始数组 + byte[] secretKeyByte = secretKey.getEncoded(); + // 5.生成AES秘钥 + SecretKey key = new SecretKeySpec(secretKeyByte, "AES"); + // 6.初始化密码器 + Cipher cipher = Cipher.getInstance("AES"); + cipher.init(Cipher.DECRYPT_MODE, key); + // 7.要解密内容的字节数组 + byte[] contentByte = Base64.decodeBase64(content); + // 8.解密 + return new String(cipher.doFinal(contentByte), "UTF-8"); + } + + /** + * 通用aes加密,兼容IOS + * + * @param key + * @param content + * @return + * @throws Exception + */ + public static String aesCommonEncoder(String key, String content) throws Exception { + byte[] byteContent = content.getBytes("UTF-8"); + byte[] enCodeFormat = key.getBytes(); + SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES"); + byte[] initParam = IV_STRING.getBytes(); + IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam); + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); + byte[] encryptedBytes = cipher.doFinal(byteContent); + return new String(Base64.encodeBase64(encryptedBytes), "UTF-8"); + } + + /** + * aes通用解密,兼容IOS + * + * @param key + * @param content + * @return + * @throws Exception + */ + public static String aesCommonDecoder(String key, String content) throws Exception { + byte[] encryptedBytes = Base64.decodeBase64(content); + byte[] enCodeFormat = key.getBytes(); + SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES"); + byte[] initParam = IV_STRING.getBytes(); + IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam); + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); + byte[] result = cipher.doFinal(encryptedBytes); + return new String(result, "UTF-8"); + } +} diff --git a/cloud-common/src/main/java/com/cm/common/utils/HashMapUtil.java b/cloud-common/src/main/java/com/cm/common/utils/HashMapUtil.java index 16f6361..94bc76e 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/HashMapUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/HashMapUtil.java @@ -1,9 +1,11 @@ package com.cm.common.utils; +import javax.servlet.http.HttpServletRequest; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; +import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -16,6 +18,26 @@ import java.util.Map.Entry; */ public class HashMapUtil { + /** + * 请求参数转Map + * + * @param request + * @return + */ + public static Map requestParamsToMap(HttpServletRequest request) { + Enumeration requestNames = request.getParameterNames(); + Map params = new HashMap<>(); + while (requestNames.hasMoreElements()) { + String name = requestNames.nextElement(); + String value = request.getParameter(name); + if (value.isEmpty()) { + continue; + } + params.put(name, value); + } + return params; + } + /** * 类转Map * diff --git a/pom.xml b/pom.xml index 8671216..8ccbf54 100644 --- a/pom.xml +++ b/pom.xml @@ -40,11 +40,12 @@ 1.3.1 1.12 1.18 + 1.9.3 1.3.18 1.4.0 3.17 2.0.1.Final - 6.0.8.Final + 6.0.17.Final 1.6.1 2.3.0 2.10.4 @@ -276,6 +277,11 @@ commons-compress ${common-compress} + + commons-beanutils + commons-beanutils + ${common-beanutils.version} +