新增APP过滤器,加密工具

This commit is contained in:
wenc000 2019-08-02 10:42:09 +08:00
parent 66f896bc04
commit b5dada173a
7 changed files with 204 additions and 91 deletions

View File

@ -1,5 +1,6 @@
package com.cm.common.base; package com.cm.common.base;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.RequestUtil; import com.cm.common.utils.RequestUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -53,16 +54,7 @@ public abstract class AbstractController {
* @return * @return
*/ */
protected Map<String, Object> requestParams(HttpServletRequest request) { protected Map<String, Object> requestParams(HttpServletRequest request) {
Enumeration<String> requestNames = request.getParameterNames(); Map<String, Object> params = HashMapUtil.requestParamsToMap(request);
Map<String, Object> params = getParams();
while (requestNames.hasMoreElements()) {
String name = requestNames.nextElement();
String value = request.getParameter(name);
if (value.isEmpty()) {
continue;
}
params.put(name, value);
}
params.put("requestUri", request.getRequestURI()); params.put("requestUri", request.getRequestURI());
params.put("requestHost", request.getRemoteHost()); params.put("requestHost", request.getRemoteHost());
params.put("requestPort", request.getLocalPort()); params.put("requestPort", request.getLocalPort());

View File

@ -63,7 +63,7 @@ public abstract class AbstractService {
* @param subCount * @param subCount
*/ */
protected void setZTreeInfo(ZTreeDTO zTreeDTO, Integer subCount) { protected void setZTreeInfo(ZTreeDTO zTreeDTO, Integer subCount) {
zTreeDTO.setUrl("javascript:;"); zTreeDTO.setUrl("javascript:void(0);");
if (null != subCount && 0 < subCount) { if (null != subCount && 0 < subCount) {
zTreeDTO.setIsParent(true); zTreeDTO.setIsParent(true);
} else { } else {

View File

@ -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;
}
}

View File

@ -8,45 +8,61 @@ package com.cm.common.constants;
* @Version: 1.0 * @Version: 1.0
**/ **/
public interface ISystemConstant { public interface ISystemConstant {
/** /**
* 系统接口标签前缀 * 系统接口标签前缀
*/ */
String API_TAGS_SYSTEM_PREFIX = "系统接口-"; String API_TAGS_SYSTEM_PREFIX = "系统接口-";
/** /**
* 资源接口标签前缀 * 资源接口标签前缀
*/ */
String API_TAGS_RESOURCE_PREFIX = "资源接口-"; String API_TAGS_RESOURCE_PREFIX = "资源接口-";
/**
* APP接口前缀
*/
String API_TAGS_APP_PREFIX = "APP接口-";
/** /**
* api前缀 * api前缀
*/ */
String API_PREFIX = "/api"; String API_PREFIX = "/api";
/** /**
* 路由前缀 * 路由前缀
*/ */
String ROUTE_PREFIX = "/route"; String ROUTE_PREFIX = "/route";
/** /**
* 资源前缀 * 资源前缀
*/ */
String RESOURCE_PREFIX = "/resource"; String RESOURCE_PREFIX = "/resource";
/**
* APP前缀
*/
String APP_PREFIX = "/app";
/** /**
* true * true
*/ */
String IS_TRUE = "true"; String IS_TRUE = "true";
/** /**
* false * false
*/ */
String IS_FALSE = "false"; String IS_FALSE = "false";
/** /**
* UTF-8编码 * UTF-8编码
*/ */
String CHARSET_UTF8 = "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_";
} }

View File

@ -1,71 +1,117 @@
package com.cm.common.utils; package com.cm.common.utils;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher; import javax.crypto.*;
import javax.crypto.KeyGenerator; import javax.crypto.spec.IvParameterSpec;
import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException;
import java.security.SecureRandom; import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
public class AesUtil { import java.security.NoSuchAlgorithmException;
/** import java.security.SecureRandom;
* AES加密
* public class AesUtil {
* @param encodeRule
* @param content private static final String IV_STRING = "16-Bytes--String";
* @return
* @throws Exception /**
*/ * AES加密
public static String aesEncoder(String encodeRule, String content) throws Exception { *
// 1.构造秘钥生成器 * @param encodeRule
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); * @param content
// 2.根据编码规则构建秘钥生成器 * @return
SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); * @throws Exception
random.setSeed(encodeRule.getBytes()); */
keyGenerator.init(128, random); public static String aesEncoder(String encodeRule, String content) throws Exception {
// 3.产生堆成秘钥 // 1.构造秘钥生成器
SecretKey secretKey = keyGenerator.generateKey(); KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
// 4.对称秘钥原始数组 // 2.根据编码规则构建秘钥生成器
byte[] secretKeyByte = secretKey.getEncoded(); SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
// 5.生成AES秘钥 random.setSeed(encodeRule.getBytes());
SecretKey key = new SecretKeySpec(secretKeyByte, "AES"); keyGenerator.init(128, random);
// 6.初始化密码器 // 3.产生堆成秘钥
Cipher cipher = Cipher.getInstance("AES"); SecretKey secretKey = keyGenerator.generateKey();
cipher.init(Cipher.ENCRYPT_MODE, key); // 4.对称秘钥原始数组
// 8.获取加密内容字节数组 byte[] secretKeyByte = secretKey.getEncoded();
byte[] contentByte = content.getBytes("UTF-8"); // 5.生成AES秘钥
// 9.加密 SecretKey key = new SecretKeySpec(secretKeyByte, "AES");
return new String(Base64.encodeBase64(cipher.doFinal(contentByte))); // 6.初始化密码器
} Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
/** // 8.获取加密内容字节数组
* AES解密 byte[] contentByte = content.getBytes("UTF-8");
* // 9.加密
* @param encodeRule return new String(Base64.encodeBase64(cipher.doFinal(contentByte)));
* @param content }
* @return
* @throws Exception /**
*/ * AES解密
public static String aesDecoder(String encodeRule, String content) throws Exception { *
// 1.构建秘钥生成器 * @param encodeRule
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); * @param content
// 2.根据编码规则构建秘钥生成器 * @return
SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); * @throws Exception
random.setSeed(encodeRule.getBytes()); */
keyGenerator.init(128, random); public static String aesDecoder(String encodeRule, String content) throws Exception {
// 3.生成对称解密秘钥 // 1.构建秘钥生成器
SecretKey secretKey = keyGenerator.generateKey(); KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
// 4.对称秘钥原始数组 // 2.根据编码规则构建秘钥生成器
byte[] secretKeyByte = secretKey.getEncoded(); SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
// 5.生成AES秘钥 random.setSeed(encodeRule.getBytes());
SecretKey key = new SecretKeySpec(secretKeyByte, "AES"); keyGenerator.init(128, random);
// 6.初始化密码器 // 3.生成对称解密秘钥
Cipher cipher = Cipher.getInstance("AES"); SecretKey secretKey = keyGenerator.generateKey();
cipher.init(Cipher.DECRYPT_MODE, key); // 4.对称秘钥原始数组
// 7.要解密内容的字节数组 byte[] secretKeyByte = secretKey.getEncoded();
byte[] contentByte = Base64.decodeBase64(content); // 5.生成AES秘钥
// 8.解密 SecretKey key = new SecretKeySpec(secretKeyByte, "AES");
return new String(cipher.doFinal(contentByte), "UTF-8"); // 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");
}
}

View File

@ -1,9 +1,11 @@
package com.cm.common.utils; package com.cm.common.utils;
import javax.servlet.http.HttpServletRequest;
import java.beans.BeanInfo; import java.beans.BeanInfo;
import java.beans.Introspector; import java.beans.Introspector;
import java.beans.PropertyDescriptor; import java.beans.PropertyDescriptor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -16,6 +18,26 @@ import java.util.Map.Entry;
*/ */
public class HashMapUtil { public class HashMapUtil {
/**
* 请求参数转Map
*
* @param request
* @return
*/
public static Map<String, Object> requestParamsToMap(HttpServletRequest request) {
Enumeration<String> requestNames = request.getParameterNames();
Map<String, Object> 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 * 类转Map
* *

View File

@ -40,11 +40,12 @@
<common-fileupload.version>1.3.1</common-fileupload.version> <common-fileupload.version>1.3.1</common-fileupload.version>
<common-codec.version>1.12</common-codec.version> <common-codec.version>1.12</common-codec.version>
<common-compress>1.18</common-compress> <common-compress>1.18</common-compress>
<common-beanutils.version>1.9.3</common-beanutils.version>
<saaj.version>1.3.18</saaj.version> <saaj.version>1.3.18</saaj.version>
<shiro.version>1.4.0</shiro.version> <shiro.version>1.4.0</shiro.version>
<poi.version>3.17</poi.version> <poi.version>3.17</poi.version>
<validation.version>2.0.1.Final</validation.version> <validation.version>2.0.1.Final</validation.version>
<hibernate-validation.version>6.0.8.Final</hibernate-validation.version> <hibernate-validation.version>6.0.17.Final</hibernate-validation.version>
<dom4j.version>1.6.1</dom4j.version> <dom4j.version>1.6.1</dom4j.version>
<quartz.version>2.3.0</quartz.version> <quartz.version>2.3.0</quartz.version>
<ehcache.version>2.10.4</ehcache.version> <ehcache.version>2.10.4</ehcache.version>
@ -276,6 +277,11 @@
<artifactId>commons-compress</artifactId> <artifactId>commons-compress</artifactId>
<version>${common-compress}</version> <version>${common-compress}</version>
</dependency> </dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>${common-beanutils.version}</version>
</dependency>
<!-- Apache commons end --> <!-- Apache commons end -->
<!-- POI start --> <!-- POI start -->
<dependency> <dependency>