对比视图

在下面输入commit id(如: 4eedf23 ) 或者分支/标签名(如: master
Diff
23 文件发生了变化
frame-core/pom.xml
@@ -13,6 +13,7 @@
<packaging>jar</packaging>
 
<dependencies>
+ <!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
@@ -29,6 +30,15 @@
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.data</groupId>
+ <artifactId>spring-data-redis</artifactId>
+ </dependency>
+ <!-- spring end -->
 
<!-- 数据库操作相关依赖包 -->
<dependency>
@@ -43,6 +53,10 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
+ <dependency>
+ <groupId>redis.clients</groupId>
+ <artifactId>jedis</artifactId>
+ </dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
@@ -143,14 +157,6 @@
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
</dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- </dependency>
</dependencies>
 
</project>
frame-core/src/main/java/cn/platform/agent/frame/core/aop/ServiceAspect.java
@@ -6,41 +6,45 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
+ * @author Bob Lee
* @ClassName: ServiceAspect
* @Description: 业务切面,对业务逻辑的公共处理
- * @author Bob Lee
* @date 2017年3月29日 下午2:22:00
*/
public class ServiceAspect {
 
- private IExceptionConverter exceptionConverter;
-
- private static final Logger logger = LoggerFactory.getLogger(ServiceAspect.class);
-
- public Object around(ProceedingJoinPoint pjp) throws Throwable {
-
- try {
- return pjp.proceed(pjp.getArgs());
- } catch (DaoException e) {
- return this.exceptionConverter.convert(e);
- } catch (ValidationException e) {
- return this.exceptionConverter.convert(e);
- } catch (ServiceException e) {
- return this.exceptionConverter.convert(e);
- } catch (ComponentException e) {
- return this.exceptionConverter.convert(e);
- } catch (Throwable e) {
- logger.error(e.getMessage(), e);
- return this.exceptionConverter.convert(e);
- }
- }
-
- public IExceptionConverter getExceptionConverter() {
- return exceptionConverter;
- }
-
- public void setExceptionConverter(IExceptionConverter exceptionConverter) {
- this.exceptionConverter = exceptionConverter;
- }
+ private IExceptionConverter exceptionConverter;
+
+ private static final Logger logger = LoggerFactory.getLogger(ServiceAspect.class);
+
+ public Object around(ProceedingJoinPoint pjp) throws Throwable {
+
+ try {
+ return pjp.proceed(pjp.getArgs());
+ } catch (DaoException e) {
+ logger.error(e.getMessage(), e);
+ return this.exceptionConverter.convert(e);
+ } catch (ValidationException e) {
+ //logger.error(e.getMessage(), e);
+ return this.exceptionConverter.convert(e);
+ } catch (ServiceException e) {
+ logger.error(e.getMessage(), e);
+ return this.exceptionConverter.convert(e);
+ } catch (ComponentException e) {
+ logger.error(e.getMessage(), e);
+ return this.exceptionConverter.convert(e);
+ } catch (Throwable e) {
+ logger.error(e.getMessage(), e);
+ return this.exceptionConverter.convert(e);
+ }
+ }
+
+ public IExceptionConverter getExceptionConverter() {
+ return exceptionConverter;
+ }
+
+ public void setExceptionConverter(IExceptionConverter exceptionConverter) {
+ this.exceptionConverter = exceptionConverter;
+ }
 
}
frame-core/src/main/java/cn/platform/agent/frame/core/common/ResultBean.java
@@ -45,14 +45,15 @@ public class ResultBean&lt;E&gt; {
this.messageBeans = MessageBeanHelper.buildForOne(messageBean);
}
 
- public ResultBean(boolean success, MessageBean messageBean, E resultData) {
+
+ public ResultBean(boolean success, MessageBean messageBean, E resultData) {
super();
this.success = success;
this.messageBeans = MessageBeanHelper.buildForOne(messageBean);
this.resultData = resultData;
}
 
- public boolean isSuccess() {
+ public boolean isSuccess() {
return success;
}
 
frame-core/src/main/java/cn/platform/agent/frame/core/common/ResultBeanHelper.java
package cn.platform.agent.frame.core.common;
 
+import java.util.List;
+
public class ResultBeanHelper {
 
public static ResultBean success() {
@@ -17,4 +19,11 @@ public class ResultBeanHelper {
return result;
}
 
+ public static ResultBean error(MessageBean messageBean){
+ return new ResultBean(false, messageBean);
+ }
+
+ public static ResultBean error(List<MessageBean> messageBean){
+ return new ResultBean(false, messageBean , null , null);
+ }
}
frame-core/src/main/java/cn/platform/agent/frame/core/config/PropertyPlaceholder.java 0 → 100644
+package cn.platform.agent.frame.core.config;
+
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
+
+import java.util.Properties;
+
+/**
+ * <p>标题: 读取属性文件Property </p>
+ * <p>功能描述: </p>
+ * <p>创建时间:17/4/13 </p>
+ * <p>作者:SIVEN</p>
+ * <p>修改历史记录:</p>
+ * ============================================================<br>
+ */
+public class PropertyPlaceholder extends PropertyPlaceholderConfigurer {
+ private Properties props; // 存取properties配置文件key-value结果
+
+ @Override
+ protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
+ throws BeansException {
+ super.processProperties(beanFactoryToProcess, props);
+ this.props = props;
+ }
+
+ public String getValue(String key){
+ return this.props.getProperty(key);
+ }
+
+ public String getValue(String key, String defaultValue) {
+ return this.props.getProperty(key, defaultValue);
+ }
+
+ public Object setValue(String key, String value) {
+ return this.props.setProperty(key, value);
+ }
+}
frame-core/src/main/java/cn/platform/agent/frame/core/exception/AuthorizationException.java 0 → 100644
+package cn.platform.agent.frame.core.exception;
+
+/**
+ * <p>标题: 定义权限异常 </p>
+ * <p>功能描述: </p>
+ * <p>创建时间:17/4/21 </p>
+ * <p>作者:SIVEN</p>
+ * <p>修改历史记录:</p>
+ * ============================================================<br>
+ */
+public class AuthorizationException extends Exception {
+
+ public AuthorizationException() {
+ super("系统拦截到用户尚未登录或登录已过期! >| 跳转到登录页面..");
+ }
+
+
+}
frame-core/src/main/java/cn/platform/agent/frame/core/helper/DesHelper.java 0 → 100644
+package cn.platform.agent.frame.core.helper;
+
+import javax.crypto.Cipher;
+import java.security.Key;
+import java.security.Security;
+
+/**
+ * DES加密和解密工具,可以对字符串进行加密和解密操作 。
+ */
+public class DesHelper {
+
+ /** 字符串默认键值 */
+ private static String strDefaultKey = "national";
+
+ /** 加密工具 */
+ private Cipher encryptCipher = null;
+
+ /** 解密工具 */
+ private Cipher decryptCipher = null;
+
+ /**
+ * 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
+ * hexStr2ByteArr(String strIn) 互为可逆的转换过程
+ *
+ * @param arrB
+ * 需要转换的byte数组
+ * @return 转换后的字符串
+ * @throws Exception
+ * 本方法不处理任何异常,所有异常全部抛出
+ */
+ public static String byteArr2HexStr(byte[] arrB) throws Exception {
+ int iLen = arrB.length;
+ // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
+ StringBuffer sb = new StringBuffer(iLen * 2);
+ for (int i = 0; i < iLen; i++) {
+ int intTmp = arrB[i];
+ // 把负数转换为正数
+ while (intTmp < 0) {
+ intTmp = intTmp + 256;
+ }
+ // 小于0F的数需要在前面补0
+ if (intTmp < 16) {
+ sb.append("0");
+ }
+ sb.append(Integer.toString(intTmp, 16));
+ }
+ return sb.toString();
+ }
+
+ /**
+ * 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
+ * 互为可逆的转换过程
+ *
+ * @param strIn
+ * 需要转换的字符串
+ * @return 转换后的byte数组
+ * @throws Exception
+ * 本方法不处理任何异常,所有异常全部抛出
+ * @author <a href="mailto:leo841001@163.com">LiGuoQing</a>
+ */
+ public static byte[] hexStr2ByteArr(String strIn) throws Exception {
+ byte[] arrB = strIn.getBytes();
+ int iLen = arrB.length;
+
+ // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
+ byte[] arrOut = new byte[iLen / 2];
+ for (int i = 0; i < iLen; i = i + 2) {
+ String strTmp = new String(arrB, i, 2);
+ arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
+ }
+ return arrOut;
+ }
+
+ /**
+ * 默认构造方法,使用默认密钥
+ *
+ * @throws Exception
+ */
+ public DesHelper() throws Exception {
+ this(strDefaultKey);
+ }
+
+ /**
+ * 指定密钥构造方法
+ *
+ * @param strKey
+ * 指定的密钥
+ * @throws Exception
+ */
+ public DesHelper(String strKey) throws Exception {
+ Security.addProvider(new com.sun.crypto.provider.SunJCE());
+ Key key = getKey(strKey.getBytes());
+
+ encryptCipher = Cipher.getInstance("DES");
+ encryptCipher.init(Cipher.ENCRYPT_MODE, key);
+
+ decryptCipher = Cipher.getInstance("DES");
+ decryptCipher.init(Cipher.DECRYPT_MODE, key);
+ }
+
+ /**
+ * 加密字节数组
+ *
+ * @param arrB
+ * 需加密的字节数组
+ * @return 加密后的字节数组
+ * @throws Exception
+ */
+ public byte[] encrypt(byte[] arrB) throws Exception {
+ return encryptCipher.doFinal(arrB);
+ }
+
+ /**
+ * 加密字符串
+ *
+ * @param strIn
+ * 需加密的字符串
+ * @return 加密后的字符串
+ * @throws Exception
+ */
+ public String encrypt(String strIn) throws Exception {
+ return byteArr2HexStr(encrypt(strIn.getBytes()));
+ }
+
+ /**
+ * 解密字节数组
+ *
+ * @param arrB
+ * 需解密的字节数组
+ * @return 解密后的字节数组
+ * @throws Exception
+ */
+ public byte[] decrypt(byte[] arrB) throws Exception {
+ return decryptCipher.doFinal(arrB);
+ }
+
+ /**
+ * 解密字符串
+ *
+ * @param strIn
+ * 需解密的字符串
+ * @return 解密后的字符串
+ * @throws Exception
+ */
+ public String decrypt(String strIn) throws Exception {
+ return new String(decrypt(hexStr2ByteArr(strIn)));
+ }
+
+ /**
+ * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
+ *
+ * @param arrBTmp
+ * 构成该字符串的字节数组
+ * @return 生成的密钥
+ * @throws java.lang.Exception
+ */
+ private Key getKey(byte[] arrBTmp) throws Exception {
+ // 创建一个空的8位字节数组(默认值为0)
+ byte[] arrB = new byte[8];
+
+ // 将原始字节数组转换为8位
+ for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
+ arrB[i] = arrBTmp[i];
+ }
+
+ // 生成密钥
+ Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
+
+ return key;
+ }
+
+ /**
+ * main方法 。
+ * @author 刘尧兴
+ * @param args
+ */
+ public static void main(String[] args) {
+ try {
+ String test = "123456789";
+ DesHelper des = new DesHelper("smp");//自定义密钥
+ System.out.println("加密前的字符:" + test);
+ System.out.println("加密后的字符:" + des.encrypt(test));
+ System.out.println("解密后的字符:" + des.decrypt(des.encrypt(test)));
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+}
frame-core/src/main/java/cn/platform/agent/frame/core/helper/ProtobuffSerializationHelper.java 0 → 100644
+package cn.platform.agent.frame.core.helper;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.springframework.objenesis.Objenesis;
+import org.springframework.objenesis.ObjenesisStd;
+
+
+/**
+ * <p>标题: </p>
+ * <p>功能描述: </p>
+ * <p>创建时间:17/4/9 </p>
+ * <p>作者:SIVEN</p>
+ * <p>修改历史记录:</p>
+ * ============================================================<br>
+ */
+public class ProtobuffSerializationHelper {
+}
frame-core/src/main/java/cn/platform/agent/frame/core/helper/StackTraceHelper.java 0 → 100644
+package cn.platform.agent.frame.core.helper;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+/**
+ * <p>标题: 异常堆栈信息工具类</p>
+ * <p>功能描述: </p>
+ * <p>版权: 税友软件集团股份限公司</p>
+ * <p>创建时间:16/12/22 </p>
+ * <p>作者:SIVEN</p>
+ * <p>修改历史记录:</p>
+ * ============================================================<br>
+ */
+public class StackTraceHelper {
+
+ public static String getMessage(String message , Exception e){
+ try {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ e.printStackTrace(pw);
+ String exceptionInfo = sw.toString();
+ return message + " ,Error Message: " +exceptionInfo;
+ } catch (Exception e1) {
+ return "get StackTrace Message abnormal!!";
+ }
+ }
+
+ /**
+ * 将捕获的异常信息转String
+ *
+ * @param e
+ * @return
+ */
+ public static String getMessage(Exception e) {
+ try {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ e.printStackTrace(pw);
+ String exceptionInfo = sw.toString();
+ return exceptionInfo;
+ } catch (Exception e1) {
+ return "get StackTrace Message abnormal!!";
+ }
+ }
+
+}
frame-core/src/main/java/cn/platform/agent/frame/core/helper/ThrowableInfoHelper.java 0 → 100644
+package cn.platform.agent.frame.core.helper;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.*;
+import java.util.ArrayList;
+
+/**
+ * <p>标题: </p>
+ * <p>功能描述: </p>
+ * <p>创建时间:17/4/12 </p>
+ * <p>作者:SIVEN</p>
+ * <p>修改历史记录:</p>
+ * ============================================================<br>
+ */
+public class ThrowableInfoHelper {
+
+ protected final Log logger = LogFactory.getLog(getClass());
+ /**
+ * =======================================================================
+ * 获得操作系统中,换行字符串。如Micorsoft Windows XP专业版中换行符"\r\n"
+ * =======================================================================
+ */
+ // Note that the line.separator property can be looked up even by applets.
+ // 参考org.apache.log4j.Layout
+ public final static String LINE_SEP = System.getProperty("line.separator");
+ // 参考java.io.BufferedWriter
+ public final static String LINE_SEP2 = (String) java.security.AccessController
+ .doPrivileged(new sun.security.action.GetPropertyAction(
+ "line.separator"));
+
+ /**
+ * @param throwable 异常对象
+ * @return
+ * @Description:将Throwable对象的错误堆栈内容形成字符串<br> 参考
+ * {@link org.apache.log4j.spi.ThrowableInformation}
+ * 代码
+ * @author mahh
+ * @since:2014-9-30 下午02:32:51
+ */
+ public static String[] getThrowableStrRep(Throwable throwable) {
+ if (throwable == null) {
+ return new String[0];
+ }
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ throwable.printStackTrace(pw);
+ pw.flush();
+ LineNumberReader reader = new LineNumberReader(new StringReader(
+ sw.toString()));
+ ArrayList lines = new ArrayList();
+ try {
+ String line = reader.readLine();
+ while (line != null) {
+ lines.add(line);
+ line = reader.readLine();
+ }
+ } catch (IOException ex) {
+ lines.add(ex.toString());
+ }
+ String[] rep = new String[lines.size()];
+ lines.toArray(rep);
+ return rep;
+ }
+
+
+ public static String getThrowableStr(Throwable throwable) {
+ StringBuffer throwableStr = new StringBuffer();
+ String[] errorStrArray = getThrowableStrRep(throwable);
+ for (int i = 0; i < errorStrArray.length; i++) {
+ throwableStr.append(errorStrArray[i]).append(LINE_SEP2);
+ }
+ return throwableStr.toString();
+ }
+
+ // 测试代码
+ public static void main(String[] args) {
+ RuntimeException e1 = new RuntimeException("aaa");
+ RuntimeException e2 = new RuntimeException("e2", e1);
+ RuntimeException e3 = new RuntimeException(e2);
+
+ System.out.println("=====");
+ String throwableStr = getThrowableStr(e3);
+ System.out.println(throwableStr);
+ }
+}
frame-core/src/main/java/cn/platform/agent/frame/core/redis/AbstractBaseRedis.java 0 → 100644
+package cn.platform.agent.frame.core.redis;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.RedisSerializer;
+
+/**
+ * <p>标题: </p>
+ * <p>功能描述: </p>
+ * <p>创建时间:17/4/9 </p>
+ * <p>作者:SIVEN</p>
+ * <p>修改历史记录:</p>
+ * ============================================================<br>
+ */
+public abstract class AbstractBaseRedis<K, V> {
+
+ @Autowired
+ protected RedisTemplate<K, V> redisTemplate;
+
+ /**
+ * 设置redisTemplate
+ * @param redisTemplate the redisTemplate to set
+ */
+ public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
+ this.redisTemplate = redisTemplate;
+ }
+
+ /**
+ * 获取 String RedisSerializer
+ * <br>------------------------------<br>
+ */
+ protected RedisSerializer<String> getStringSerializer() {
+ return redisTemplate.getStringSerializer();
+ }
+
+}
frame-core/src/main/java/cn/platform/agent/frame/core/redis/RedisClient.java 0 → 100755
frame-web/pom.xml 0 → 100644
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>cn.platform.agent.frame</groupId>
+ <artifactId>frame-main</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>frame-web</artifactId>
+ <name>frame-web</name>
+ <packaging>jar</packaging>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>frame-core</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ </dependency>
+ <!-- web 相关依赖 -->
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-web</artifactId>
+ <exclusions>
+ <exclusion>
+ <artifactId>commons-logging</artifactId>
+ <groupId>commons-logging</groupId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context-support</artifactId>
+ </dependency>
+ <dependency>
+ <artifactId>spring-webmvc</artifactId>
+ <groupId>org.springframework</groupId>
+ </dependency>
+
+ <!-- jackson -->
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
frame-web/src/main/java/cn/platform/agent/frame/web/BufferedResponseWrapper.java 0 → 100644
+package cn.platform.agent.frame.web;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+
+public class BufferedResponseWrapper extends HttpServletResponseWrapper {
+
+ private TeeServletOutputStream mTeeOutputStream;
+
+ private static class TeeOutputStream extends OutputStream {
+ private OutputStream mChainStream;
+ private OutputStream mTeeStream;
+
+ public TeeOutputStream(OutputStream chainStream, OutputStream teeStream) {
+ mChainStream = chainStream;
+ mTeeStream = teeStream;
+ }
+
+ @Override
+ public void write(int b) throws IOException {
+ mChainStream.write(b);
+ mTeeStream.write(b);
+ mTeeStream.flush();
+ }
+
+ @Override
+ public void close() throws IOException {
+ flush();
+ mChainStream.close();
+ mTeeStream.close();
+ }
+
+ @Override
+ public void flush() throws IOException {
+ mChainStream.close();
+ }
+ }
+
+ public class TeeServletOutputStream extends ServletOutputStream {
+ private final TeeOutputStream targetStream;
+
+ public TeeServletOutputStream(OutputStream one, OutputStream two) {
+ targetStream = new TeeOutputStream(one, two);
+ }
+
+ @Override
+ public void write(int b) throws IOException {
+ this.targetStream.write(b);
+ }
+
+ @Override
+ public void flush() throws IOException {
+ super.flush();
+ this.targetStream.flush();
+ }
+
+ @Override
+ public void close() throws IOException {
+ super.close();
+ this.targetStream.close();
+ }
+ }
+
+ private ByteArrayOutputStream mByteArrayOutputStream;
+
+ public BufferedResponseWrapper(HttpServletResponse response) throws IOException {
+ super(response);
+ mByteArrayOutputStream = new ByteArrayOutputStream();
+ mTeeOutputStream = new TeeServletOutputStream(super.getResponse().getOutputStream(), mByteArrayOutputStream);
+ }
+
+ @Override
+ public PrintWriter getWriter() throws IOException {
+ return super.getResponse().getWriter();
+ }
+
+ @Override
+ public ServletOutputStream getOutputStream() throws IOException {
+ return mTeeOutputStream;
+ }
+
+ public String getResponseBody() {
+ return mByteArrayOutputStream.toString();
+ }
+
+}
frame-web/src/main/java/cn/platform/agent/frame/web/LogHttpServletReuqestWrapper.java 0 → 100644
+package cn.platform.agent.frame.web;
+
+
+import cn.platform.agent.frame.core.helper.IOHelper;
+
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+public class LogHttpServletReuqestWrapper extends HttpServletRequestWrapper {
+
+ private String _body;
+
+ public LogHttpServletReuqestWrapper(HttpServletRequest request) throws IOException {
+ super(request);
+ _body = IOHelper.toString(request.getReader());
+ }
+
+ @Override
+ public ServletInputStream getInputStream() throws IOException {
+ final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_body.getBytes());
+ return new ServletInputStream() {
+ @Override
+ public int read() throws IOException {
+ return byteArrayInputStream.read();
+ }
+ };
+ }
+
+ @Override
+ public BufferedReader getReader() throws IOException {
+ return new BufferedReader(new InputStreamReader(this.getInputStream()));
+ }
+
+ public String getBody() {
+ return this._body;
+ }
+
+}
frame-web/src/main/java/cn/platform/agent/frame/web/WebExceptionHandler.java 0 → 100644
+package cn.platform.agent.frame.web;
+
+import cn.platform.agent.frame.core.common.MessageBean;
+import cn.platform.agent.frame.core.common.MessageBeanHelper;
+import cn.platform.agent.frame.core.common.ResultBean;
+import cn.platform.agent.frame.core.common.ResultType;
+import cn.platform.agent.frame.core.exception.DaoException;
+import cn.platform.agent.frame.core.exception.ValidationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.converter.HttpMessageConversionException;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * @ClassName: GlobalExceptionHandler
+ * @Description: 全局的异常处理类,用于处理所有的异常信息
+ * @author Bob Lee
+ * @date 2017年3月14日 下午2:46:39
+ */
+@ControllerAdvice
+public class WebExceptionHandler {
+
+ private static final Logger logger = LoggerFactory.getLogger(WebExceptionHandler.class);
+
+ @ExceptionHandler(value = HttpMessageConversionException.class)
+ @ResponseBody
+ public ResultBean exceptionHandler(HttpServletRequest req, HttpMessageConversionException e) throws Exception {
+ ResultBean resultBean = new ResultBean();
+ resultBean.setSuccess(false);
+ resultBean.setType(ResultType.MESSAGE_CONVERT_EXCEPTION.toString());
+ resultBean.setMessageBeans(MessageBeanHelper.buildForOne(new MessageBean(e.getMessage())));
+ return resultBean;
+ }
+
+ @ExceptionHandler(value = ValidationException.class)
+ @ResponseBody
+ public ResultBean exceptionHandler(HttpServletRequest req, ValidationException e) throws Exception {
+ ResultBean<ValidationException> result = new ResultBean<>();
+ result.setSuccess(false);
+ result.setType(ResultType.VALIDATION_EXCEPTION.toString());
+ result.setMessageBeans(e.getMessageBeans());
+ return result;
+ }
+
+ @ExceptionHandler(value = DaoException.class)
+ @ResponseBody
+ public ResultBean exceptionHandler(HttpServletRequest req, DaoException e) throws Exception {
+ logger.error("DaoException", e);
+ ResultBean<DaoException> result = new ResultBean<>();
+ result.setSuccess(false);
+ result.setType(ResultType.DAO_EXCEPTION.toString());
+ result.setMessageBeans(MessageBeanHelper.buildForOne(e.getMessageBean()));
+ return result;
+ }
+
+ @ExceptionHandler(value = Exception.class)
+ @ResponseBody
+ public ResultBean exceptionHandler(HttpServletRequest req, Exception e) throws Exception {
+ logger.error(e.getMessage(), e);
+ ResultBean resultBean = new ResultBean();
+ resultBean.setSuccess(false);
+ resultBean.setType(ResultType.UNKWON_EXCEPTION.toString());
+ resultBean.setMessageBeans(MessageBeanHelper.buildForOne(new MessageBean(e.getMessage())));
+ return resultBean;
+ }
+
+}
frame-web/src/main/java/cn/platform/agent/frame/web/controller/BaseController.java 0 → 100644
+package cn.platform.agent.frame.web.controller;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.annotation.ModelAttribute;
+
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+/**
+ * <p>标题: </p>
+ * <p>功能描述: </p>
+ * <p>创建时间:17/4/21 </p>
+ * <p>作者:SIVEN</p>
+ * <p>修改历史记录:</p>
+ * ============================================================<br>
+ */
+public class BaseController {
+
+ private Logger logger = LoggerFactory.getLogger(getClass());
+ /**
+ * ThreadLocal确保高并发下每个请求的request,response都是独立的
+ */
+ private static ThreadLocal<ServletRequest> currentRequest = new ThreadLocal<ServletRequest>();
+ private static ThreadLocal<ServletResponse> currentResponse = new ThreadLocal<ServletResponse>();
+
+ /**
+ * 线程安全初始化reque,respose对象
+ *
+ * @param request
+ * @param response
+ */
+ @ModelAttribute
+ public void initReqAndRep(HttpServletRequest request, HttpServletResponse response) {
+ currentRequest.set(request);
+ currentResponse.set(response);
+ }
+
+ /**
+ * 线程安全
+ *
+ * @return
+ */
+ public HttpServletRequest request() {
+ return (HttpServletRequest) currentRequest.get();
+ }
+
+ /**
+ * 线程安全
+ *
+ * @return
+ */
+ public HttpServletResponse response() {
+ return (HttpServletResponse) currentResponse.get();
+ }
+
+ /**
+ * 获取Session对象
+ * @return
+ */
+ public HttpSession session() {
+ return this.request().getSession();
+ }
+
+}
frame-web/src/main/java/cn/platform/agent/frame/web/filter/ExceptionFilter.java 0 → 100644
+package cn.platform.agent.frame.web.filter;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.*;
+import java.io.IOException;
+
+public class ExceptionFilter implements Filter {
+
+ private static final Logger logger = LoggerFactory.getLogger(ExceptionFilter.class);
+
+ private IExceptionHandler exceptionHandler;
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
+ try {
+ chain.doFilter(request, response);
+ } catch (Throwable e) {
+ logger.error("exception filter throwable:" + e.getMessage());
+ this.exceptionHandler.handle(request, response, e);
+ }
+
+ }
+
+ @Override
+ public void destroy() {
+ }
+
+ public IExceptionHandler getExceptionHandler() {
+ return exceptionHandler;
+ }
+
+ public void setExceptionHandler(IExceptionHandler exceptionHandler) {
+ this.exceptionHandler = exceptionHandler;
+ }
+
+}
frame-web/src/main/java/cn/platform/agent/frame/web/filter/IExceptionHandler.java 0 → 100644
+package cn.platform.agent.frame.web.filter;
+
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+/**
+ * @ClassName: IExceptionHandler
+ * @Description: 异常处理类,用于web端异常过滤器使用,用于处理controller的业务异常或未处理异常。
+ * @author Bob Lee
+ * @date 2017年3月14日 上午9:11:22
+ */
+public interface IExceptionHandler {
+
+ void handle(ServletRequest request, ServletResponse response, Throwable e);
+
+}
frame-web/src/main/java/cn/platform/agent/frame/web/filter/RestfulLogFilter.java 0 → 100644
+package cn.platform.agent.frame.web.filter;
+
+import cn.platform.agent.frame.core.helper.UUIDHelper;
+import cn.platform.agent.frame.web.BufferedResponseWrapper;
+import cn.platform.agent.frame.web.LogHttpServletReuqestWrapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
+
+import javax.servlet.*;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * 日志记录过滤器,使用logback来记录日志,通过logback来配置日志的文件路径等
+ *
+ * @author libx
+ */
+public class RestfulLogFilter implements Filter {
+
+ private static final Logger logger = LoggerFactory.getLogger(RestfulLogFilter.class);
+
+ /**
+ * 是否记录日志,当为false时,不记录日志
+ */
+ private boolean actlog = true;
+
+ /**
+ * 日志的分隔符
+ */
+ private String seperate = "|";
+
+ @Override
+ public void destroy() {
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+ throws IOException, ServletException {
+
+ MDC.put("reqSerial", UUIDHelper.getUuid());
+
+ if (actlog) {
+
+ LogHttpServletReuqestWrapper httpRequest = new LogHttpServletReuqestWrapper((HttpServletRequest) request);
+ BufferedResponseWrapper httpResponse = new BufferedResponseWrapper((HttpServletResponse) response);
+ logger.debug("===============filter begin, Url[{}]===============", httpRequest.getRequestURI());
+ Long requestTime = System.currentTimeMillis();
+ try {
+ chain.doFilter(httpRequest, httpResponse);
+ } catch (Exception e) {
+ logger.error(e.getMessage(), e);
+ }
+ Long responseTime = System.currentTimeMillis();
+ StringBuilder sb = new StringBuilder();
+ sb.append(httpRequest.getRequestURI());// 请求的URL
+ sb.append(seperate);
+ sb.append(requestTime); // 请求时间
+ sb.append(seperate);
+ sb.append(httpRequest.getBody()); // 请求的内容
+ sb.append(seperate);
+ sb.append(responseTime); // 响应时间
+ sb.append(seperate);
+ sb.append(httpResponse.getResponseBody()); // 请求的响应内容
+ sb.append(seperate);
+ sb.append(responseTime - requestTime);// 整个请求的使用时长
+ logger.info(sb.toString());
+ } else {
+ // 不记录日志
+ chain.doFilter(request, response);
+ }
+ }
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ }
+
+ public void setActlog(boolean actlog) {
+ this.actlog = actlog;
+ }
+
+ public void setSeperate(String seperate) {
+ this.seperate = seperate;
+ }
+
+}
frame-web/src/main/java/cn/platform/agent/frame/web/filter/UnknowExceptionHandler.java 0 → 100644
+package cn.platform.agent.frame.web.filter;
+
+import cn.platform.agent.frame.core.common.MessageBean;
+import cn.platform.agent.frame.core.common.MessageBeanHelper;
+import cn.platform.agent.frame.core.common.ResultBean;
+import cn.platform.agent.frame.core.common.ResultType;
+import cn.platform.agent.frame.core.helper.JsonHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * @ClassName: UnknowExceptionHandler
+ * @Description: 未知异常处理,当controller发生未知异常时,则异常转成{@code ResultBean},并在
+ * {@code ServletResponse}中直接以JSON字节流输出
+ * @author Bob Lee
+ * @date 2017年3月15日 下午2:18:01
+ */
+public class UnknowExceptionHandler implements IExceptionHandler {
+
+ private static final Logger logger = LoggerFactory.getLogger(UnknowExceptionHandler.class);
+
+ @Override
+ public void handle(ServletRequest request, ServletResponse response, Throwable e) {
+ ResultBean<Throwable> result = new ResultBean<>();
+ result.setSuccess(false);
+ result.setType(ResultType.UNKWON_EXCEPTION.toString());
+ result.setMessageBeans(MessageBeanHelper.buildForOne(new MessageBean(e.getMessage())));
+ result.setResultData(e);
+ writeResult(response, result);
+ }
+
+ private void writeResult(ServletResponse response, ResultBean<?> result) {
+ try (OutputStream os = response.getOutputStream();) {
+ os.write(JsonHelper.toJSONString(result).getBytes());
+ os.flush();
+ os.close();
+ } catch (IOException e) {
+ logger.error(e.getMessage(), e);
+ }
+ }
+
+}
frame-web/src/main/java/cn/platform/agent/frame/web/interceptor/SecurityInterceptor.java 0 → 100644
+package cn.platform.agent.frame.web.interceptor;
+
+import cn.platform.agent.frame.core.exception.AuthorizationException;
+import org.springframework.util.StringUtils;
+import org.springframework.web.servlet.HandlerInterceptor;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import java.util.List;
+
+/**
+ * <p>标题: 登录认证拦截器</p>
+ * <p>功能描述: </p>
+ * <p>创建时间:17/4/21 </p>
+ * <p>作者:SIVEN</p>
+ * <p>修改历史记录:</p>
+ * ============================================================<br>
+ */
+public class SecurityInterceptor implements HandlerInterceptor {
+
+ private String sessionKey;
+ private List<String> excludedUrls;
+
+ public void setSessionKey(String sessionKey) {
+ this.sessionKey = sessionKey;
+ }
+
+ public void setExcludedUrls(List<String> excludedUrls) {
+ this.excludedUrls = excludedUrls;
+ }
+
+ @Override
+ public boolean preHandle(HttpServletRequest request,
+ HttpServletResponse response, Object handler) throws Exception {
+ // excluded URLs:
+ // see http://stackoverflow.com/questions/9908124/spring-mvc-3-interceptor-on-all-excluding-some-defined-paths
+ String requestUri = request.getRequestURI();
+ if (null!=excludedUrls)
+ for (String url : excludedUrls) {
+ if (requestUri.endsWith(url)) {
+ return true;
+ }
+ }
+
+ // intercept
+ HttpSession session = request.getSession();
+ if (session.getAttribute(sessionKey) == null) {
+ // see http://stackoverflow.com/questions/12713873/spring-3-1-how-do-you-send-all-exception-to-one-page
+ throw new AuthorizationException();
+ } else {
+ return true;
+ }
+ }
+
+ @Override
+ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
+ }
+
+ @Override
+ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
+ }
+}
@@ -13,6 +13,7 @@
 
