From 6c82b0f944a0d3547659db5ec1f640a9ec7d3fd3 Mon Sep 17 00:00:00 2001 From: xiaoyong931011 <15274802129@163.com> Date: Wed, 15 Jun 2022 11:08:52 +0800 Subject: [PATCH] 20220606 --- src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CommonServiceImpl.java | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CommonServiceImpl.java b/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CommonServiceImpl.java index dffba4e..51f3f21 100644 --- a/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CommonServiceImpl.java +++ b/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CommonServiceImpl.java @@ -8,17 +8,24 @@ import com.xcong.farmer.cms.common.response.Result; import com.xcong.farmer.cms.configurations.properties.ApplicationProperties; import com.xcong.farmer.cms.configurations.properties.SecurityProperties; -import com.xcong.farmer.cms.modules.member.entity.MemberEntity; import com.xcong.farmer.cms.modules.system.dto.AdminLoginDto; import com.xcong.farmer.cms.modules.system.entity.UserEntity; import com.xcong.farmer.cms.modules.system.mapper.UserMapper; import com.xcong.farmer.cms.modules.system.service.ICommonService; +import com.xcong.farmer.cms.modules.system.util.CaptchaUtil; import com.xcong.farmer.cms.modules.system.util.LoginUserUtil; -import com.xcong.farmer.cms.utils.MessageSourceUtils; +import com.xcong.farmer.cms.modules.system.util.UUIDUtil; import com.xcong.farmer.cms.utils.RedisUtils; +import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; + +import java.io.IOException; import java.util.HashMap; import java.util.Map; @@ -40,9 +47,33 @@ private ApplicationProperties applicationProperties; @Resource private SecurityProperties securityProperties; + @Autowired + private RedisTemplate<String, Object> redisTemplate; + @Autowired + private UUIDUtil uuidUtil; + @Autowired + private CaptchaUtil captchaUtil; + //从SpringBoot的配置文件中取出过期时间 + @Value("${server.servlet.session.timeout}") + private Integer timeout; @Override public Result login(AdminLoginDto adminLoginDto) { + + //根据前端传回的token在redis中找对应的value + ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue(); + String codeToken = adminLoginDto.getCodeToken(); + String codeValue = adminLoginDto.getCodeValue(); + if (redisTemplate.hasKey(codeToken)) { + //验证通过, 删除对应的key + if (valueOperations.get(codeToken).equals(codeValue)) { + redisTemplate.delete(codeToken); + } else { + return Result.fail("请输入正确验证码"); + } + } else { + return Result.fail("验证码已过期,请刷新当前页面"); + } String username = adminLoginDto.getUsername(); String password = adminLoginDto.getPassword(); UserEntity userEntity = userMapper.selectByUserNameAndPassword(username, SecureUtil.md5(password)); @@ -93,6 +124,26 @@ return Result.ok("退出成功"); } + @Override + public Map<String, Object> createToken(String captcha) { + //生成一个token + String key = uuidUtil.getUUID32(); + //生成验证码对应的token 以token为key 验证码为value存在redis中 + ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue(); + valueOperations.set(key, captcha); + //设置验证码过期时间 + redisTemplate.expire(key, timeout, TimeUnit.MINUTES); + Map<String, Object> map = new HashMap<>(); + map.put("token", key); + map.put("expire", timeout); + return map; + } + + @Override + public Result captchaCreator() throws IOException { + return captchaUtil.catchaImgCreator(); + } + public String generateAsaToken(String token) { RSA rsa = new RSA(null, securityProperties.getPublicKey()); return rsa.encryptBase64(token + "_" + System.currentTimeMillis(), KeyType.PublicKey); -- Gitblit v1.9.1