🔨 Refactor: [后台]:完善common模块代码

This commit is contained in:
fanxb 2019-07-06 17:38:19 +08:00
parent f2f7f14d3b
commit 41532ef476
14 changed files with 304 additions and 13 deletions

View File

@ -0,0 +1,11 @@
package com.fanxb.bookmark.business.user.dao;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/7/6 11:36
*/
public class UserDao {
}

View File

@ -0,0 +1,11 @@
package com.fanxb.bookmark.business.user.entity;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/7/6 17:25
*/
public class LoginBody {
}

View File

@ -0,0 +1,11 @@
package com.fanxb.bookmark.business.user.entity;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/7/6 16:52
*/
public class LoginRes {
}

View File

@ -0,0 +1,11 @@
package com.fanxb.bookmark.business.user.entity;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/7/6 11:23
*/
public class RegisterBody {
}

View File

@ -9,7 +9,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common</artifactId>
<artifactId>bookmark-common</artifactId>
<dependencies>
@ -43,6 +43,32 @@
<artifactId>commons-pool2</artifactId>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!--druid连接池依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.18</version>
</dependency>
<!--数据库版本管理-->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.2.4</version>
</dependency>
<!--mysql jdbc依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--邮件依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
@ -71,6 +97,15 @@
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<!--单元测试-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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