<!-- spring相关 -->
<spring.version>4.3.5.RELEASE</spring.version>
+ <spring.data.redis.version>1.6.0.RELEASE</spring.data.redis.version>
 
<!-- 数据库操作相关 -->
<spring.data.commons.version>1.13.0.RELEASE</spring.data.commons.version>
@@ -22,6 +23,7 @@
<mysql.version>5.1.36</mysql.version>
<mybatis-spring.version>1.3.1</mybatis-spring.version>
<mybatis.version>3.4.2</mybatis.version>
+ <redis.clients.version>2.7.3</redis.clients.version>
 
<!-- 分布式事务处理 -->
<atomikos.version>4.0.2</atomikos.version>
@@ -65,6 +67,12 @@
<version>${project.version}</version>
</dependency>
 
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>frame-web</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
@@ -160,6 +168,12 @@
<artifactId>spring-data-commons</artifactId>
<version>${spring.data.commons.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>org.springframework.data</groupId>
+ <artifactId>spring-data-redis</artifactId>
+ <version>${spring.data.redis.version}</version>
+ </dependency>
<!-- spring end -->
 
<!-- commons begin -->
@@ -287,6 +301,12 @@
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>redis.clients</groupId>
+ <artifactId>jedis</artifactId>
+ <version>${redis.clients.version}</version>
+ </dependency>
<!-- 数据连接池和数据库驱动end -->
<!-- mybatis -->
<dependency>
@@ -436,5 +456,6 @@
</build>
<modules>
<module>frame-core</module>
+ <module>frame-web</module>
</modules>
</project>
\ No newline at end of file