From 41532ef476d95f6877cfde2a82cd0fa22f42ce17 Mon Sep 17 00:00:00 2001 From: fanxb Date: Sat, 6 Jul 2019 17:38:19 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Refactor:=20[=E5=90=8E=E5=8F=B0]?= =?UTF-8?q?:=E5=AE=8C=E5=96=84common=E6=A8=A1=E5=9D=97=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bookmark/business/user/dao/UserDao.java | 11 +++ .../business/user/entity/LoginBody.java | 11 +++ .../business/user/entity/LoginRes.java | 11 +++ .../business/user/entity/RegisterBody.java | 11 +++ .../main/resources/mapper/user-userMapper.xml | 0 bookMarkService/common/pom.xml | 37 +++++++- .../com/fanxb/bookmark/common/Constant.java | 27 +++++- .../fanxb/bookmark/common/entity/User.java | 11 +++ .../common/exception/CustomException.java | 37 ++++++-- .../common/exception/ExceptionHandle.java | 6 +- .../common/exception/FormDataException.java | 14 +++ .../common/exception/NoLoginException.java | 14 +++ .../bookmark/common/filter/LoginFilter.java | 39 ++++++++ .../fanxb/bookmark/common/util/HashUtil.java | 88 +++++++++++++++++++ 14 files changed, 304 insertions(+), 13 deletions(-) create mode 100644 bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/dao/UserDao.java create mode 100644 bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/entity/LoginBody.java create mode 100644 bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/entity/LoginRes.java create mode 100644 bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/entity/RegisterBody.java create mode 100644 bookMarkService/business/user/src/main/resources/mapper/user-userMapper.xml create mode 100644 bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/FormDataException.java create mode 100644 bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/NoLoginException.java create mode 100644 bookMarkService/common/src/main/java/com/fanxb/bookmark/common/filter/LoginFilter.java create mode 100644 bookMarkService/common/src/main/java/com/fanxb/bookmark/common/util/HashUtil.java diff --git a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/dao/UserDao.java b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/dao/UserDao.java new file mode 100644 index 0000000..3764eeb --- /dev/null +++ b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/dao/UserDao.java @@ -0,0 +1,11 @@ +package com.fanxb.bookmark.business.user.dao; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/6 11:36 + */ +public class UserDao { +} diff --git a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/entity/LoginBody.java b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/entity/LoginBody.java new file mode 100644 index 0000000..fe62b12 --- /dev/null +++ b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/entity/LoginBody.java @@ -0,0 +1,11 @@ +package com.fanxb.bookmark.business.user.entity; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/6 17:25 + */ +public class LoginBody { +} diff --git a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/entity/LoginRes.java b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/entity/LoginRes.java new file mode 100644 index 0000000..63adb04 --- /dev/null +++ b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/entity/LoginRes.java @@ -0,0 +1,11 @@ +package com.fanxb.bookmark.business.user.entity; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/6 16:52 + */ +public class LoginRes { +} diff --git a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/entity/RegisterBody.java b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/entity/RegisterBody.java new file mode 100644 index 0000000..4d075eb --- /dev/null +++ b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/entity/RegisterBody.java @@ -0,0 +1,11 @@ +package com.fanxb.bookmark.business.user.entity; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/6 11:23 + */ +public class RegisterBody { +} diff --git a/bookMarkService/business/user/src/main/resources/mapper/user-userMapper.xml b/bookMarkService/business/user/src/main/resources/mapper/user-userMapper.xml new file mode 100644 index 0000000..e69de29 diff --git a/bookMarkService/common/pom.xml b/bookMarkService/common/pom.xml index 407b864..59c9152 100644 --- a/bookMarkService/common/pom.xml +++ b/bookMarkService/common/pom.xml @@ -9,7 +9,7 @@ 4.0.0 - common + bookmark-common @@ -43,6 +43,32 @@ commons-pool2 + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.0.1 + + + + + com.alibaba + druid-spring-boot-starter + 1.1.18 + + + + + org.flywaydb + flyway-core + 5.2.4 + + + + mysql + mysql-connector-java + + org.springframework.boot @@ -71,6 +97,15 @@ fastjson 1.2.56 + + + + + org.springframework.boot + spring-boot-starter-test + test + + diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/Constant.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/Constant.java index 30232e0..48280d2 100644 --- a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/Constant.java +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/Constant.java @@ -1,5 +1,6 @@ package com.fanxb.bookmark.common; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** @@ -26,7 +27,31 @@ public class Constant { /** * 验证码过期时间 */ - public static int VERIFY_CODE_EXPIRE = 15 * 60 * 1000; + public static int AUTH_CODE_EXPIRE = 15 * 60 * 1000; + /** + * Description: 生成email存在redis中的key + * + * @param email 邮箱地址 + * @return java.lang.String + * @author fanxb + * @date 2019/7/6 10:56 + */ + public static String authCodeKey(String email) { + return email + "_authCode"; + } + public static boolean isDev = false; + + @Value("${isDev}") + public void setIsDev(boolean isDev) { + Constant.isDev = isDev; + } + + public static String jwtSecret = ""; + + @Value("${jwtSecret}") + public void setJwtSecret(String jwtSecret) { + Constant.jwtSecret = jwtSecret; + } } diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/User.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/User.java index b3b7487..5133b47 100644 --- a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/User.java +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/User.java @@ -1,5 +1,7 @@ package com.fanxb.bookmark.common.entity; +import lombok.Data; + /** * 类功能简述: * 类功能详述: @@ -7,5 +9,14 @@ package com.fanxb.bookmark.common.entity; * @author fanxb * @date 2019/7/4 20:14 */ +@Data public class User { + + private int userId; + private String username; + private String email; + private String icon; + private String password; + private long createTime; + private long lastLoginTime; } diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/CustomException.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/CustomException.java index d127cbe..ceebc91 100644 --- a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/CustomException.java +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/CustomException.java @@ -1,7 +1,9 @@ package com.fanxb.bookmark.common.exception; +import com.fanxb.bookmark.common.util.StringUtil; + /** - * 类功能简述: + * 类功能简述: 自定义错误类,默认错误码为0,表示一般错误 * 类功能详述: * * @author fanxb @@ -9,32 +11,49 @@ package com.fanxb.bookmark.common.exception; */ public class CustomException extends RuntimeException { - String message; + private String message; + private int code; public CustomException() { - super(); + this(null, null, null); } public CustomException(String message) { - super(message); - this.message = message; + this(message, null, null); } - public CustomException(Exception e){ - super(e); + public CustomException(Exception e) { + this(null, null, e); } public CustomException(String message, Exception e) { + this(message, null, e); + } + + public CustomException(String message, Integer code, Exception e) { super(e); - this.message = message; + this.message = message == null ? "" : message; + this.code = code == null ? 0 : code; } @Override public String getMessage() { - if (this.message == null) { + if (StringUtil.isEmpty(this.message)) { return super.getMessage(); } else { return this.message; } } + + public void setMessage(String message) { + this.message = message; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } } diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/ExceptionHandle.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/ExceptionHandle.java index 3759697..976c63f 100644 --- a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/ExceptionHandle.java +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/ExceptionHandle.java @@ -21,10 +21,12 @@ public class ExceptionHandle { @ExceptionHandler(Exception.class) public Result handleException(Exception e) { logger.error("捕获到错误:", e); + CustomException ce; if (e instanceof CustomException) { - return Result.failed(e.getMessage()); + ce = (CustomException) e; } else { - return Result.failed(""); + ce = new CustomException(e); } + return new Result(ce.getCode(), ce.getMessage(), null); } } diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/FormDataException.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/FormDataException.java new file mode 100644 index 0000000..ec689f3 --- /dev/null +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/FormDataException.java @@ -0,0 +1,14 @@ +package com.fanxb.bookmark.common.exception; + +/** + * 类功能简述:表单错误引起的异常,使用-2表示 + * 类功能详述: + * + * @author fanxb + * @date 2019/7/6 17:08 + */ +public class FormDataException extends CustomException { + public FormDataException(String message) { + super(message, -2, null); + } +} diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/NoLoginException.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/NoLoginException.java new file mode 100644 index 0000000..ca7b256 --- /dev/null +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/NoLoginException.java @@ -0,0 +1,14 @@ +package com.fanxb.bookmark.common.exception; + +/** + * 类功能简述: 未登录异常,使用-1表示 + * 类功能详述: + * + * @author fanxb + * @date 2019/7/5 15:49 + */ +public class NoLoginException extends CustomException { + NoLoginException() { + super("您尚未登录", -1, null); + } +} diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/filter/LoginFilter.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/filter/LoginFilter.java new file mode 100644 index 0000000..d9043d0 --- /dev/null +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/filter/LoginFilter.java @@ -0,0 +1,39 @@ +package com.fanxb.bookmark.common.filter; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import java.io.IOException; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/5 16:07 + */ + +@Component +@WebFilter(urlPatterns = "/*", filterName = "loginFilter") +@Slf4j +@Order(100) +public class LoginFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void destroy() { + + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + filterChain.doFilter(servletRequest, servletResponse); + } +} diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/util/HashUtil.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/util/HashUtil.java new file mode 100644 index 0000000..d07d1e0 --- /dev/null +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/util/HashUtil.java @@ -0,0 +1,88 @@ +package com.fanxb.bookmark.common.util; + +import com.fanxb.bookmark.common.exception.CustomException; + +import java.security.MessageDigest; + +/** + * 类功能简述:消息摘要计算 + * 类功能详述: + * + * @author fanxb + * @date 2019/7/6 14:31 + */ +public class HashUtil { + + /** + * Description: md5 签名字符串 + * + * @param str 16进制字符串 + * @return java.lang.String + * @author fanxb + * @date 2019/7/6 14:38 + */ + public static String md5(String str) { + return hash(str, "MD5"); + } + + /** + * Description: 使用sha1签名字符串 + * + * @param str sha1签名字符串 + * @return java.lang.String + * @author fanxb + * @date 2019/7/6 14:40 + */ + public static String sha1(String str) { + return hash(str, "SHA1"); + } + + /** + * Description: 根据type签名str + * + * @param str str + * @param type 签名类别 + * @return java.lang.String + * @author fanxb + * @date 2019/7/6 14:40 + */ + public static String hash(String str, String type) { + try { + MessageDigest md = MessageDigest.getInstance(type); + md.update(str.getBytes()); + return bytesToHexString(md.digest()); + } catch (Exception e) { + throw new CustomException(e); + } + } + + + /** + * Description: 字节数组转16进制字符串 + * + * @param bits 字节数组 + * @return java.lang.String + * @author fanxb + * @date 2019/7/6 14:37 + */ + public static String bytesToHexString(byte[] bits) { + StringBuilder builder = new StringBuilder(); + for (int i = 0, length = bits.length; i < length; i++) { + int k = bits[i]; + if (k < 0) { + k += 256; + } + if (k < 16) { + builder.append("0"); + } + builder.append(Integer.toHexString(k)); + } + return builder.toString(); + } + + public static void main(String[] args){ + System.out.println(md5("abc")); + System.out.println(sha1("abc")); + } + +}