From 3e6909572484fa6c4d7ec2af12a7553de09ed694 Mon Sep 17 00:00:00 2001 From: sxu <602087911@qq.com> Date: Mon, 27 Jan 2025 13:53:28 +0800 Subject: [PATCH] login --- .../main/java/net/xnzn/utils/RedisCache.java | 538 +++++++++--------- .../core/customer/mapper/CustInfoMapper.java | 2 +- .../service/impl/CustInfoBusinessImpl.java | 52 +- .../xnzn/core/notice/v2/api/SmsCodeApi.java | 60 +- 4 files changed, 344 insertions(+), 308 deletions(-) diff --git a/bonus-common-biz/src/main/java/net/xnzn/utils/RedisCache.java b/bonus-common-biz/src/main/java/net/xnzn/utils/RedisCache.java index c2ed3662..4a6b024f 100644 --- a/bonus-common-biz/src/main/java/net/xnzn/utils/RedisCache.java +++ b/bonus-common-biz/src/main/java/net/xnzn/utils/RedisCache.java @@ -1,269 +1,269 @@ -package net.xnzn.utils; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.BoundSetOperations; -import org.springframework.data.redis.core.HashOperations; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.ValueOperations; -import org.springframework.stereotype.Component; - -import java.util.*; -import java.util.concurrent.TimeUnit; - -/** - * spring redis 工具类 - * - **/ -@Component -public class RedisCache -{ - @Autowired - public RedisTemplate redisTemplate; - - //添加分布式锁 - public Boolean setNxCacheObject(final String key, final T value,long lt,TimeUnit tu) - { - return redisTemplate.opsForValue().setIfAbsent(key,value,lt,tu); - } - - /** - * 缓存基本的对象,Integer、String、实体类等 - * - * @param key 缓存的键值 - * @param value 缓存的值 - */ - public void setCacheObject(final String key, final T value) - { - redisTemplate.opsForValue().set(key, value); - } - - /** - * 缓存基本的对象,Integer、String、实体类等 - * - * @param key 缓存的键值 - * @param value 缓存的值 - * @param timeout 时间 - * @param timeUnit 时间颗粒度 - */ - public void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) - { - redisTemplate.opsForValue().set(key, value, timeout, timeUnit); - } - - /** - * 设置有效时间 - * - * @param key Redis键 - * @param timeout 超时时间 - * @return true=设置成功;false=设置失败 - */ - public boolean expire(final String key, final long timeout) - { - return expire(key, timeout, TimeUnit.SECONDS); - } - - /** - * 设置有效时间 - * - * @param key Redis键 - * @param timeout 超时时间 - * @param unit 时间单位 - * @return true=设置成功;false=设置失败 - */ - public boolean expire(final String key, final long timeout, final TimeUnit unit) - { - return redisTemplate.expire(key, timeout, unit); - } - - /** - * 获取有效时间 - * - * @param key Redis键 - * @return 有效时间 - */ - public long getExpire(final String key) - { - return redisTemplate.getExpire(key); - } - - /** - * 判断 key是否存在 - * - * @param key 键 - * @return true 存在 false不存在 - */ - public Boolean hasKey(String key) - { - return redisTemplate.hasKey(key); - } - - /** - * 获得缓存的基本对象。 - * - * @param key 缓存键值 - * @return 缓存键值对应的数据 - */ - public T getCacheObject(final String key) - { - ValueOperations operation = redisTemplate.opsForValue(); - return operation.get(key); - } - - /** - * 删除单个对象 - * - * @param key - */ - public boolean deleteObject(final String key) - { - return redisTemplate.delete(key); - } - - /** - * 删除集合对象 - * - * @param collection 多个对象 - * @return - */ - public boolean deleteObject(final Collection collection) - { - return redisTemplate.delete(collection) > 0; - } - - /** - * 缓存List数据 - * - * @param key 缓存的键值 - * @param dataList 待缓存的List数据 - * @return 缓存的对象 - */ - public long setCacheList(final String key, final List dataList) - { - Long count = redisTemplate.opsForList().rightPushAll(key, dataList); - return count == null ? 0 : count; - } - - /** - * 获得缓存的list对象 - * - * @param key 缓存的键值 - * @return 缓存键值对应的数据 - */ - public List getCacheList(final String key) - { - return redisTemplate.opsForList().range(key, 0, -1); - } - - /** - * 缓存Set - * - * @param key 缓存键值 - * @param dataSet 缓存的数据 - * @return 缓存数据的对象 - */ - public BoundSetOperations setCacheSet(final String key, final Set dataSet) - { - BoundSetOperations setOperation = redisTemplate.boundSetOps(key); - Iterator it = dataSet.iterator(); - while (it.hasNext()) - { - setOperation.add(it.next()); - } - return setOperation; - } - - /** - * 获得缓存的set - * - * @param key - * @return - */ - public Set getCacheSet(final String key) - { - return redisTemplate.opsForSet().members(key); - } - - /** - * 缓存Map - * - * @param key - * @param dataMap - */ - public void setCacheMap(final String key, final Map dataMap) - { - if (dataMap != null) { - redisTemplate.opsForHash().putAll(key, dataMap); - } - } - - /** - * 获得缓存的Map - * - * @param key - * @return - */ - public Map getCacheMap(final String key) - { - return redisTemplate.opsForHash().entries(key); - } - - /** - * 往Hash中存入数据 - * - * @param key Redis键 - * @param hKey Hash键 - * @param value 值 - */ - public void setCacheMapValue(final String key, final String hKey, final T value) - { - redisTemplate.opsForHash().put(key, hKey, value); - } - - /** - * 获取Hash中的数据 - * - * @param key Redis键 - * @param hKey Hash键 - * @return Hash中的对象 - */ - public T getCacheMapValue(final String key, final String hKey) - { - HashOperations opsForHash = redisTemplate.opsForHash(); - return opsForHash.get(key, hKey); - } - - /** - * 获取多个Hash中的数据 - * - * @param key Redis键 - * @param hKeys Hash键集合 - * @return Hash对象集合 - */ - public List getMultiCacheMapValue(final String key, final Collection hKeys) - { - return redisTemplate.opsForHash().multiGet(key, hKeys); - } - - /** - * 删除Hash中的某条数据 - * - * @param key Redis键 - * @param hKey Hash键 - * @return 是否成功 - */ - public boolean deleteCacheMapValue(final String key, final String hKey) - { - return redisTemplate.opsForHash().delete(key, hKey) > 0; - } - - /** - * 获得缓存的基本对象列表 - * - * @param pattern 字符串前缀 - * @return 对象列表 - */ - public Collection keys(final String pattern) - { - return redisTemplate.keys(pattern); - } -} +//package net.xnzn.utils; +// +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.data.redis.core.BoundSetOperations; +//import org.springframework.data.redis.core.HashOperations; +//import org.springframework.data.redis.core.RedisTemplate; +//import org.springframework.data.redis.core.ValueOperations; +//import org.springframework.stereotype.Component; +// +//import java.util.*; +//import java.util.concurrent.TimeUnit; +// +///** +// * spring redis 工具类 +// * +// **/ +//@Component +//public class RedisCache +//{ +// @Autowired +// public RedisTemplate redisTemplate; +// +// //添加分布式锁 +// public Boolean setNxCacheObject(final String key, final T value,long lt,TimeUnit tu) +// { +// return redisTemplate.opsForValue().setIfAbsent(key,value,lt,tu); +// } +// +// /** +// * 缓存基本的对象,Integer、String、实体类等 +// * +// * @param key 缓存的键值 +// * @param value 缓存的值 +// */ +// public void setCacheObject(final String key, final T value) +// { +// redisTemplate.opsForValue().set(key, value); +// } +// +// /** +// * 缓存基本的对象,Integer、String、实体类等 +// * +// * @param key 缓存的键值 +// * @param value 缓存的值 +// * @param timeout 时间 +// * @param timeUnit 时间颗粒度 +// */ +// public void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) +// { +// redisTemplate.opsForValue().set(key, value, timeout, timeUnit); +// } +// +// /** +// * 设置有效时间 +// * +// * @param key Redis键 +// * @param timeout 超时时间 +// * @return true=设置成功;false=设置失败 +// */ +// public boolean expire(final String key, final long timeout) +// { +// return expire(key, timeout, TimeUnit.SECONDS); +// } +// +// /** +// * 设置有效时间 +// * +// * @param key Redis键 +// * @param timeout 超时时间 +// * @param unit 时间单位 +// * @return true=设置成功;false=设置失败 +// */ +// public boolean expire(final String key, final long timeout, final TimeUnit unit) +// { +// return redisTemplate.expire(key, timeout, unit); +// } +// +// /** +// * 获取有效时间 +// * +// * @param key Redis键 +// * @return 有效时间 +// */ +// public long getExpire(final String key) +// { +// return redisTemplate.getExpire(key); +// } +// +// /** +// * 判断 key是否存在 +// * +// * @param key 键 +// * @return true 存在 false不存在 +// */ +// public Boolean hasKey(String key) +// { +// return redisTemplate.hasKey(key); +// } +// +// /** +// * 获得缓存的基本对象。 +// * +// * @param key 缓存键值 +// * @return 缓存键值对应的数据 +// */ +// public T getCacheObject(final String key) +// { +// ValueOperations operation = redisTemplate.opsForValue(); +// return operation.get(key); +// } +// +// /** +// * 删除单个对象 +// * +// * @param key +// */ +// public boolean deleteObject(final String key) +// { +// return redisTemplate.delete(key); +// } +// +// /** +// * 删除集合对象 +// * +// * @param collection 多个对象 +// * @return +// */ +// public boolean deleteObject(final Collection collection) +// { +// return redisTemplate.delete(collection) > 0; +// } +// +// /** +// * 缓存List数据 +// * +// * @param key 缓存的键值 +// * @param dataList 待缓存的List数据 +// * @return 缓存的对象 +// */ +// public long setCacheList(final String key, final List dataList) +// { +// Long count = redisTemplate.opsForList().rightPushAll(key, dataList); +// return count == null ? 0 : count; +// } +// +// /** +// * 获得缓存的list对象 +// * +// * @param key 缓存的键值 +// * @return 缓存键值对应的数据 +// */ +// public List getCacheList(final String key) +// { +// return redisTemplate.opsForList().range(key, 0, -1); +// } +// +// /** +// * 缓存Set +// * +// * @param key 缓存键值 +// * @param dataSet 缓存的数据 +// * @return 缓存数据的对象 +// */ +// public BoundSetOperations setCacheSet(final String key, final Set dataSet) +// { +// BoundSetOperations setOperation = redisTemplate.boundSetOps(key); +// Iterator it = dataSet.iterator(); +// while (it.hasNext()) +// { +// setOperation.add(it.next()); +// } +// return setOperation; +// } +// +// /** +// * 获得缓存的set +// * +// * @param key +// * @return +// */ +// public Set getCacheSet(final String key) +// { +// return redisTemplate.opsForSet().members(key); +// } +// +// /** +// * 缓存Map +// * +// * @param key +// * @param dataMap +// */ +// public void setCacheMap(final String key, final Map dataMap) +// { +// if (dataMap != null) { +// redisTemplate.opsForHash().putAll(key, dataMap); +// } +// } +// +// /** +// * 获得缓存的Map +// * +// * @param key +// * @return +// */ +// public Map getCacheMap(final String key) +// { +// return redisTemplate.opsForHash().entries(key); +// } +// +// /** +// * 往Hash中存入数据 +// * +// * @param key Redis键 +// * @param hKey Hash键 +// * @param value 值 +// */ +// public void setCacheMapValue(final String key, final String hKey, final T value) +// { +// redisTemplate.opsForHash().put(key, hKey, value); +// } +// +// /** +// * 获取Hash中的数据 +// * +// * @param key Redis键 +// * @param hKey Hash键 +// * @return Hash中的对象 +// */ +// public T getCacheMapValue(final String key, final String hKey) +// { +// HashOperations opsForHash = redisTemplate.opsForHash(); +// return opsForHash.get(key, hKey); +// } +// +// /** +// * 获取多个Hash中的数据 +// * +// * @param key Redis键 +// * @param hKeys Hash键集合 +// * @return Hash对象集合 +// */ +// public List getMultiCacheMapValue(final String key, final Collection hKeys) +// { +// return redisTemplate.opsForHash().multiGet(key, hKeys); +// } +// +// /** +// * 删除Hash中的某条数据 +// * +// * @param key Redis键 +// * @param hKey Hash键 +// * @return 是否成功 +// */ +// public boolean deleteCacheMapValue(final String key, final String hKey) +// { +// return redisTemplate.opsForHash().delete(key, hKey) > 0; +// } +// +// /** +// * 获得缓存的基本对象列表 +// * +// * @param pattern 字符串前缀 +// * @return 对象列表 +// */ +// public Collection keys(final String pattern) +// { +// return redisTemplate.keys(pattern); +// } +//} diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/mapper/CustInfoMapper.java b/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/mapper/CustInfoMapper.java index 01ca8219..b6ef0f38 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/mapper/CustInfoMapper.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/mapper/CustInfoMapper.java @@ -9,7 +9,7 @@ import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; @Mapper -public interface CustInfoMapper extends BaseMapper { +public interface CustInfoMapper { //extends BaseMapper { // @Select({"select cust_id, cust_name, cust_num, cust_photo_url, mobile ,pwd from cust_info ${ew.customSqlSegment}"}) // CustInfoAppIdLoginVO selectLoginInfo(@Param("ew") Wrapper wrapper); diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/service/impl/CustInfoBusinessImpl.java b/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/service/impl/CustInfoBusinessImpl.java index 410fa8bc..96f925b7 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/service/impl/CustInfoBusinessImpl.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/service/impl/CustInfoBusinessImpl.java @@ -7,7 +7,14 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.bonus.common.core.constant.SecurityConstants; import com.bonus.common.core.exception.ServiceException; +import com.bonus.common.core.utils.JwtUtils; +import com.bonus.common.core.utils.ip.IpUtils; +import com.bonus.common.core.utils.uuid.IdUtils; +import com.bonus.common.core.web.domain.AjaxResult; +import com.bonus.system.api.domain.SysUser; +import com.bonus.system.api.model.LoginUser; import net.xnzn.core.common.encrypt.SM4EncryptUtils; import net.xnzn.core.common.enums.DelFlagEnum; import net.xnzn.core.common.utils.AesEncryptUtil; @@ -31,6 +38,12 @@ import org.springframework.context.annotation.Lazy; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; +import javax.annotation.Resource; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + @Service public class CustInfoBusinessImpl implements CustInfoBusiness { private static final Logger log = LoggerFactory.getLogger(CustInfoBusinessImpl.class); @@ -41,10 +54,10 @@ public class CustInfoBusinessImpl implements CustInfoBusiness { @Autowired private SmsCodeApi smsCodeApi; - @Autowired + @Autowired(required=true) private CustInfoMapper custInfoMapper; - @Autowired + @Autowired(required=true) private CustCasualMapper custCasualMapper; public CustInfoAppIdLoginVO customLoginWithAppId(CustInfoAppIdLoginDTO content, Integer sourceType) { @@ -128,4 +141,39 @@ public class CustInfoBusinessImpl implements CustInfoBusiness { return result; } + + /** + * 创建令牌 + */ +// public Map createToken(LoginUser loginUser) { +// // 检查并删除已有的token +// delExistingToken(loginUser.getSysUser().getUserId()); +// String token = IdUtils.fastUUID(); +// Long userId = loginUser.getSysUser().getUserId(); +// String userName = loginUser.getSysUser().getUserName(); +// loginUser.setToken(token); +// loginUser.setUserid(userId); +// loginUser.setUsername(userName); +// loginUser.setIpaddr(IpUtils.getIpAddr()); +// refreshToken(loginUser); +// // Jwt存储信息 +// Map claimsMap = new HashMap(16); +// claimsMap.put(SecurityConstants.USER_KEY, token); +// claimsMap.put(SecurityConstants.DETAILS_USER_ID, userId); +// claimsMap.put(SecurityConstants.DETAILS_USERNAME, userName); +// String accessToken = JwtUtils.createToken(claimsMap); +// Map rspMap = new HashMap(16); +// rspMap.put("access_token", accessToken); +// rspMap.put("expires_in", EXPIRETIME); +// rspMap.put("isLogin", isLogin(String.valueOf(userId))); +// long tokenTime = getTokenTime(); +// //对token进行存储 +// redisService.setCacheObject(LOGIN_USER_KEY + userId, token, tokenTime, TimeUnit.MINUTES); +// SysUser sysUser = new SysUser(); +// sysUser.setUserId(loginUser.getSysUser().getUserId()); +// sysUser.setLoginDate(new Date()); +// AjaxResult edit = remoteUserService.systemUpdateUser(sysUser, SecurityConstants.INNER); +// return rspMap; +// } + } diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/notice/v2/api/SmsCodeApi.java b/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/notice/v2/api/SmsCodeApi.java index 266f57d9..dc2974ef 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/notice/v2/api/SmsCodeApi.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/notice/v2/api/SmsCodeApi.java @@ -3,9 +3,9 @@ package net.xnzn.core.notice.v2.api; import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.bonus.common.core.exception.ServiceException; +import com.bonus.common.redis.service.RedisService; import net.xnzn.core.common.constant.LeMqConstant; import net.xnzn.core.merchant.dto.SmsCodeVerifyDTO; -import net.xnzn.utils.RedisCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -22,28 +22,28 @@ public class SmsCodeApi { private static final Logger log = LoggerFactory.getLogger(SmsCodeApi.class); @Autowired - private RedisCache redisCache; - - public void sendSmsCodePost(String telephoneNumber) { - this.sendSmsCodePost(telephoneNumber, "code_"); - } - - public void sendSmsCodePost(String telephoneNumber, String cacheKey) { - String limitKey = "limit_" + telephoneNumber; - LocalDateTime now = LocalDateTime.now(); - LocalDateTime endOfDay = now.with(LocalTime.MAX); - long expirTime = Math.max(1L, Duration.between(now, endOfDay).getSeconds()); - String lastSendTimeKey = "last_send_time_" + telephoneNumber; - String lastSendTime = redisCache.getCacheObject(lastSendTimeKey); - if (lastSendTime != null) { - long lastSendTimestamp = Long.parseLong(lastSendTime); - long currentTimestamp = System.currentTimeMillis(); - long timeElapsed = currentTimestamp - lastSendTimestamp; - if (timeElapsed < 60000L) { - throw new ServiceException("验证码重复"); - } - } + private RedisService redisService; +// public void sendSmsCodePost(String telephoneNumber) { +// this.sendSmsCodePost(telephoneNumber, "code_"); +// } +// +// public void sendSmsCodePost(String telephoneNumber, String cacheKey) { +// String limitKey = "limit_" + telephoneNumber; +// LocalDateTime now = LocalDateTime.now(); +// LocalDateTime endOfDay = now.with(LocalTime.MAX); +// long expirTime = Math.max(1L, Duration.between(now, endOfDay).getSeconds()); +// String lastSendTimeKey = "last_send_time_" + telephoneNumber; +// String lastSendTime = redisCache.getCacheObject(lastSendTimeKey); +// if (lastSendTime != null) { +// long lastSendTimestamp = Long.parseLong(lastSendTime); +// long currentTimestamp = System.currentTimeMillis(); +// long timeElapsed = currentTimestamp - lastSendTimestamp; +// if (timeElapsed < 60000L) { +// throw new ServiceException("验证码重复"); +// } +// } +// // Integer times = RedisUtil.incr(limitKey, expirTime); // if (times > 5) { // throw new ServiceException("验证码时间超过限制"); @@ -60,19 +60,7 @@ public class SmsCodeApi { // //redisCache.setNxCacheObject(lastSendTimeKey, String.valueOf(System.currentTimeMillis())); //TODO // redisCache.setNxCacheObject(lastSendTimeKey, String.valueOf(System.currentTimeMillis()), 300L, TimeUnit.SECONDS); // } - - int code = (int)((Math.random() * 9.0 + 1.0) * 100000.0); - String codeString = "" + code; - Map maps = new HashMap(); - maps.put("telephoneNumber", telephoneNumber); - maps.put("sendCode", codeString); - log.info("验证码发送code : {}", codeString); - //MqUtil.send(JSON.toJSONString(maps), LeMqConstant.Topic.NOTICE_VERIFICATION_CODE); - String key = cacheKey + telephoneNumber; - redisCache.setNxCacheObject(key, codeString, 300L, TimeUnit.SECONDS); - //redisCache.setNxCacheObject(lastSendTimeKey, String.valueOf(System.currentTimeMillis())); //TODO - redisCache.setNxCacheObject(lastSendTimeKey, String.valueOf(System.currentTimeMillis()), 300L, TimeUnit.SECONDS); - } +// } public boolean verifySmsCode(SmsCodeVerifyDTO smsCodeVerifyDTO) { return this.verifySmsCode(smsCodeVerifyDTO, "code_"); @@ -80,7 +68,7 @@ public class SmsCodeApi { public boolean verifySmsCode(SmsCodeVerifyDTO smsCodeVerifyDTO, String cacheKey) { String key = cacheKey + smsCodeVerifyDTO.getTelephoneNumber(); - String code = redisCache.getCacheObject(key); + String code = redisService.getCacheObject(key); log.info("redis缓存验证码code : {}", code); return ObjectUtil.isNotEmpty(code) && ObjectUtil.equal(code, smsCodeVerifyDTO.getCode()); }