提交 c2f37915 authored 作者: 李秋林's avatar 李秋林

DataUtils、JwtUtils工具类放到commont模块中;修改pom文件中对parent版本依赖

上级 aef3d43b
......@@ -27,6 +27,195 @@
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${alibaba-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- Web 场景启动器) 来为 Web 开发予以支持。spring-boot-starter-web 为我们提供了嵌入的 Servlet 容器以及 SpringMVC 的依赖,并为 Spring MVC 提供了大量自动配置,可以适用于大多数 Web 开发场景。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-annotation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk18on -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
</dependency>
<!-- 微信小程序 SDK 依赖 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 腾讯云服务 -->
<!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 -->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-sms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.auth0/java-jwt -->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.xuxueli/xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<artifactId>commons-io</artifactId>
<groupId>commons-io</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
......
......@@ -3,7 +3,7 @@ package com.wangxiaolu.promotion.controller.user;
import com.wangxiaolu.promotion.exception.ParamException;
import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.service.user.TencentCoreService;
import com.wangxiaolu.promotion.utils.DataUtils;
import com.wangxiaolu.promotion.common.util.DataUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......
......@@ -8,7 +8,7 @@ import com.wangxiaolu.promotion.pojo.user.dto.WxTemporaryInfoDto;
import com.wangxiaolu.promotion.pojo.user.vo.WxTemporaryEnrollVo;
import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.service.wechat.WeChatUserCoreService;
import com.wangxiaolu.promotion.utils.DataUtils;
import com.wangxiaolu.promotion.common.util.DataUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
......
......@@ -5,7 +5,7 @@ import com.wangxiaolu.promotion.pojo.user.dto.WxTemporaryInfoDto;
import com.wangxiaolu.promotion.pojo.user.vo.WxTemporaryLoginVo;
import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.service.wechat.WeChatUserQueryService;
import com.wangxiaolu.promotion.utils.DataUtils;
import com.wangxiaolu.promotion.common.util.DataUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......
......@@ -5,7 +5,7 @@ import com.wangxiaolu.promotion.common.redis.service.RedisCache;
import com.wangxiaolu.promotion.exception.ParamException;
import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.service.user.TencentCoreService;
import com.wangxiaolu.promotion.utils.DataUtils;
import com.wangxiaolu.promotion.common.util.DataUtils;
import com.wangxiaolu.promotion.utils.TencentUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
......@@ -44,8 +44,8 @@ public class TencentCoreServiceImpl implements TencentCoreService {
log.info("腾讯云短信,发送验证码:{}-{}", phone, phoneVerCode);
redisCache.addToMinute(redisKey, phoneVerCode, overdueLong);
boolean succss = tencentUtils.sendSmsPhoneVerCode(phone, phoneVerCode);
// boolean succss = true;
// boolean succss = tencentUtils.sendSmsPhoneVerCode(phone, phoneVerCode);
boolean succss = true;
if (!succss) {
redisCache.removeKey(redisKey);
log.info("腾讯云短信发送失败:删除验证码:{}-{}", phone, phoneVerCode);
......
......@@ -4,9 +4,8 @@ import com.alibaba.fastjson.JSONObject;
import com.wangxiaolu.promotion.common.redis.service.RedisCache;
import com.wangxiaolu.promotion.domain.user.dao.TemporaryInfoDao;
import com.wangxiaolu.promotion.pojo.user.dto.WxTemporaryInfoDto;
import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.service.wechat.WeChatUserQueryService;
import com.wangxiaolu.promotion.utils.JwtUtils;
import com.wangxiaolu.promotion.common.util.JwtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......
package com.wangxiaolu.promotion.utils;
import java.util.Random;
import java.util.regex.Pattern;
/**
* 人员信息校验
*/
public class DataUtils {
private static final String ID_CARD_PATTERN = "^[1-9]\\d{5}(19|20)\\d{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2]\\d|3[0-1])|(04|06|09|11)(0[1-9]|[1-2]\\d|30)|02(0[1-9]|[1-2]\\d))\\d{3}[\\dXx]$";
private static final String PHONE_NUMBER_PATTERN = "^1[3-9]\\d{9}$";
/**
* 身份证号检验
*/
public static boolean idenCardPattern(String idCard) {
return Pattern.matches(ID_CARD_PATTERN, idCard);
}
/**
* 手机号验证,是否合法
*
* @param phoneNumber 手机号
* @return 是否合法
*/
public static boolean phonePattern(String phoneNumber) {
return phoneNumber.matches(PHONE_NUMBER_PATTERN);
}
/**
* 生成6位数字验证码
*/
public static String phoneVerCode() {
String code = "";
Random random = new Random();
for (int i = 0; i < 6; i++) {
int ri = random.nextInt(10);
code += ri;
}
return code;
}
}
package com.wangxiaolu.promotion.utils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.wangxiaolu.promotion.exception.ParamException;
import com.wangxiaolu.promotion.result.basedata.RCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author : liqiulin
* @date : 2024-04-16 17
* @describe : java web token
*/
@Slf4j
@Component
public class JwtUtils {
/**
* 促销员-解密秘钥
*/
@Value("${wx.miniapp.temporary.token_secret}")
private String TEMPORARY_SECRET;
public String getTemporaryToken(String openId, String phone) {
try {
//用秘钥生成签名
Algorithm algorithm = Algorithm.HMAC256(TEMPORARY_SECRET);
//默认头部+载荷(手机号/id)+签名=jwt
String jwtToken = JWT.create()
.withClaim("openId", openId)
.withClaim("phone", phone)
.sign(algorithm);
return jwtToken;
} catch (Exception e) {
log.error("用户{}的token生成异常:{}", openId, e);
throw new ParamException(RCode.LOGIN_PARAM_ERROR, e.getMessage());
}
}
/**
* 校验token是否正确
**/
public boolean verifyTemporaryToken(String token,String openId, String phone) {
try {
// 生成JWT效验器
Algorithm algorithm = Algorithm.HMAC256(TEMPORARY_SECRET);
JWTVerifier verifier = JWT.require(algorithm).build();
verifier.verify(token);
return true;
// DecodedJWT jwt = JWT.decode(token);
// if (
// openId.equals(jwt.getClaim("openId").asString())
// && phone.equals(jwt.getClaim("phone").asString())
// ){
// return true;
// }
// return false;
} catch (Exception e) {
log.error("用户{}的token解析异常:{}", openId, e);
throw new ParamException(RCode.LOGIN_PARAM_ERROR, e.getMessage());
}
}
}
......@@ -11,6 +11,13 @@ spring:
database: 0
password: u)R3jrHk(qwt~mv$Tg=U
cloud:
nacos:
discovery:
server-addr: 42.193.103.153:8848
namespace: dd681f3c-0d21-42e5-a96e-14863a7cdcdb
group: promotion
logging:
config: classpath:logback-spring.xml
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论